Module: Juniter::HasAttributes::ClassMethods
- Defined in:
- lib/juniter/has_attributes.rb
Instance Method Summary collapse
- #attribute(name, as: nil, required: false, validation: nil, map: nil) ⇒ Object
- #attribute_aliases ⇒ Object
- #attribute_processors ⇒ Object
- #attributes ⇒ Object
- #required_attributes ⇒ Object
Instance Method Details
#attribute(name, as: nil, required: false, validation: nil, map: nil) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/juniter/has_attributes.rb', line 12 def attribute(name, as: nil, required: false, validation: nil, map: nil) attributes << name attribute_aliases[name] = as unless as.nil? attribute_processors[name] = map || ->(value) { value } define_method :"#{name}" do instance_variable_get :"@_#{name}" if instance_variable_defined?(:"@_#{name}") end define_method :"#{name}=" do |value| send validation, value unless validation.nil? instance_variable_set :"@_#{name}", value end end |
#attribute_aliases ⇒ Object
31 32 33 |
# File 'lib/juniter/has_attributes.rb', line 31 def attribute_aliases @__attribute_aliases ||= {} end |
#attribute_processors ⇒ Object
35 36 37 |
# File 'lib/juniter/has_attributes.rb', line 35 def attribute_processors @__attribute_processors ||= {} end |
#attributes ⇒ Object
27 28 29 |
# File 'lib/juniter/has_attributes.rb', line 27 def attributes @__attributes ||= [] end |
#required_attributes ⇒ Object
39 40 41 |
# File 'lib/juniter/has_attributes.rb', line 39 def required_attributes @__required_attributes ||= [] end |