Module: ApiClient::Mixins::Scoping
- Included in:
- Base
- Defined in:
- lib/api_client/mixins/scoping.rb
Instance Attribute Summary collapse
-
#default_scopes ⇒ Object
Returns the value of attribute default_scopes.
Instance Method Summary collapse
-
#always(&block) ⇒ Object
Default scoping.
-
#scope(options = {}) ⇒ Object
Scoping.
- #scope_in_thread ⇒ Object
- #scope_thread_attribute_name ⇒ Object
-
#scoped(scope) ⇒ Object
Allow wrapping singleton methods in a scope Store the handler in a thread-local variable for thread safety.
Instance Attribute Details
#default_scopes ⇒ Object
Returns the value of attribute default_scopes.
7 8 9 |
# File 'lib/api_client/mixins/scoping.rb', line 7 def default_scopes @default_scopes end |
Instance Method Details
#always(&block) ⇒ Object
Default scoping
10 11 12 |
# File 'lib/api_client/mixins/scoping.rb', line 10 def always(&block) default_scopes.push(block) if block end |
#scope(options = {}) ⇒ Object
Scoping
19 20 21 |
# File 'lib/api_client/mixins/scoping.rb', line 19 def scope( = {}) scope_in_thread || Scope.new(self).params() end |
#scope_in_thread ⇒ Object
39 40 41 42 43 |
# File 'lib/api_client/mixins/scoping.rb', line 39 def scope_in_thread if found = Thread.current[scope_thread_attribute_name] found.last end end |
#scope_thread_attribute_name ⇒ Object
35 36 37 |
# File 'lib/api_client/mixins/scoping.rb', line 35 def scope_thread_attribute_name "#{self.name}_scope" end |
#scoped(scope) ⇒ Object
Allow wrapping singleton methods in a scope Store the handler in a thread-local variable for thread safety
25 26 27 28 29 30 31 32 33 |
# File 'lib/api_client/mixins/scoping.rb', line 25 def scoped(scope) Thread.current[scope_thread_attribute_name] ||= [] Thread.current[scope_thread_attribute_name].push scope begin yield ensure Thread.current[scope_thread_attribute_name] = nil end end |