Support for 'predicate' and 'clearer' methods
-
Sean O'Halpin
- State changed from new to resolved
As you have pointed out, #instance_variable_defined? and #remove_instance_variable do exactly what you want.
However, using them does rely on the underlying implementation of doodle, so I take your point. I really don't like the 'attribute query' methods of ActiveRecord (e.g. foo.name?) - IMHO they just add to the namespace clutter so I prefer two methods that take arguments over creating two more new methods per attribute.
Doodle already has #assigned?(name). I have just added #clear!(name), so that will be in the next release (real soon now). For example:
class Foo < Doodle has :name end foo = Foo("Jim") foo.assigned?(:name) # => true foo.clear!(:name) foo.assigned?(:name) # => false -
Justin Ossevoort
The clearer is great, that really complements the <.assigned?>
I agree that the ActiveRecord way is ugly for it really conflicts with the way you'd expect boolean attributes to behave.
I was thinking more along the lines of:class Foo < Doodle has :foo, :default => 'bar' end foo = Foo.new foo.assigned?(:name) # => false foo.has?(:name) # => true foo.foo = 'foobar' foo.assigned?(:name) # => true foo.has?(:name) # => trueWhere <.has?> nicely mirrors the declarative form of Doodle.
-
Justin Ossevoort
- Tag set to feature
-
Sean O'Halpin
I like #has?
obj.has?(name) would be syntactic sugar for
obj.doodle.attributes.key?(name) where obj can be either a class or an
instance.I'll include this in the next release (so we'll have #has?, #assigned?
and #clear!).While we're at it, can you think of any other reflections that should
be exposed at the class or instance level? -
Justin Ossevoort
I've been mulling over this for some days, but for these cover the really essential introspection needs. The only possibly relevant one that's possibly missing is whether or not something has a default value.
For this example I use
valued?as the property, but I haven't given much thought for a name:require 'doodle' class Foo has simple_attr has default_attr do default{ 'bar' } end has init_attr do init{ 'bar' } end end f = Foo.new f.has?(:simple_attr) # => true f.valued?(:init_attr) # => false f.assigned?(:init_attr) # => false f.has?(:default_attr) # => true f.valued?(:default_attr) # => true f.assigned?(:default_attr) # => false f.has?(:init_attr) # => true f.valued?(:init_attr) # => true f.assigned?(:init_attr) # => trueThe biggest problem with this one is that I haven't come up with a clear scenario for this information (though it clearly conveys information).
-
Justin Ossevoort
oh and the first block should of course have read:
f.has?(:simple_attr) # => true f.valued?(:simple_attr) # => false f.assigned?(:simple_attr) # => falseClear case of copy/past bug ;-)
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.