Module: OReflect::Accessors::ClassMethods
- Defined in:
- lib/oreflect/accessors.rb
Instance Method Summary collapse
- #boolean_accessor(*names) ⇒ Object
- #dynamic_accessor(*names) ⇒ Object
- #splat_accessor(*names) ⇒ Object
Instance Method Details
#boolean_accessor(*names) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/oreflect/accessors.rb', line 9 def boolean_accessor(*names) names.each do |attr_name| dynamic_accessor attr_name define_method "#{attr_name}?" do instance_variable_get("@#{attr_name}") ? true : false end define_method "#{attr_name}!" do instance_variable_set "@#{attr_name}", true end end end |
#dynamic_accessor(*names) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/oreflect/accessors.rb', line 36 def dynamic_accessor(*names) names.each do |attr_name| define_method attr_name do |*args| if args[0] instance_variable_set "@#{attr_name}", args[0] else instance_variable_get "@#{attr_name}" end end end end |
#splat_accessor(*names) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/oreflect/accessors.rb', line 23 def splat_accessor(*names) names.each do |attr_name| define_method attr_name do |*values| instance_variable_set("@#{attr_name}", []) unless instance_variable_get("@#{attr_name}") if values.empty? instance_variable_get "@#{attr_name}" else instance_variable_get("@#{attr_name}").concat(values) end end end end |