Module: ApiClient::Mixins::Scoping

Included in:
Base
Defined in:
lib/api_client/mixins/scoping.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_scopesObject

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(options = {})
  scope_in_thread || Scope.new(self).params(options)
end

#scope_in_threadObject



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_nameObject



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