Class: OldApiResource::Associations::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/old_api_resource/associations/scope.rb

Direct Known Subclasses

RelationScope, ResourceScope

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, current_scope, opts) ⇒ Scope

Returns a new instance of Scope.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/old_api_resource/associations/scope.rb', line 11

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 <<-EOE, __FILE__, __LINE__ + 1
      # This class always has at least one scope, adding a new one should clone this object
      def #{key}(opts = {})
        obj = self.clone
        # Call reload to make it go back to the webserver the next time it loads
        obj.reload
        obj.enhance_current_scope(:#{key}, opts)
        return obj
      end
    EOE
    self.scopes[key.to_s] = val
  end
  # Use the method current scope because it gives a string
  # This expression substitutes the options from opts into the default attributes of the scope, it will only copy keys that exist in the original
  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}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



56
57
58
# File 'lib/old_api_resource/associations/scope.rb', line 56

def method_missing(method, *args, &block)
  self.internal_object.send(method, *args, &block)
end

Instance Attribute Details

#current_scopeObject

Returns the value of attribute current_scope.



7
8
9
# File 'lib/old_api_resource/associations/scope.rb', line 7

def current_scope
  @current_scope
end

#internal_objectObject

Use this method to access the internal data, this guarantees that loading only occurs once per object



36
37
38
# File 'lib/old_api_resource/associations/scope.rb', line 36

def internal_object
  @internal_object
end

#klassObject

Returns the value of attribute klass.



7
8
9
# File 'lib/old_api_resource/associations/scope.rb', line 7

def klass
  @klass
end

#scopesObject (readonly)

Returns the value of attribute scopes.



9
10
11
# File 'lib/old_api_resource/associations/scope.rb', line 9

def scopes
  @scopes
end

Instance Method Details

#inspectObject



69
70
71
# File 'lib/old_api_resource/associations/scope.rb', line 69

def inspect
  self.internal_object.inspect
end

#reloadObject



60
61
62
63
# File 'lib/old_api_resource/associations/scope.rb', line 60

def reload
  remove_instance_variable(:@internal_object) if instance_variable_defined?(:@internal_object)
  self
end

#scope?(scp) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/old_api_resource/associations/scope.rb', line 44

def scope?(scp)
  self.scopes.key?(scp.to_s)
end

#to_queryObject



52
53
54
# File 'lib/old_api_resource/associations/scope.rb', line 52

def to_query
  self.scopes[self.current_scope].to_query
end

#to_sObject



65
66
67
# File 'lib/old_api_resource/associations/scope.rb', line 65

def to_s
  self.internal_object.to_s
end