Class: ActiveDelegate::Attribute::Accessor

Inherits:
Object
  • Object
show all
Defined in:
lib/active_delegate/attribute/accessor.rb

Overview

Reads, writes and type casts attribute value

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, options = {}) ⇒ Accessor

Returns a new instance of Accessor.



9
10
11
12
# File 'lib/active_delegate/attribute/accessor.rb', line 9

def initialize(record, options = {})
  @record  = record
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/active_delegate/attribute/accessor.rb', line 7

def options
  @options
end

#recordObject (readonly)

Returns the value of attribute record.



7
8
9
# File 'lib/active_delegate/attribute/accessor.rb', line 7

def record
  @record
end

Instance Method Details

#association_nameObject



18
19
20
# File 'lib/active_delegate/attribute/accessor.rb', line 18

def association_name
  options[:association].to_sym
end

#association_recordObject



38
39
40
# File 'lib/active_delegate/attribute/accessor.rb', line 38

def association_record
  record.send(association_name)
end

#attribute_nameObject



22
23
24
# File 'lib/active_delegate/attribute/accessor.rb', line 22

def attribute_name
  options[:attribute].to_sym
end

#attribute_value(*args) ⇒ Object



42
43
44
# File 'lib/active_delegate/attribute/accessor.rb', line 42

def attribute_value(*args)
  association_record.try(attribute_name, *args)
end

#cast_read_value(value) ⇒ Object



54
55
56
# File 'lib/active_delegate/attribute/accessor.rb', line 54

def cast_read_value(value)
  read_type_caster.cast(value)
end

#cast_write_value(value) ⇒ Object



58
59
60
# File 'lib/active_delegate/attribute/accessor.rb', line 58

def cast_write_value(value)
  write_type_caster.cast(value)
end

#default_valueObject



14
15
16
# File 'lib/active_delegate/attribute/accessor.rb', line 14

def default_value
  options[:default]
end

#normalize_value(value) ⇒ Object



62
63
64
65
# File 'lib/active_delegate/attribute/accessor.rb', line 62

def normalize_value(value)
  value = cast_read_value(value)
  cast_write_value(value)
end

#read(*args) ⇒ Object



67
68
69
70
# File 'lib/active_delegate/attribute/accessor.rb', line 67

def read(*args)
  value = attribute_value(*args) || default_value
  type_cast? ? cast_read_value(value) : value
end

#read_typeObject



26
27
28
# File 'lib/active_delegate/attribute/accessor.rb', line 26

def read_type
  options[:read_type]
end

#read_type_casterObject



46
47
48
# File 'lib/active_delegate/attribute/accessor.rb', line 46

def read_type_caster
  lookup_type_caster(read_type)
end

#type_cast?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/active_delegate/attribute/accessor.rb', line 34

def type_cast?
  read_type_caster.type != write_type_caster.type
end

#write(value) ⇒ Object



72
73
74
75
# File 'lib/active_delegate/attribute/accessor.rb', line 72

def write(value)
  value = normalize_value(value) if type_cast?
  association_record.send(:"#{attribute_name}=", value)
end

#write_typeObject



30
31
32
# File 'lib/active_delegate/attribute/accessor.rb', line 30

def write_type
  options[:write_type]
end

#write_type_casterObject



50
51
52
# File 'lib/active_delegate/attribute/accessor.rb', line 50

def write_type_caster
  lookup_type_caster(write_type)
end