Class: Daylight::ResourceProxy

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/daylight/resource_proxy.rb

Overview

Proxies requests to ActiveResource once the data has been accessed. Allows chaining of scope calls ala ActiveRecord

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(association = {}) ⇒ ResourceProxy

Returns a new instance of ResourceProxy.



16
17
18
19
# File 'lib/daylight/resource_proxy.rb', line 16

def initialize association={}
  @current_params = {}
  @association_name, @association_resource = association.first
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Will attempt to fulfill the method if it exists on the resource or if it exists on an Array. Delegates the method on for subsequent execution.



182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/daylight/resource_proxy.rb', line 182

def method_missing(method_name, *args, &block)
  if resource_class.respond_to?(method_name)
    self.class.send(:define_method, method_name) do |*method_args, &method_block|
      resource_class.send(method_name, *method_args, &method_block)
    end
    resource_class.send(method_name, *args, &block)
  elsif Array.method_defined?(method_name)
    wrap_array_method(method_name)
    # resend call to newly wrapped method
    send(method_name, *args, &block)
  else
    super
  end
end

Class Attribute Details

.resource_classObject

Each ResourceProxy will have thier own resource



137
138
139
# File 'lib/daylight/resource_proxy.rb', line 137

def resource_class
  @resource_class
end

Instance Attribute Details

#association_nameObject (readonly)

Returns the value of attribute association_name.



11
12
13
# File 'lib/daylight/resource_proxy.rb', line 11

def association_name
  @association_name
end

#association_resourceObject (readonly)

Returns the value of attribute association_resource.



11
12
13
# File 'lib/daylight/resource_proxy.rb', line 11

def association_resource
  @association_resource
end

Class Method Details

.[](resource_class) ⇒ Object

Factory method to generate a child class for ResourceProxy with the required resource class.

proxy = ResourceProxy[User]  #=> User::ResourceProxy

Onece a child class has been created, it can be use it to create instances:

ResourceProxy[User].new


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/daylight/resource_proxy.rb', line 150

def [] resource_class
  if resource_class.const_defined?(:ResourceProxy)
    return resource_class.const_get(:ResourceProxy)
  end

  Class.new(Daylight::ResourceProxy) do
    # Allow instances to be created
    public_class_method :new

    # Set our resource
    self.resource_class = resource_class

    # Set our ResourceProxy constant
    resource_class.const_set(:ResourceProxy, self)
  end
end

.nameObject Also known as: inspect

Define a name for the class



169
170
171
# File 'lib/daylight/resource_proxy.rb', line 169

def name
  "#{resource_class}::ResourceProxy"
end

Instance Method Details

#append_scope(scope) ⇒ Object

Adds scopes to the current parameters



60
61
62
63
64
65
66
# File 'lib/daylight/resource_proxy.rb', line 60

def append_scope scope
  spawn.tap do |proxy|
    proxy.current_params[:scopes] ||= []
    proxy.current_params[:scopes] << scope
    proxy.current_params[:scopes].uniq!
  end
end

#find_by(conditions) ⇒ Object

Merges conditions to the current parameters, and fetches the first result. Immediately issues the request to the API.



105
106
107
# File 'lib/daylight/resource_proxy.rb', line 105

def find_by conditions
  where(conditions).limit(1).first
end

#firstObject

If loaded, return the first element, otherwise - sets the limit to the current parameters, and fetches the first result. Immediately issues the request to the API.



114
115
116
117
118
119
120
# File 'lib/daylight/resource_proxy.rb', line 114

def first
  if loaded?
    to_a.first
  else
    limit(1).to_a.first
  end
end

#from(from) ⇒ Object

Sets ‘from` URL on a request



23
24
25
26
# File 'lib/daylight/resource_proxy.rb', line 23

def from from
  @from = from
  self
end

#inspectObject

Special inspect that shows the fetched results (up to 10 fo them) and the current params to fetch those results.

Immediately issues the request to the API.



128
129
130
131
132
133
# File 'lib/daylight/resource_proxy.rb', line 128

def inspect
  records = to_a.take(11)
  records[10] = '...' if entries.size == 11

  "#<#{self.class.name} #{records} @current_params=#{current_params}>"
end

#limit(value) ⇒ Object

Sets limit in the current parameters



79
80
81
82
83
# File 'lib/daylight/resource_proxy.rb', line 79

def limit value
  spawn.tap do |proxy|
    proxy.current_params[:limit] = value
  end
end

#loadObject

Loads records from server based on current paremeters and from URL



30
31
32
# File 'lib/daylight/resource_proxy.rb', line 30

def load
  resource_class.find(:all, params: to_params, from: @from)
end

#offset(value) ⇒ Object

Sets offset in the current parameters



95
96
97
98
99
# File 'lib/daylight/resource_proxy.rb', line 95

def offset value
  spawn.tap do |proxy|
    proxy.current_params[:offset] = value
  end
end

#order(value) ⇒ Object

Sets order in the current parameters



87
88
89
90
91
# File 'lib/daylight/resource_proxy.rb', line 87

def order value
  spawn.tap do |proxy|
    proxy.current_params[:order] = value
  end
end

#recordsObject

Returns the records, requests them from server if not fetched



42
43
44
# File 'lib/daylight/resource_proxy.rb', line 42

def records
  @records ||= load
end

#reloadObject

Returns the records, forces fetch from server



48
49
50
# File 'lib/daylight/resource_proxy.rb', line 48

def reload
  @records = load
end

#to_aObject

Converts records to an Array



54
55
56
# File 'lib/daylight/resource_proxy.rb', line 54

def to_a
  records.to_a
end

#to_paramsObject

Returns a copy of the current parameters used to fetch records



36
37
38
# File 'lib/daylight/resource_proxy.rb', line 36

def to_params
  current_params.dup
end

#where(conditions) ⇒ Object

Merges conditions to the current parameters



70
71
72
73
74
75
# File 'lib/daylight/resource_proxy.rb', line 70

def where conditions
  spawn.tap do |proxy|
    proxy.current_params[:filters] ||= {}
    proxy.current_params[:filters].merge! conditions
  end
end