Module: Architect4r::Model::Properties

Extended by:
ActiveSupport::Concern
Defined in:
lib/architect4r/model/properties.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#_to_database_hashObject

Return a hash of all properties which can be transformed into json Remove all nil values, as those cannot be stored in the graph



18
19
20
21
# File 'lib/architect4r/model/properties.rb', line 18

def _to_database_hash
  @properties_data.merge('architect4r_type' => self.class.name).
                   reject { |key, value| value.nil? }
end

#parse_properties(properties = {}) ⇒ Object



11
12
13
14
# File 'lib/architect4r/model/properties.rb', line 11

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.



27
28
29
30
# File 'lib/architect4r/model/properties.rb', line 27

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=



67
68
69
70
71
72
73
74
75
76
# File 'lib/architect4r/model/properties.rb', line 67

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



34
35
36
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
# File 'lib/architect4r/model/properties.rb', line 34

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