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

#service

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RestfullyAccessible

admin=, api_version, api_version=, as_member, client, create, destroy, extended, find_by_name, get, get!, list, resources_path, resources_path=, return_single_on_querying, return_single_on_querying=, update

Constructor Details

#initialize(data_via_json) ⇒ Base

Returns a new instance of Base.



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

def initialize(data_via_json)
  @data = data_via_json
end

Class Method Details

.friendly_attributes(*names) ⇒ Object



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

def self.friendly_attributes(*names)
  names.map(&:to_s).each do |name|
    define_method(name) do
      if !@data.key?(name) && id
        @data = self.class.get(id).to_hash
      end
      self[name]
    end
  end
end

.map_attribute_to_attribute(k_and_v) ⇒ Object



17
18
19
20
21
22
# File 'lib/yao/resources/base.rb', line 17

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



24
25
26
27
28
29
30
# File 'lib/yao/resources/base.rb', line 24

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



32
33
34
35
36
37
38
39
40
# File 'lib/yao/resources/base.rb', line 32

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



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

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

#[]=(name, value) ⇒ Object



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

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

#createdObject



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

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

#idObject



58
59
60
# File 'lib/yao/resources/base.rb', line 58

def id
  self["id"]
end

#to_hashObject



54
55
56
# File 'lib/yao/resources/base.rb', line 54

def to_hash
  @data
end

#updatedObject



68
69
70
71
72
# File 'lib/yao/resources/base.rb', line 68

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