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)


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

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

#syncObject



13
14
15
# File 'lib/granite/represents/attribute.rb', line 13

def sync
  reference.public_send(writer, read) if reference.respond_to?(writer)
end

#typeObject



23
24
25
26
27
# File 'lib/granite/represents/attribute.rb', line 23

def type
  return reflection.options[:type] if reflection.options[:type].present?

  active_data_type || type_from_type_for_attribute || super
end

#typecast(value) ⇒ Object



17
18
19
20
21
# File 'lib/granite/represents/attribute.rb', line 17

def typecast(value)
  return value if value.class == type

  typecaster.call(value, self) unless value.nil?
end

#typecasterObject



29
30
31
32
33
34
# File 'lib/granite/represents/attribute.rb', line 29

def typecaster
  @typecaster ||= begin
                    type_class = type.instance_of?(Class) ? type : type.class
                    @typecaster = ActiveData.typecaster(type_class.ancestors.grep(Class))
                  end
end