Class: Constructable::Attribute
- Inherits:
-
Object
- Object
- Constructable::Attribute
- Defined in:
- lib/constructable/attribute.rb
Constant Summary collapse
- ATTRIBUTES =
[:group, :writable, :readable, :accessible, :required, :default, :converter]
Instance Method Summary collapse
- #accessible=(boolean) ⇒ Object
- #attr_writer_symbol ⇒ Object
-
#initialize(name, options = {}) ⇒ Attribute
constructor
A new instance of Attribute.
- #ivar_symbol ⇒ Object
- #process(value) ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Attribute
Returns a new instance of Attribute.
6 7 8 9 10 11 |
# File 'lib/constructable/attribute.rb', line 6 def initialize(name, = {}) @name = name ATTRIBUTES.each do |attribute| self.send(:"#{attribute}=", [attribute]) end end |
Instance Method Details
#accessible=(boolean) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/constructable/attribute.rb', line 13 def accessible=(boolean) if boolean self.readable = true self.writable = true end end |
#attr_writer_symbol ⇒ Object
24 25 26 |
# File 'lib/constructable/attribute.rb', line 24 def attr_writer_symbol (self.name.to_s + '=').to_sym end |
#ivar_symbol ⇒ Object
20 21 22 |
# File 'lib/constructable/attribute.rb', line 20 def ivar_symbol ('@' + self.name.to_s).to_sym end |
#process(value) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/constructable/attribute.rb', line 28 def process(value) unless value.nil? self.converter ? converter.(value) : value else raise AttributeError, ":#{self.name} is a required attribute" if self.required self.default.call if self.default end end |