Module: Yogo::Collection::Data::Model::InstanceMethods

Defined in:
lib/yogo/collection/data/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



17
18
19
20
21
22
# File 'lib/yogo/collection/data/model.rb', line 17

def self.included(base)
  base.class_eval do
    alias_method :[], :named_attribute_get
    alias_method :[]=, :named_attribute_set
  end
end

Instance Method Details

#as_json(options = nil) ⇒ Object



24
25
26
27
28
# File 'lib/yogo/collection/data/model.rb', line 24

def as_json(options=nil)
  hash = super
  hash[:id] = self.id.to_s
  hash
end

#named_attribute_get(name) ⇒ Object



30
31
32
33
34
# File 'lib/yogo/collection/data/model.rb', line 30

def named_attribute_get(name)
  name = name.to_s
  property = resolve_property(:name => name)
  property ? attribute_get(property.to_s) : attribute_get(name)
end

#named_attribute_set(name, value) ⇒ Object



36
37
38
39
40
# File 'lib/yogo/collection/data/model.rb', line 36

def named_attribute_set(name, value)
  name = name.to_s
  property = resolve_property(:name => name)
  property ? attribute_set(property.to_s, value) : attribute_set(name, value)
end

#named_attributesObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/yogo/collection/data/model.rb', line 42

def named_attributes
  attributes.inject({}) do |h,(k,v)|
    if k.to_s =~ /^field_/
      key = k.to_s.gsub(/^field_/,'').gsub('_','-')
      property = resolve_property(:id => key)
      h[property.name] = v
    end
    h
  end
end

#named_attributes=(attrs) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/yogo/collection/data/model.rb', line 53

def named_attributes=(attrs)
  attrs = attrs.inject({}) do |h,(k,v)|
    property = resolve_property(:name => k.to_s)
    if(property)
      h[property.to_s.intern] = v
    end
    h
  end
  self.attributes = attrs
end

#resolve_property(options) ⇒ Object



64
65
66
# File 'lib/yogo/collection/data/model.rb', line 64

def resolve_property(options)
  self.class.resolve_property(options)
end