-
Notifications
You must be signed in to change notification settings - Fork 55
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
Idea: Predicate method for Boolean values #15
Comments
Oh hey that's cool! Yeah sure please give it a shot. There's a lot of metaprogramming there (kinda needed to) so I hope you find your way around. |
What do you think about simply solving this like here: https://github.com/stebo/storext/commit/9335e5d23c395f509c13f67c4afd7166ee984d3a |
Edit: I just came up that it maybe would be better to add the predicate method for all type of values, not only Booleans (which Rails is also doing for Strings, Integers...), so my commit could be simplified even more by just adding alias_method "#{attr}?", attr in line 22... So you could e.g. also check if a book has a title with book.title? # => true if title is set |
@stebo yes look like your commit will work for booleans. About your second comment: won't that just return the value? For example, if |
@ramontayag ahhh of course you are right, did not really made a test for non-boolean values. def storext_define_predicater(column, attr)
define_method "#{attr}?" do
if send(column) && send(column).has_key?(attr.to_s)
store_val = !!read_store_attribute(column, attr)
# storext_cast_proxy.send("#{attr}=", store_val)
else
false
end
# storext_cast_proxy.send("#{attr}")
end
end and call it in
object.attribute => nil
object.attribute? => nil # and we want false here I think we could than shorten the method to def storext_define_predicater(column, attr)
define_method "#{attr}?" do
false unless send(column) && send(column).has_key?(attr.to_s)
!!read_store_attribute(column, attr)
end
end What do you think? |
Yes I think that will work! |
Good hint, I included that. Here is the pull request: #26 |
Thanks for the contribution! |
|
😄 😄 awesome @stebo @ramontayag ! |
Hello, love this gem :)
What do you think of auto defining predicate methods for boolean values? (to match what Rails does for booleans).
If you like this idea, I'd be happy to implement.
The text was updated successfully, but these errors were encountered: