Module: ActiveData::Model::Attributable
- Extended by:
- ActiveSupport::Concern
- 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 (also: #update_attributes)
- #has_attribute?(name) ⇒ Boolean
- #present_attributes ⇒ Object
- #read_attribute(name) ⇒ Object (also: #[])
- #read_attribute_before_type_cast(name) ⇒ Object
- #reverse_update_attributes(attributes) ⇒ Object
- #write_attribute(name, value) ⇒ Object (also: #[]=)
- #write_attributes(attributes) ⇒ Object
Instance Method Details
#attribute_names ⇒ Object
85 86 87 |
# File 'lib/active_data/model/attributable.rb', line 85 def attribute_names @attributes.keys end |
#attributes ⇒ Object
74 75 76 |
# File 'lib/active_data/model/attributable.rb', line 74 def attributes Hash[attribute_names.map { |name| [name, send(name)] }] end |
#attributes=(attributes) ⇒ Object Also known as: update_attributes
89 90 91 |
# File 'lib/active_data/model/attributable.rb', line 89 def attributes= attributes assign_attributes(attributes) end |
#has_attribute?(name) ⇒ Boolean
59 60 61 |
# File 'lib/active_data/model/attributable.rb', line 59 def has_attribute? name @attributes.key? name.to_sym end |
#present_attributes ⇒ Object
78 79 80 81 82 83 |
# File 'lib/active_data/model/attributable.rb', line 78 def present_attributes Hash[attribute_names.map do |name| value = send(name) [name, value] unless value.respond_to?(:empty?) ? value.empty? : value.nil? end.compact] end |
#read_attribute(name) ⇒ Object Also known as: []
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/active_data/model/attributable.rb', line 46 def read_attribute name name = name.to_sym if attributes_cache.key? name attributes_cache[name] else attribute = self.class._attributes[name] value = attribute.type_cast @attributes[name] use_default = attribute.default_blank? && value.respond_to?(:empty?) ? value.empty? : value.nil? attributes_cache[name] = use_default ? attribute.default_value(self) : value end end |
#read_attribute_before_type_cast(name) ⇒ Object
63 64 65 |
# File 'lib/active_data/model/attributable.rb', line 63 def read_attribute_before_type_cast name @attributes[name.to_sym] end |
#reverse_update_attributes(attributes) ⇒ Object
98 99 100 |
# File 'lib/active_data/model/attributable.rb', line 98 def reverse_update_attributes attributes reverse_assign_attributes(attributes) end |
#write_attribute(name, value) ⇒ Object Also known as: []=
67 68 69 70 71 |
# File 'lib/active_data/model/attributable.rb', line 67 def write_attribute name, value name = name.to_sym attributes_cache.delete name @attributes[name] = value end |
#write_attributes(attributes) ⇒ Object
94 95 96 |
# File 'lib/active_data/model/attributable.rb', line 94 def write_attributes attributes attributes.each { |(name, value)| send("#{name}=", value) } end |