-
Notifications
You must be signed in to change notification settings - Fork 35
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
Casting of boolean values #59
Comments
It's not trivial to do that because of the way core data works. You can't do it right in CDQManagedObject because CoreData will subclass your class and then add the property accessors rather than the other way around as in ActiveRecord. To work around that you can define question-mark predicate methods ("happy?"). To do it automatically you have to dig into Objective C, so I haven't got around to it. |
Couldn't this be done with ruby's def self.define_predicate_methods
bool_methods.each do |method|
define_method("#{method}?") do
self.send(method).boolValue
end
end
end |
Just updating the snippet slightly, after realising that it didn't handle def self.define_predicate_methods
methods.each do |method|
define_method("#{method}?") do
value = self.send(method)
value.nil? ? value : value.boolValue
end
end
end |
I'm don't remember why define_method didn't work for me, but if you can make it work, great. MotionData used Objective-C to get this feature, for what it's worth. |
Boolean values are stored as 0/1, and that's what CDQ returns when you query an object. Since ruby treats
0
as a truthy value, you can't do something likeif person.happy
, but instead have to doif person.happy == 1
. I'm new to CDQ, so I'm not sure if this is something that should be cast to true/false automatically?Sample schema:
Sample REPL code:
The text was updated successfully, but these errors were encountered: