Class: LazyResource::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/lazy_resource/relation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, options = {}) ⇒ Relation

Returns a new instance of Relation.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/lazy_resource/relation.rb', line 15

def initialize(klass, options = {})
  @klass = klass
  @route = options.fetch(:where_values, {}).delete(:_route)
  @values = options.slice(:where_values, :order_value, :limit_value, :offset_value, :page_value)
  @fetched = options[:fetched] || false
  @method = options[:method]

  unless fetched?
    resource_queue.queue(self)
  end

  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/lazy_resource/relation.rb', line 147

def method_missing(name, *args, &block)
  if result.respond_to?(name)
    self.to_a.send(name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#fetchedObject

Returns the value of attribute fetched.



11
12
13
# File 'lib/lazy_resource/relation.rb', line 11

def fetched
  @fetched
end

#fromObject

Returns the value of attribute from.



11
12
13
# File 'lib/lazy_resource/relation.rb', line 11

def from
  @from
end

#klassObject

Returns the value of attribute klass.



11
12
13
# File 'lib/lazy_resource/relation.rb', line 11

def klass
  @klass
end

#methodObject



37
38
39
# File 'lib/lazy_resource/relation.rb', line 37

def method
  @method ? @method.downcase.to_sym : nil
end

#other_attributesObject

Returns the value of attribute other_attributes.



11
12
13
# File 'lib/lazy_resource/relation.rb', line 11

def other_attributes
  @other_attributes
end

#request_errorObject

Returns the value of attribute request_error.



11
12
13
# File 'lib/lazy_resource/relation.rb', line 11

def request_error
  @request_error
end

#routeObject (readonly)

Returns the value of attribute route.



13
14
15
# File 'lib/lazy_resource/relation.rb', line 13

def route
  @route
end

#siteObject

Returns the value of attribute site.



11
12
13
# File 'lib/lazy_resource/relation.rb', line 11

def site
  @site
end

#valuesObject

Returns the value of attribute values.



11
12
13
# File 'lib/lazy_resource/relation.rb', line 11

def values
  @values
end

Class Method Details

.resource_queueObject



6
7
8
# File 'lib/lazy_resource/relation.rb', line 6

def resource_queue
  Thread.current[:resource_queue] ||= ResourceQueue.new
end

Instance Method Details

#as_json(options = {}) ⇒ Object



141
142
143
144
145
# File 'lib/lazy_resource/relation.rb', line 141

def as_json(options = {})
  to_a.map do |record|
    record.as_json
  end
end

#collection_nameObject



33
34
35
# File 'lib/lazy_resource/relation.rb', line 33

def collection_name
  from
end

#fetched?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/lazy_resource/relation.rb', line 123

def fetched?
  @fetched
end

#headersObject



65
66
67
# File 'lib/lazy_resource/relation.rb', line 65

def headers
  @headers ||= @klass.default_headers
end

#limit(limit_value) ⇒ Object



88
89
90
91
# File 'lib/lazy_resource/relation.rb', line 88

def limit(limit_value)
  @values[:limit_value] = limit_value
  self
end

#limit_valueObject



111
112
113
# File 'lib/lazy_resource/relation.rb', line 111

def limit_value
  @values[:limit_value]
end

#load(objects) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lazy_resource/relation.rb', line 51

def load(objects)
  @fetched = true

  if mapped_name = @klass.mapped_root_node_name(objects)
    other_attributes = objects.dup
    objects = other_attributes.delete(mapped_name)
    @other_attributes = other_attributes
  end

  @result = objects.map do |object|
    @klass.load(object)
  end
end

#offset(offset_value) ⇒ Object



93
94
95
96
# File 'lib/lazy_resource/relation.rb', line 93

def offset(offset_value)
  @values[:offset_value] = offset_value
  self
end

#offset_valueObject



115
116
117
# File 'lib/lazy_resource/relation.rb', line 115

def offset_value
  @values[:offset_value]
end

#order(order_value) ⇒ Object



83
84
85
86
# File 'lib/lazy_resource/relation.rb', line 83

def order(order_value)
  @values[:order_value] = order_value
  self
end

#order_valueObject



107
108
109
# File 'lib/lazy_resource/relation.rb', line 107

def order_value
  @values[:order_value] 
end

#page(page_value) ⇒ Object



98
99
100
101
# File 'lib/lazy_resource/relation.rb', line 98

def page(page_value)
  @values[:page_value] = page_value
  self
end

#page_valueObject



119
120
121
# File 'lib/lazy_resource/relation.rb', line 119

def page_value
  @values[:page_value]
end

#resource_queueObject



69
70
71
# File 'lib/lazy_resource/relation.rb', line 69

def resource_queue
  self.class.resource_queue
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/lazy_resource/relation.rb', line 137

def respond_to?(method, include_private = false)
  super || result.respond_to?(method, include_private)
end

#resultObject



133
134
135
# File 'lib/lazy_resource/relation.rb', line 133

def result
  @result ||= []
end

#to_aObject



127
128
129
130
131
# File 'lib/lazy_resource/relation.rb', line 127

def to_a
  resource_queue.run if !fetched?
  raise self.request_error if self.request_error.present?
  result
end

#to_paramsObject



41
42
43
44
45
46
47
48
49
# File 'lib/lazy_resource/relation.rb', line 41

def to_params
  params = {}
  params.merge!(where_values) unless where_values.nil?
  params.merge!(:order => order_value) unless order_value.nil?
  params.merge!(:limit => limit_value) unless limit_value.nil?
  params.merge!(:offset => offset_value) unless offset_value.nil?
  params.merge!(:page => page_value) unless page_value.nil?
  params
end

#where(where_values) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/lazy_resource/relation.rb', line 73

def where(where_values)
  if @values[:where_values].nil?
    @values[:where_values] = where_values
  else
    @values[:where_values].merge!(where_values)
  end

  self
end

#where_valuesObject



103
104
105
# File 'lib/lazy_resource/relation.rb', line 103

def where_values
  @values[:where_values]
end