Class: Toy::Attribute
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#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
- #abbr ⇒ Object
- #abbr? ⇒ Boolean
- #default ⇒ Object
- #default? ⇒ Boolean
- #eql?(other) ⇒ Boolean (also: #==)
- #from_store(value) ⇒ Object
-
#initialize(model, name, type, options = {}) ⇒ Attribute
constructor
A new instance of Attribute.
- #persisted? ⇒ Boolean
- #persisted_name ⇒ Object
- #to_store(value) ⇒ Object
- #virtual? ⇒ Boolean
Constructor Details
#initialize(model, name, type, options = {}) ⇒ Attribute
Returns a new instance of Attribute.
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/toy/attribute.rb', line 5 def initialize(model, name, type, ={}) .assert_valid_keys(:default, :virtual, :abbr) @model, @name, @type, @options = model, name.to_s, type, @virtual = .fetch(:virtual, false) if abbr? [:abbr] = abbr.to_s model.alias_attribute(abbr, name) end model.attributes[name.to_s] = self end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
3 4 5 |
# File 'lib/toy/attribute.rb', line 3 def model @model end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/toy/attribute.rb', line 3 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/toy/attribute.rb', line 3 def @options end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
3 4 5 |
# File 'lib/toy/attribute.rb', line 3 def type @type end |
Instance Method Details
#abbr ⇒ Object
58 59 60 |
# File 'lib/toy/attribute.rb', line 58 def abbr [:abbr] end |
#default ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/toy/attribute.rb', line 29 def default if .key?(:default) default = [:default] if default.respond_to?(:call) backwards_compatible_call(default) else default end else type.respond_to?(:store_default) ? type.store_default : nil end end |
#default? ⇒ Boolean
42 43 44 |
# File 'lib/toy/attribute.rb', line 42 def default? .key?(:default) || type.respond_to?(:store_default) end |
#eql?(other) ⇒ Boolean Also known as: ==
66 67 68 69 70 |
# File 'lib/toy/attribute.rb', line 66 def eql?(other) self.class.eql?(other.class) && model == other.model && name == other.name end |
#from_store(value) ⇒ Object
19 20 21 22 |
# File 'lib/toy/attribute.rb', line 19 def from_store(value) value = default if default? && value.nil? type.from_store(value, self) end |
#persisted_name ⇒ Object
62 63 64 |
# File 'lib/toy/attribute.rb', line 62 def persisted_name abbr? ? abbr : name end |
#to_store(value) ⇒ Object
24 25 26 27 |
# File 'lib/toy/attribute.rb', line 24 def to_store(value) value = default if default? && value.nil? type.to_store(value, self) end |