Module: PropMixin

Included in:
Cart, CartItem, User
Defined in:
app/models/prop_mixin.rb

Instance Method Summary collapse

Instance Method Details

#deserialized_propertiesObject


39
40
41
42
43
# File 'app/models/prop_mixin.rb', line 39

def deserialized_properties
  properties_copy = {}
  self.properties.collect{|p| properties_copy[p.property] = YAML::load(p.value)}
  properties_copy
end

#getProp(p) ⇒ Object


29
30
31
32
33
34
35
36
37
# File 'app/models/prop_mixin.rb', line 29

def getProp(p)
  ps = Property.where(:hasproperties_id => id,:hasproperties_type => self.class.name, :property => p)
  if ps.empty?
    nil
  else
    val = ps[0].value
    YAML::load(val)
  end
end

#set_props(props) ⇒ Object


23
24
25
26
27
# File 'app/models/prop_mixin.rb', line 23

def set_props(props)
  props.each do |key, val|
    self.setProp(key, val)
  end
end

#setProp(p, v) ⇒ Object

TODO: We need the class name deduced from “self” to that this code can be reused on any :hasproperties class


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/models/prop_mixin.rb', line 5

def setProp(p,v)
  ps = Property.where(:hasproperties_id => id,:hasproperties_type => self.class.name, :property => p)
  if ps.empty?
    val_str = YAML::dump(v)
    p0 = Property.create(property: p, value: val_str)
  else
    p0 = ps[0]
    p0.value = v
    p0.save
  end

  # TODO: we want to make this a single property, and also do a
  # ruby encoding of the type someohow so we are not limiited to strings.
  # We don't actually want multiple values for a single property---
  # If we do, we will implement that ourselves in some other way.
  p0.update_attribute(:hasproperties,self)
end