Sha256: 4ba12c6640e9c289b5eaa92c8c08d2190c0e0b4de2ce934d4c0f48bb7075f2de

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

module Elasticsearch
  module Persistence
    module Scoping
      extend ActiveSupport::Concern

      included do
        include Default
        include Named
      end

      module ClassMethods
        def current_scope
           ScopeRegistry.value_for(:current_scope, base_class.to_s)
        end

        def current_scope=(scope) #:nodoc:
          ScopeRegistry.set_value_for(:current_scope, base_class.to_s, scope)
        end
      end

      class ScopeRegistry # :nodoc:
        extend ActiveSupport::PerThreadRegistry

        VALID_SCOPE_TYPES = [:current_scope, :ignore_default_scope]

        def initialize
          @registry = Hash.new { |hash, key| hash[key] = {} }
        end

        # Obtains the value for a given +scope_name+ and +variable_name+.
        def value_for(scope_type, variable_name)
          raise_invalid_scope_type!(scope_type)
          @registry[scope_type][variable_name]
        end

        # Sets the +value+ for a given +scope_type+ and +variable_name+.
        def set_value_for(scope_type, variable_name, value)
          raise_invalid_scope_type!(scope_type)
          @registry[scope_type][variable_name] = value
        end

        private

        def raise_invalid_scope_type!(scope_type)
          if !VALID_SCOPE_TYPES.include?(scope_type)
            raise ArgumentError, "Invalid scope type '#{scope_type}' sent to the registry. Scope types must be included in VALID_SCOPE_TYPES"
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
elasticsearch-persistence-queryable-0.1.9 lib/elasticsearch/persistence/scoping.rb
elasticsearch-persistence-queryable-0.1.8 lib/elasticsearch/persistence/scoping.rb