Class: Granite::Represents::Attribute
- Inherits:
-
Form::Model::Attributes::Attribute
- Object
- Form::Model::Attributes::Attribute
- Granite::Represents::Attribute
- Defined in:
- lib/granite/represents/attribute.rb
Constant Summary collapse
- TYPES =
types.freeze
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.
9 10 11 12 13 14 |
# File 'lib/granite/represents/attribute.rb', line 9 def initialize(*_args) super set_default_value set_default_value_before_type_cast end |
Instance Method Details
#changed? ⇒ Boolean
39 40 41 42 43 44 45 |
# File 'lib/granite/represents/attribute.rb', line 39 def changed? if reflection..key?(:default) reference.public_send(reader) != read else owner.public_send("#{name}_changed?") end end |
#sync ⇒ Object
16 17 18 |
# File 'lib/granite/represents/attribute.rb', line 16 def sync reference.public_send(writer, read) if reference.respond_to?(writer) end |
#type ⇒ Object
26 27 28 29 30 |
# File 'lib/granite/represents/attribute.rb', line 26 def type return reflection.[:type] if reflection.[:type].present? granite_form_type || type_from_type_for_attribute || super end |
#typecast(value) ⇒ Object
20 21 22 23 24 |
# File 'lib/granite/represents/attribute.rb', line 20 def typecast(value) return value if value.class == type # rubocop:disable Style/ClassEqualityComparison typecaster.call(value, self) unless value.nil? end |
#typecaster ⇒ Object
32 33 34 35 36 37 |
# File 'lib/granite/represents/attribute.rb', line 32 def typecaster @typecaster ||= begin type_class = type.instance_of?(Class) ? type : type.class @typecaster = Granite::Form.typecaster(type_class.ancestors.grep(Class)) end end |