Module: ActiveData::Model::Attributable
- Extended by:
- ActiveSupport::Concern
- Includes:
- Serializable
- Defined in:
- lib/active_data/model/attributable.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #attribute_names ⇒ Object
- #attributes ⇒ Object
- #attributes=(attributes) ⇒ Object
- #present_attributes ⇒ Object
- #read_attribute(name) ⇒ Object (also: #[])
- #read_attribute_before_type_cast(name) ⇒ Object
- #update_attributes(attributes) ⇒ Object
- #write_attribute(name, value) ⇒ Object (also: #[]=)
Methods included from Serializable
Instance Method Details
#attribute_names ⇒ Object
95 96 97 |
# File 'lib/active_data/model/attributable.rb', line 95 def attribute_names @attributes.keys end |
#attributes ⇒ Object
84 85 86 |
# File 'lib/active_data/model/attributable.rb', line 84 def attributes Hash[attribute_names.map { |name| [name, send(name)] }] end |
#attributes=(attributes) ⇒ Object
99 100 101 |
# File 'lib/active_data/model/attributable.rb', line 99 def attributes= attributes assign_attributes(attributes) end |
#present_attributes ⇒ Object
88 89 90 91 92 93 |
# File 'lib/active_data/model/attributable.rb', line 88 def present_attributes Hash[attribute_names.map do |name| value = send(name) [name, value] if value.present? end] end |
#read_attribute(name) ⇒ Object Also known as: []
69 70 71 |
# File 'lib/active_data/model/attributable.rb', line 69 def read_attribute name @attributes[name].nil? ? attribute_default(name) : @attributes[name] end |
#read_attribute_before_type_cast(name) ⇒ Object
74 75 76 |
# File 'lib/active_data/model/attributable.rb', line 74 def read_attribute_before_type_cast name deserialize(send(name)) end |
#update_attributes(attributes) ⇒ Object
103 104 105 |
# File 'lib/active_data/model/attributable.rb', line 103 def update_attributes attributes self.attributes = attributes end |
#write_attribute(name, value) ⇒ Object Also known as: []=
78 79 80 81 |
# File 'lib/active_data/model/attributable.rb', line 78 def write_attribute name, value type = self.class._attributes[name][:type] @attributes[name] = serialize(value, type) end |