Class: Hungry::Resource

Inherits:
Object
  • Object
show all
Extended by:
Enumerable
Includes:
HTTParty
Defined in:
lib/hungry/resource.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_attributes = {}) ⇒ Resource

INSTANCE METHODS:



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/hungry/resource.rb', line 141

def initialize(new_attributes = {})
  self.attributes = {}

  new_attributes.each do |key, value|
    if value.respond_to?(:symbolize_keys)
      value = value.symbolize_keys
    end

    attributes[key] = value

    if respond_to?("#{key}=")
      send("#{key}=", value)
    end
  end
end

Class Attribute Details

.default_criteriaObject



98
99
100
101
102
103
104
# File 'lib/hungry/resource.rb', line 98

def default_criteria
  if @default_criteria.respond_to?(:call)
    @default_criteria.call
  else
    @default_criteria || {}
  end
end

.endpointObject



94
95
96
# File 'lib/hungry/resource.rb', line 94

def endpoint
  @endpoint || (superclass.respond_to?(:endpoint) ? superclass.endpoint : nil)
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



7
8
9
# File 'lib/hungry/resource.rb', line 7

def attributes
  @attributes
end

#data_sourceObject

Returns the value of attribute data_source.



7
8
9
# File 'lib/hungry/resource.rb', line 7

def data_source
  @data_source
end

#resourcesObject

Returns the value of attribute resources.



7
8
9
# File 'lib/hungry/resource.rb', line 7

def resources
  @resources
end

Class Method Details

.all(criteria = {}) ⇒ Object



130
131
132
# File 'lib/hungry/resource.rb', line 130

def all(criteria = {})
  collection.all(criteria)
end

.belongs_to(association, klass = 'Resource') ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hungry/resource.rb', line 17

def self.belongs_to(association, klass = 'Resource')
  define_method association do
    @belongs_to ||= {}
    @belongs_to[association] ||= begin
      klass = Kernel.const_get(klass) if klass.is_a?(String)

      if attributes[association].present?
        resource = klass.new attributes[association]
        resource.data_source = data_source
        resource
      elsif url = resources[association]
        attributes = self.class.get url
        resource = klass.new attributes
        resource.data_source = url
        resource
      end
    end
  end

  define_method "#{association}=" do |object_or_attributes|
    @belongs_to ||= {}

    klass = Kernel.const_get(klass) if klass.is_a?(String)

    if object_or_attributes.is_a?(klass)
      @belongs_to[association] = object_or_attributes
    elsif object_or_attributes.present?
      self.attributes.merge!(association => object_or_attributes)
    else
      @belongs_to[association] = nil
    end

    @belongs_to[association]
  end
end

.collectionObject



106
107
108
# File 'lib/hungry/resource.rb', line 106

def collection
  Collection.new(self, endpoint, default_criteria)
end

.each(&block) ⇒ Object



134
135
136
# File 'lib/hungry/resource.rb', line 134

def each(&block)
  all.each(&block)
end

.find(id) ⇒ Object

Raises:

  • (NoEndpointSpecified)


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/hungry/resource.rb', line 110

def find(id)
  raise NoEndpointSpecified unless endpoint

  uri = "#{endpoint}/#{id}"

  Util.log "GET: #{uri}"
  response = get uri

  if response.code == 200
    attributes = Util.parse_json(response.body)
    resource = new(attributes)
    resource.data_source = uri
    resource
  end
end

.first(n = 1) ⇒ Object



126
127
128
# File 'lib/hungry/resource.rb', line 126

def first(n = 1)
  collection.first(n)
end

.get(*args) ⇒ Object

CLASS METHODS:



11
12
13
14
15
# File 'lib/hungry/resource.rb', line 11

def self.get(*args)
  self.base_uri Hungry.api_url

  super
end

.has_many(association, klass = 'Resource') ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hungry/resource.rb', line 53

def self.has_many(association, klass = 'Resource')
  define_method association do
    @has_many ||= {}
    @has_many[association] ||= begin
      klass = Kernel.const_get(klass) if klass.is_a?(String)

      if url = resources[association]
        collection = klass.collection.from_url(url)

        if attributes[association].present?
          collection.results = attributes[resource]
        end

        collection
      end
    end
  end
end

.lazy_load(*attributes) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hungry/resource.rb', line 72

def self.lazy_load(*attributes)
  attributes.each do |attribute|
    alias_method "#{attribute}_without_lazy_load".to_sym, attribute
    define_method attribute do
      result = send "#{attribute}_without_lazy_load".to_sym

      if !result.present? && (canonical_data_source != data_source && canonical_data_source.present?)
        reload
        send "#{attribute}_without_lazy_load".to_sym
      else
        result
      end
    end
  end
end

Instance Method Details

#canonical_data_sourceObject



157
158
159
# File 'lib/hungry/resource.rb', line 157

def canonical_data_source
  resources && resources[:self]
end

#reloadObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/hungry/resource.rb', line 161

def reload
  resource = self.class.find id

  if resource
    self.data_source = resource.data_source
    initialize resource.attributes
  else
    self.data_source = nil
    initialize Hash[attributes.keys.map { |key| [key, nil] }]
  end

  @has_many   = {}
  @belongs_to = {}

  self
end