Class: Granite::Represents::Attribute

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

Constant Summary collapse

TYPES =
types.freeze

Instance Method Summary collapse

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

Returns:

  • (Boolean)


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

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

#syncObject



16
17
18
# File 'lib/granite/represents/attribute.rb', line 16

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

#typeObject



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

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

#typecasterObject



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