Class: ActiveDelegate::Attribute::Accessor
- Inherits:
-
Object
- Object
- ActiveDelegate::Attribute::Accessor
- Defined in:
- lib/active_delegate/attribute/accessor.rb
Overview
Reads, writes and type casts attribute value
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#record ⇒ Object
readonly
Returns the value of attribute record.
Instance Method Summary collapse
- #association_name ⇒ Object
- #association_record ⇒ Object
- #attribute_name ⇒ Object
- #attribute_value(*args) ⇒ Object
- #cast_read_value(value) ⇒ Object
- #cast_write_value(value) ⇒ Object
- #default_value ⇒ Object
-
#initialize(record, options = {}) ⇒ Accessor
constructor
A new instance of Accessor.
- #normalize_value(value) ⇒ Object
- #read(*args) ⇒ Object
- #read_type ⇒ Object
- #read_type_caster ⇒ Object
- #type_cast? ⇒ Boolean
- #write(value) ⇒ Object
- #write_type ⇒ Object
- #write_type_caster ⇒ Object
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, = {}) @record = record @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/active_delegate/attribute/accessor.rb', line 7 def @options end |
#record ⇒ Object (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_name ⇒ Object
18 19 20 |
# File 'lib/active_delegate/attribute/accessor.rb', line 18 def association_name [:association].to_sym end |
#association_record ⇒ Object
38 39 40 |
# File 'lib/active_delegate/attribute/accessor.rb', line 38 def association_record record.send(association_name) end |
#attribute_name ⇒ Object
22 23 24 |
# File 'lib/active_delegate/attribute/accessor.rb', line 22 def attribute_name [: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_value ⇒ Object
14 15 16 |
# File 'lib/active_delegate/attribute/accessor.rb', line 14 def default_value [: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_type ⇒ Object
26 27 28 |
# File 'lib/active_delegate/attribute/accessor.rb', line 26 def read_type [:read_type] end |
#read_type_caster ⇒ Object
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
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_type ⇒ Object
30 31 32 |
# File 'lib/active_delegate/attribute/accessor.rb', line 30 def write_type [:write_type] end |
#write_type_caster ⇒ Object
50 51 52 |
# File 'lib/active_delegate/attribute/accessor.rb', line 50 def write_type_caster lookup_type_caster(write_type) end |