-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a criteria method to the type adapter #591
Add a criteria method to the type adapter #591
Conversation
1eb65f9
to
f5fbd7c
Compare
f5fbd7c
to
f59073f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably fine, just that one part confusing me a little
{% if column[:type].is_a?(Generic) %} | ||
# Checking Array type | ||
generate_criteria_method(BaseQuery, {{ column[:name] }}, {{ column[:type].type_vars.first }}) | ||
generate_criteria_method({{ column[:name] }}, {{ column[:type] }}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little confused on why we don't need to check for Array(String)
and it will know to use String.adapter
🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given a type of Array(String)
it will call Array(String).adapter
and that method calls .adapter
on the generic type of the array which in this case is String.adapter
.
avram/src/avram/charms/array_extensions.cr
Lines 1 to 7 in efe1d8a
class Array(T) | |
# Proxy to the `T`'s adapter so we can call methods like | |
# `Array(String).adapter.to_db(["test"])` | |
def self.adapter | |
T.adapter | |
end | |
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahh! boom! I missed that part.
No connected issue
After #587, there was a still one place with a hard-coded reference to the type extension namespaces
avram/src/avram/base_query_template.cr
Lines 14 to 19 in 5f51beb
This adds a
criteria
method that takes in the query and the column name and returns an instance of anAvram::Criteria
for the given type.One of the other benefits it gives us is that we don't need special handling for generic types since we are calling methods on the class instead of using it to access a namespace.
This would be a breaking change since the
criteria
method is completely new and custom types would need to implement it.The rules for adding custom types would be:
adapter
class-level method that returns a class that includes theAvram::Type
modulecriteria
class-level method that returns an instance ofAvram::Criteria
for the typeI considered moving the
criteria
method onto the type we extend but I didn't want to pollute the core library types with another function.