Class: Module
- Inherits:
-
Object
- Object
- Module
- Defined in:
- lib/everythingrb/core/module.rb
Instance Method Summary collapse
-
#attr_predicate(*attributes) ⇒ nil
Creates predicate (boolean) methods that return true/false Similar to attr_reader, attr_writer, etc.
Instance Method Details
#attr_predicate(*attributes) ⇒ nil
Creates predicate (boolean) methods that return true/false Similar to attr_reader, attr_writer, etc. Designed to work with regular classes, Struct, and Data objects.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/everythingrb/core/module.rb', line 33 def attr_predicate(*attributes) attributes.each do |attribute| if method_defined?(:"#{attribute}?") raise ArgumentError, "Cannot create predicate method on #{self.class} - #{attribute}? is already defined. Please choose a different name or remove the existing method." end module_eval <<-STR, __FILE__, __LINE__ + 1 def #{attribute}? if instance_variable_defined?(:@#{attribute}) !!@#{attribute} elsif respond_to?(:#{attribute}) !!self.#{attribute} else false end end STR end nil end |