Sha256: acf47f70a93780e8719f19cec7041bcd731b54507ec141968c315436d1505d60

Contents?: true

Size: 1.49 KB

Versions: 2

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module Invar
   # A set of configurations
   class Scope
      def initialize(data = nil)
         @data = convert(data)

         @data.freeze
         freeze
      end

      # Retrieve the value for the given key
      #
      # @param [symbol] key
      # @raise KeyError if no such key exists.
      # @see #override
      def fetch(key)
         key = key.downcase.to_sym
         @data.fetch key
      rescue KeyError => e
         raise KeyError, "#{ e.message }. Known keys are #{ known_keys }"
      end

      alias / fetch
      alias [] fetch

      def method_missing(symbol, *args)
         raise ::Invar::ImmutableRealityError, ::Invar::ImmutableRealityError::PRETEND_MSG if symbol == :pretend

         super
      end

      # Returns a hash representation of this scope and subscopes.
      #
      # @return [Hash] a hash representation of this scope
      def to_h
         @data.to_h.transform_values do |value|
            case value
            when Scope
               value.to_h
            else
               value
            end
         end
      end

      def key?(key_name)
         to_h.key?(key_name)
      end

      private

      def known_keys
         @data.keys.sort.collect { |k| ":#{ k }" }.join(', ')
      end

      def convert(data)
         (data || {}).dup.each_with_object({}) do |pair, agg|
            key, value = pair

            agg[key.to_s.downcase.to_sym] = value.is_a?(Hash) ? Scope.new(value) : value
         end
      end
   end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
invar-0.9.0 lib/invar/scope.rb
invar-0.8.0 lib/invar/scope.rb