Class: Artisanal::Model::Attribute
- Inherits:
-
Module
- Object
- Module
- Artisanal::Model::Attribute
- Defined in:
- lib/artisanal/model/attribute.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #included(base) ⇒ Object
-
#initialize(name, coercer = nil, **options) ⇒ Attribute
constructor
A new instance of Attribute.
Constructor Details
#initialize(name, coercer = nil, **options) ⇒ Attribute
Returns a new instance of Attribute.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/artisanal/model/attribute.rb', line 5 def initialize(name, coercer=nil, **) @name, @options = name, @type = coercer || [:type] # Convert :from option to :as if .has_key? :from @name, [:as] = [:from], name end # Add default values for certain types unless .has_key?(:default) .merge!(default: type_default) end raise ArgumentError.new("type missing for attribute #{name}") if type.nil? end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/artisanal/model/attribute.rb', line 3 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/artisanal/model/attribute.rb', line 3 def @options end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
3 4 5 |
# File 'lib/artisanal/model/attribute.rb', line 3 def type @type end |
Instance Method Details
#included(base) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/artisanal/model/attribute.rb', line 22 def included(base) # Create dry-initializer option base.option(name, **.merge(type: type_builder)) # Create writer method define_writer(base, name) if [:writer] end |