Sha256: 5459ea44787fa32f59416774070db1c2e3978da046ad0e3ab88b38e47116e296

Contents?: true

Size: 980 Bytes

Versions: 2

Compression:

Stored size: 980 Bytes

Contents

module Configurable
  
  # Implements AGET and ASET methods that symbolize string keys, in effect 
  # producing indifferent access.  IndifferentAccess is intended to extend
  # a Hash.
  #
  # Note that the indifference produced by this module is very thin indeed.
  # Strings may still be used as keys through store/fetch, and
  # existing string keys are not changed in any way.  Nonetheless,
  # these methods are sufficient for Configurable and ConfigHash.
  module IndifferentAccess
    
    # Symbolizes string keys and calls super.
    def [](key)
      super(convert(key))
    end
    
    # Symbolizes string keys and calls super.
    def []=(key, value)
      super(convert(key), value)
    end
    
    # Ensures duplicates use indifferent access.
    def dup
      super().extend IndifferentAccess
    end
    
    private
    
    # a helper to convert strings to symbols
    def convert(key) # :nodoc:
      key.kind_of?(String) ? key.to_sym : key
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
configurable-0.7.0 lib/configurable/indifferent_access.rb
configurable-0.6.0 lib/configurable/indifferent_access.rb