Class: Granite::Represents::Attribute

Inherits:
ActiveData::Model::Attributes::Attribute
  • Object
show all
Defined in:
lib/granite/represents/attribute.rb

Instance Method Summary collapse

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

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/granite/represents/attribute.rb', line 38

def changed?
  if reflection.options[:default].present?
    reference.public_send(reader) != read
  else
    owner.public_send("#{name}_changed?")
  end
end

#syncObject



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

#typeObject



25
26
27
28
29
# File 'lib/granite/represents/attribute.rb', line 25

def type
  return reflection.options[:type] if reflection.options[: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

#typecasterObject



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