avdi / naught

A toolkit for building Null Object classes in Ruby
MIT License
1.05k stars 53 forks source link

Add option to make predicates return `false` #3

Closed avdi closed 11 years ago

avdi commented 11 years ago

Presently all generated predicate methods (methods ending in ?) return the same default value as any other method: either nil or self, depending on the configuration. It might be nice to have an option to automatically define predicates to return false instead, to better comply with legacy code expectations. This is particularly important for black hole objects, since self is truthy for the purpose of if statements etc.

guillec commented 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
fcofdez commented 11 years ago

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.

avdi commented 11 years ago

I'd like it to be a separate builder method - predicates_return(false) or something along those lines.

guillec commented 11 years ago

Great thanks for the clarification. Tied up with some work right now, will work on this later today. Thanks!

avdi commented 11 years ago

To be least-surprising, this will have to work in several contexts:

Bonus points for doing this completely in a command object without putting any new logic in NullClassBuilder!

mdespuits commented 11 years ago

Bonus points for doing this completely in a command object without putting any new logic in NullClassBuilder!

Challenge Considered

avdi commented 11 years ago

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.