Class: Constructable::Attribute
- Inherits:
-
Object
- Object
- Constructable::Attribute
- Defined in:
- lib/constructable/attribute.rb
Constant Summary collapse
- ATTRIBUTES =
[:group, :writable, :readable, :accessible, :required, :validate, :default, :validate_type, :converter]
- REQUIREMENTS =
[ { name: :validate_type, message: proc {":#{self.name} must be of type #{self.validate_type}"}, check: ->(value) { value.is_a? self.validate_type } }, { name: :validate, message: proc {":#{self.name} did not pass validation"}, check: ->(value) { self.validate.call(value)} } ]
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.
19 20 21 22 23 24 |
# File 'lib/constructable/attribute.rb', line 19 def initialize(name, = {}) @name = name ATTRIBUTES.each do |attribute| self.send(:"#{attribute}=", [attribute]) end end |
Instance Method Details
#accessible=(boolean) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/constructable/attribute.rb', line 26 def accessible=(boolean) if boolean self.readable = true self.writable = true end end |
#attr_writer_symbol ⇒ Object
37 38 39 |
# File 'lib/constructable/attribute.rb', line 37 def attr_writer_symbol (self.name.to_s + '=').to_sym end |
#ivar_symbol ⇒ Object
33 34 35 |
# File 'lib/constructable/attribute.rb', line 33 def ivar_symbol ('@' + self.name.to_s).to_sym end |
#process(value) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/constructable/attribute.rb', line 50 def process(value) unless value.nil? REQUIREMENTS.each do |requirement| check_for_requirement(requirement, value) end 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 |