Sha256: 9ae7af7a2f2ba268d677d2cc4131698e51a792bb43c1896f93d93d59fc70c9c3

Contents?: true

Size: 1006 Bytes

Versions: 47

Compression:

Stored size: 1006 Bytes

Contents

module ApiClient

  module Mixins

    module Scoping

      attr_accessor :default_scopes

      # Default scoping
      def always(&block)
        default_scopes.push(block) if block
      end

      def default_scopes
        @default_scopes || []
      end

      # Scoping
      def scope(options = {})
        scope_in_thread || Scope.new(self).params(options)
      end

      # Allow wrapping singleton methods in a scope
      # Store the handler in a thread-local variable for thread safety
      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

      def scope_thread_attribute_name
        "#{self.name}_scope"
      end

      def scope_in_thread
        if found = Thread.current[scope_thread_attribute_name]
          found.last
        end
      end

    end

  end

end

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
api_client-0.6.0-java lib/api_client/mixins/scoping.rb
api_client-0.6.0 lib/api_client/mixins/scoping.rb
api_client-0.5.26-java lib/api_client/mixins/scoping.rb
api_client-0.5.26 lib/api_client/mixins/scoping.rb
api_client-0.5.25-java lib/api_client/mixins/scoping.rb
api_client-0.5.25 lib/api_client/mixins/scoping.rb
api_client-0.5.24 lib/api_client/mixins/scoping.rb
api_client-0.5.22 lib/api_client/mixins/scoping.rb
api_client-0.5.21 lib/api_client/mixins/scoping.rb
api_client-0.5.20 lib/api_client/mixins/scoping.rb
api_client-0.5.19 lib/api_client/mixins/scoping.rb
api_client-0.5.18 lib/api_client/mixins/scoping.rb
api_client-0.5.17 lib/api_client/mixins/scoping.rb
api_client-0.5.16 lib/api_client/mixins/scoping.rb
api_client-0.5.15 lib/api_client/mixins/scoping.rb
api_client-0.5.14 lib/api_client/mixins/scoping.rb
api_client-0.5.13 lib/api_client/mixins/scoping.rb
api_client-0.5.12 lib/api_client/mixins/scoping.rb
api_client-0.5.11 lib/api_client/mixins/scoping.rb
api_client-0.5.9 lib/api_client/mixins/scoping.rb