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: ClassMethods, 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.
12 13 14 |
# File 'lib/tiny_dyno/attributes.rb', line 12 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.
135 136 137 |
# File 'lib/tiny_dyno/attributes.rb', line 135 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.
98 99 100 101 102 |
# File 'lib/tiny_dyno/attributes.rb', line 98 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.
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/tiny_dyno/attributes.rb', line 77 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.
28 29 30 31 32 33 34 |
# File 'lib/tiny_dyno/attributes.rb', line 28 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
117 118 119 120 121 122 |
# File 'lib/tiny_dyno/attributes.rb', line 117 def typed_value_for(key, value) # raise MissingAttributeError if fields[key].nil? and hash_keys.find_index { |a| a[:attr] == key }.nil? raise MissingAttributeError if fields[key].nil? typed_class = self.fields[key].[:type] return (self.class.document_typed(klass: typed_class, 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
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/tiny_dyno/attributes.rb', line 53 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 |