Sha256: ee76827b9d2cee938f1c732a758ba0c3b242e5fa0e4a03219feafce32f6885b3

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

require 'active_support/per_thread_registry'
require 'influxer/metrics/scoping/default'
require 'influxer/metrics/scoping/named'

module Influxer
  module Scoping # :nodoc:
    extend ActiveSupport::Concern

    class Error < StandardError; end

    included do
      include Default
      include Named
    end

    module ClassMethods # :nodoc:
      def current_scope
        ScopeRegistry.value_for(:current_scope, name)
      end

      def current_scope=(scope)
        ScopeRegistry.set_value_for(:current_scope, name, scope)
      end
    end

    class ScopeRegistry # :nodoc:
      extend ActiveSupport::PerThreadRegistry

      VALID_SCOPE_TYPES = [:current_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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
influxer-0.2.4 lib/influxer/metrics/scoping.rb