Sha256: f29631e3d2ef83fe7bf323221e673e9234017d60e6c10349145eafb2e47ed42f

Contents?: true

Size: 982 Bytes

Versions: 5

Compression:

Stored size: 982 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 DelegateHash.
  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

5 entries across 5 versions & 1 rubygems

Version Path
configurable-0.3.0 lib/configurable/indifferent_access.rb
configurable-0.4.1 lib/configurable/indifferent_access.rb
configurable-0.4.0 lib/configurable/indifferent_access.rb
configurable-0.4.2 lib/configurable/indifferent_access.rb
configurable-0.5.0 lib/configurable/indifferent_access.rb