Method: ApiResource::Associations::Scope#initialize

Defined in:
lib/api_resource/associations.rb

#initialize(klass, current_scope, opts) ⇒ Scope

Returns a new instance of Scope.



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/api_resource/associations.rb', line 171

def initialize(klass, current_scope, opts)
  # Holds onto the association proxy this RelationScope is bound to
  @klass = klass
  @current_scope = Array.wrap(current_scope.to_s)
  # define methods for the scopes of the object

  klass.scopes.each do |key, val|
    self.instance_eval "      # This class always has at least one scope, adding a new one should clone this object\n      def \#{key}(opts = {})\n        obj = self.clone\n        # Call reload to make it go back to the webserver the next time it loads\n        obj.reload\n        obj.enhance_current_scope(:\#{key}, opts)\n        return obj\n      end\n    EOE\n    self.scopes[key.to_s] = val\n  end\n  # Use the method current scope because it gives a string\n  # This expression substitutes the options from opts into the default attributes of the scope, it will only copy keys that exist in the original\n  self.scopes[self.current_scope] = opts.inject(self.scopes[current_scope]){|accum,(k,v)| accum.key?(k.to_s) ? accum.merge(k.to_s => v) : accum}\nend\n", __FILE__, __LINE__ + 1