Class: Yao::Resources::Base

Inherits:
Object
  • Object
show all
Extended by:
RestfullyAccessible
Defined in:
lib/yao/resources/base.rb

Instance Attribute Summary

Attributes included from RestfullyAccessible

#api_version, #service

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RestfullyAccessible

admin=, as_member, client, create, destroy, extended, get, list, list_detail, resources_path, resources_path=, return_single_on_querying=, update, with_resources_path

Constructor Details

#initialize(data_via_json) ⇒ Base

Returns a new instance of Base.



39
40
41
# File 'lib/yao/resources/base.rb', line 39

def initialize(data_via_json)
  @data = data_via_json
end

Class Method Details

.friendly_attributes(*names) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/yao/resources/base.rb', line 6

def self.friendly_attributes(*names)
  names.map(&:to_s).each do |name|
    define_method(name) do
      self[name]
    end
  end
end

.map_attribute_to_attribute(k_and_v) ⇒ Object



14
15
16
17
18
19
# File 'lib/yao/resources/base.rb', line 14

def self.map_attribute_to_attribute(k_and_v)
  as_json, as_method = *k_and_v.to_a.first.map(&:to_s)
  define_method(as_method) do
    self[as_json]
  end
end

.map_attribute_to_resource(k_and_v) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/yao/resources/base.rb', line 21

def self.map_attribute_to_resource(k_and_v)
  _name, klass = *k_and_v.to_a.first
  name = _name.to_s
  define_method(name) do
    self[[name, klass].join("__")] ||= klass.new(self[name])
  end
end

.map_attribute_to_resources(k_and_v) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/yao/resources/base.rb', line 29

def self.map_attribute_to_resources(k_and_v)
  _name, klass = *k_and_v.to_a.first
  name = _name.to_s
  define_method(name) do
    self[[name, klass].join("__")] ||= self[name].map {|d|
      klass.new(d)
    }
  end
end

Instance Method Details

#[](name) ⇒ Object



43
44
45
# File 'lib/yao/resources/base.rb', line 43

def [](name)
  @data[name]
end

#[]=(name, value) ⇒ Object



47
48
49
# File 'lib/yao/resources/base.rb', line 47

def []=(name, value)
  @data[name] = value
end

#createdObject



55
56
57
58
59
# File 'lib/yao/resources/base.rb', line 55

def created
  if date = self["created"]
    Time.parse(date)
  end
end

#idObject



51
52
53
# File 'lib/yao/resources/base.rb', line 51

def id
  self["id"]
end

#updatedObject



61
62
63
64
65
# File 'lib/yao/resources/base.rb', line 61

def updated
  if date = self["updated"]
    Time.parse(date)
  end
end