Module: TinyDyno::Attributes
- Extended by:
- ActiveSupport::Concern
- Includes:
- Readonly
- Included in:
- DocumentComposition
- Defined in:
- lib/tiny_dyno/attributes.rb,
lib/tiny_dyno/attributes/readonly.rb
Defined Under Namespace
Modules: Readonly
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Instance Method Summary collapse
-
#attribute_missing?(name) ⇒ true, false
Determine if the attribute is missing from the document, due to loading it from the database with missing fields.
-
#process_attribute(name, value) ⇒ Object
If the attribute is dynamic, add a field for it with a type of object and then either way set the value.
-
#process_attributes(attrs = nil) ⇒ Object
Process the provided attributes casting them to their proper values if a field exists for them on the document.
-
#read_attribute(name) ⇒ Object
(also: #[])
Read a value from the document attributes.
-
#typed_value_for(key, value) ⇒ Object
Return the typecasted value for a field.
-
#write_attribute(name, value) ⇒ Object
(also: #[]=)
Write a single attribute to the document attribute hash.
Methods included from Readonly
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
11 12 13 |
# File 'lib/tiny_dyno/attributes.rb', line 11 def attributes @attributes end |
Instance Method Details
#attribute_missing?(name) ⇒ true, false
Determine if the attribute is missing from the document, due to loading it from the database with missing fields.
132 133 134 |
# File 'lib/tiny_dyno/attributes.rb', line 132 def attribute_missing?(name) return (!self.fields.keys.include?(name)) end |
#process_attribute(name, value) ⇒ Object
If the attribute is dynamic, add a field for it with a type of object and then either way set the value.
97 98 99 100 101 |
# File 'lib/tiny_dyno/attributes.rb', line 97 def process_attribute(name, value) responds = respond_to?("#{name}=", true) raise TinyDyno::Errors::UnknownAttribute.new(self.class, name) unless responds send("#{name}=", value) end |
#process_attributes(attrs = nil) ⇒ Object
Process the provided attributes casting them to their proper values if a field exists for them on the document. This will be limited to only the attributes provided in the suppied Hash
so that no extra nil values get put into the document’s attributes.
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/tiny_dyno/attributes.rb', line 76 def process_attributes(attrs = nil) attrs ||= {} if !attrs.empty? attrs.each_pair do |key, value| process_attribute(key, value) end end # context? # yield self if block_given? end |
#read_attribute(name) ⇒ Object Also known as: []
Read a value from the document attributes. If the value does not exist it will return nil.
27 28 29 30 31 32 33 |
# File 'lib/tiny_dyno/attributes.rb', line 27 def read_attribute(name) normalized = database_field_name(name.to_s) if attribute_missing?(normalized) raise ActiveModel::MissingAttributeError, "Missing attribute: '#{name}'." end attributes[normalized] end |
#typed_value_for(key, value) ⇒ Object
Return the typecasted value for a field. Based on the field option, type which is mandatory
116 117 118 119 |
# File 'lib/tiny_dyno/attributes.rb', line 116 def typed_value_for(key, value) raise MissingAttributeError if fields[key].nil? TinyDyno::Adapter.simple_attribute(field_type: self.fields[key].[:type], value: value) end |
#write_attribute(name, value) ⇒ Object Also known as: []=
Write a single attribute to the document attribute hash. This will also fire the before and after update callbacks, and perform any necessary typecasting. called from within ActiveModel
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/tiny_dyno/attributes.rb', line 52 def write_attribute(name, value) access = database_field_name(name.to_s) typed_value = typed_value_for(access, value) if attribute_writable?(access) unless attributes[access] == typed_value|| attribute_changed?(access) attribute_will_change!(access) end attributes[access] = typed_value typed_value end end |