Module: Architect4r::Model::Properties::InstanceMethods
- Defined in:
- lib/architect4r/model/properties.rb
Instance Method Summary collapse
-
#_to_database_hash ⇒ Object
Return a hash of all properties which can be transformed into json Remove all nil values, as those cannot be stored in the graph.
- #parse_properties(properties = {}) ⇒ Object
-
#read_attribute(property, locale = nil) ⇒ Object
Read the casted value of an attribute defined with a property.
- #update_attributes_without_saving(hash) ⇒ Object (also: #attributes=)
-
#write_attribute(property, value, locale = nil) ⇒ Object
Store a casted value in the current instance of an attribute defined with a property and update dirty status.
Instance Method Details
#_to_database_hash ⇒ Object
Return a hash of all properties which can be transformed into json Remove all nil values, as those cannot be stored in the graph
21 22 23 24 |
# File 'lib/architect4r/model/properties.rb', line 21 def _to_database_hash @properties_data.merge('architect4r_type' => self.class.name). reject { |key, value| value.nil? } end |
#parse_properties(properties = {}) ⇒ Object
14 15 16 17 |
# File 'lib/architect4r/model/properties.rb', line 14 def parse_properties(properties = {}) @properties_data = {} update_attributes_without_saving(properties) end |
#read_attribute(property, locale = nil) ⇒ Object
Read the casted value of an attribute defined with a property.
Returns
- Object
-
the casted attibutes value.
30 31 32 33 |
# File 'lib/architect4r/model/properties.rb', line 30 def read_attribute(property, locale = nil) property = "#{property}_#{locale}" if locale @properties_data[property.to_s] end |
#update_attributes_without_saving(hash) ⇒ Object Also known as: attributes=
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/architect4r/model/properties.rb', line 70 def update_attributes_without_saving(hash) return if hash.nil? hash.each do |key, value| if self.respond_to?("#{key}=") self.send("#{key}=", value) else @properties_data[key] = value end end end |
#write_attribute(property, value, locale = nil) ⇒ Object
Store a casted value in the current instance of an attribute defined with a property and update dirty status
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/architect4r/model/properties.rb', line 37 def write_attribute(property, value, locale = nil) # retrieve options for the attribute opts = self.class.properties[property.to_s] # Check if we should store a localized version of the data property = "#{property}_#{locale}" if locale # TODO: Mark dirty attribute tracking # Cast the value before storing it cast_to = opts && opts[:cast_to] || Object @properties_data[property.to_s] = if value.nil? nil elsif cast_to == String value.to_s elsif cast_to == Integer value.to_i elsif cast_to == Float value.to_f elsif cast_to == DateTime value.to_datetime elsif cast_to == TrueClass or cast_to == FalseClass if value.kind_of?(Integer) value == 1 else %w[ true 1 t ].include?(value.to_s.downcase) end else value end end |