Class: Granite::Represents::Attribute
- Inherits:
-
ActiveData::Model::Attributes::Attribute
- Object
- ActiveData::Model::Attributes::Attribute
- Granite::Represents::Attribute
- Defined in:
- lib/granite/represents/attribute.rb
Instance Method Summary collapse
- #changed? ⇒ Boolean
-
#initialize(*_args) ⇒ Attribute
constructor
A new instance of Attribute.
- #sync ⇒ Object
- #type ⇒ Object
- #typecast(value) ⇒ Object
- #typecaster ⇒ Object
Constructor Details
#initialize(*_args) ⇒ Attribute
Returns a new instance of Attribute.
6 7 8 9 10 11 |
# File 'lib/granite/represents/attribute.rb', line 6 def initialize(*_args) super set_default_value set_default_value_before_type_cast end |
Instance Method Details
#changed? ⇒ Boolean
38 39 40 41 42 43 44 |
# File 'lib/granite/represents/attribute.rb', line 38 def changed? if reflection.[:default].present? reference.public_send(reader) != read else owner.public_send("#{name}_changed?") end end |
#sync ⇒ Object
13 14 15 16 17 |
# File 'lib/granite/represents/attribute.rb', line 13 def sync return if reference.nil? reference.public_send(writer, read) end |
#type ⇒ Object
25 26 27 28 29 |
# File 'lib/granite/represents/attribute.rb', line 25 def type return reflection.[:type] if reflection.[:type].present? active_data_type || type_from_type_for_attribute || super end |
#typecast(value) ⇒ Object
19 20 21 22 23 |
# File 'lib/granite/represents/attribute.rb', line 19 def typecast(value) return value if value.class == type typecaster.call(value, self) unless value.nil? end |
#typecaster ⇒ Object
31 32 33 34 35 36 |
# File 'lib/granite/represents/attribute.rb', line 31 def typecaster @typecaster ||= begin type_class = type.instance_of?(Class) ? type : type.class @typecaster = ActiveData.typecaster(type_class.ancestors.grep(Class)) end end |