Class: ApiResource::Associations::Scope
- Inherits:
-
Object
- Object
- ApiResource::Associations::Scope
- Defined in:
- lib/api_resource/associations.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#current_scope ⇒ Object
Returns the value of attribute current_scope.
-
#internal_object ⇒ Object
Use this method to access the internal data, this guarantees that loading only occurs once per object.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#scopes ⇒ Object
readonly
Returns the value of attribute scopes.
Instance Method Summary collapse
-
#initialize(klass, current_scope, opts) ⇒ Scope
constructor
A new instance of Scope.
- #inspect ⇒ Object
- #method_missing(method, *args, &block) ⇒ Object
- #reload ⇒ Object
- #scope?(scp) ⇒ Boolean
- #to_query ⇒ Object
- #to_s ⇒ Object
Constructor Details
#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\ndef \#{key}(opts = {})\nobj = self.clone\n# Call reload to make it go back to the webserver the next time it loads\nobj.reload\nobj.enhance_current_scope(:\#{key}, opts)\nreturn obj\nend\n", __FILE__, __LINE__ + 1 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
216 217 218 |
# File 'lib/api_resource/associations.rb', line 216 def method_missing(method, *args, &block) self.internal_object.send(method, *args, &block) end |
Instance Attribute Details
#current_scope ⇒ Object
Returns the value of attribute current_scope.
167 168 169 |
# File 'lib/api_resource/associations.rb', line 167 def current_scope @current_scope end |
#internal_object ⇒ Object
Use this method to access the internal data, this guarantees that loading only occurs once per object
196 197 198 |
# File 'lib/api_resource/associations.rb', line 196 def internal_object @internal_object end |
#klass ⇒ Object
Returns the value of attribute klass.
167 168 169 |
# File 'lib/api_resource/associations.rb', line 167 def klass @klass end |
#scopes ⇒ Object (readonly)
Returns the value of attribute scopes.
169 170 171 |
# File 'lib/api_resource/associations.rb', line 169 def scopes @scopes end |
Instance Method Details
#inspect ⇒ Object
229 230 231 |
# File 'lib/api_resource/associations.rb', line 229 def inspect self.internal_object.inspect end |
#reload ⇒ Object
220 221 222 223 |
# File 'lib/api_resource/associations.rb', line 220 def reload remove_instance_variable(:@internal_object) if instance_variable_defined?(:@internal_object) self end |
#scope?(scp) ⇒ Boolean
204 205 206 |
# File 'lib/api_resource/associations.rb', line 204 def scope?(scp) self.scopes.key?(scp.to_s) end |
#to_query ⇒ Object
212 213 214 |
# File 'lib/api_resource/associations.rb', line 212 def to_query self.scopes[self.current_scope].to_query end |
#to_s ⇒ Object
225 226 227 |
# File 'lib/api_resource/associations.rb', line 225 def to_s self.internal_object.to_s end |