Closed avdi closed 11 years ago
@avdi would something like this do the trick? Or by option do you mean make this configurable? If so I can submit a pull request.
def stub_method_returning_self(subject, name) subject.module_eval do define_method(name) do |*args| if args[0] && args[0].match(/(.*)\?/) return false else return self end end end end
I think that this is correct, but making it configurable adds flexibility. On the other hand, Allowing all objects to use this options also add flexibility.
I'd like it to be a separate builder method - predicates_return(false)
or
something along those lines.
Great thanks for the clarification. Tied up with some work right now, will work on this later today. Thanks!
To be least-surprising, this will have to work in several contexts:
defer
block, go through all the instance_method
s currently defined on
subject
, and rewrite each one that ends in '?'.)Bonus points for doing this completely in a command object without putting
any new logic in NullClassBuilder
!
Bonus points for doing this completely in a command object without putting any new logic in
NullClassBuilder
!
Sorry, I really wanted to get this in so I went ahead and wrote it. I didn't quite qualify for bonus points, although I managed to do it with minimal changes to the NullClassBuilder
.
Presently all generated predicate methods (methods ending in
?
) return the same default value as any other method: eithernil
orself
, depending on the configuration. It might be nice to have an option to automatically define predicates to returnfalse
instead, to better comply with legacy code expectations. This is particularly important for black hole objects, sinceself
is truthy for the purpose ofif
statements etc.