123456789_123456789_123456789_123456789_123456789_

Class: Sinatra::IndifferentHash

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Hash
Instance Chain:
self, Hash
Inherits: Hash
  • Object
Defined in: lib/sinatra/indifferent_hash.rb

Overview

A poor man's ActiveSupport::HashWithIndifferentAccess, with all the Rails-y stuff removed.

Implements a hash where keys :foo and "foo" are considered to be the same.

rgb = Sinatra::IndifferentHash.new

rgb[:black] = '#000000' # symbol assignment rgb[:black] # => '#000000' # symbol retrieval rgb['black'] # => '#000000' # string retrieval

rgb['white'] = '#FFFFFF' # string assignment rgb[:white] # => '#FFFFFF' # symbol retrieval rgb['white'] # => '#FFFFFF' # string retrieval

Internally, symbols are mapped to strings when used as keys in the entire writing interface (calling e.g. []=, merge). This mapping belongs to the public interface. For example, given:

hash = Sinatra::IndifferentHash.new hash[:a] = 1

You are guaranteed that the key is returned as a string:

hash.keys # => ["a"]

Technically other types of keys are accepted:

hash = Sinatra::IndifferentHash hash[:a] = 1 hash[0] = 0 hash # => { "a"=>1, 0=>0 }

But this class is intended for use cases where strings or symbols are the expected keys and it is convenient to understand both as the same. For example the params hash in ::Sinatra.

Class Method Summary

Instance Method Summary

Class Method Details

.[](*args)

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 42

def self.[](*args)
  new.merge!(Hash[*args])
end

Instance Method Details

#[](key)

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 70

def [](key)
  super(convert_key(key))
end

#[]=(key, value) Also known as: #store

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 74

def []=(key, value)
  super(convert_key(key), convert_value(value))
end

#assoc(key)

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 56

def assoc(key)
  super(convert_key(key))
end

#compact

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 181

def compact
  dup.tap(&:compact!)
end

#convert_key(key) (private)

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 193

def convert_key(key)
  key.is_a?(Symbol) ? key.to_s : key
end

#convert_value(value) (private)

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 197

def convert_value(value)
  case value
  when Hash
    value.is_a?(self.class) ? value : self.class[value]
  when Array
    value.map(&method(:convert_value))
  else
    value
  end
end

#default(*args)

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 46

def default(*args)
  args.map!(&method(:convert_key))

  super(*args)
end

#default=(value)

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 52

def default=(value)
  super(convert_value(value))
end

#delete(key)

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 98

def delete(key)
  super(convert_key(key))
end

#dig(key, *other_keys)

Added in Ruby 2.3

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 103

def dig(key, *other_keys)
  super(convert_key(key), *other_keys)
end

#except(*keys)

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 185

def except(*keys)
  keys.map!(&method(:convert_key))

  self.class[super(*keys)]
end

#fetch(key, *args)

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 64

def fetch(key, *args)
  args.map!(&method(:convert_value))

  super(convert_key(key), *args)
end

#fetch_values(*keys)

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 107

def fetch_values(*keys)
  keys.map!(&method(:convert_key))

  super(*keys)
end

#has_key?(key)

Alias for #key?.

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 88

alias has_key? key?

#has_value?(value)

Alias for #value?.

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 96

alias has_value? value?

#include?(key)

Alias for #key?.

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 89

alias include? key?

#key(value)

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 80

def key(value)
  super(convert_value(value))
end

#key?(key) ⇒ Boolean Also known as: #has_key?, #include?, #member?

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 84

def key?(key)
  super(convert_key(key))
end

#member?(key)

Alias for #key?.

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 90

alias member? key?

#merge(*other_hashes, &block)

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 143

def merge(*other_hashes, &block)
  dup.merge!(*other_hashes, &block)
end

#merge!(*other_hashes) Also known as: #update

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 125

def merge!(*other_hashes)
  other_hashes.each do |other_hash|
    if other_hash.is_a?(self.class)
      super(other_hash)
    else
      other_hash.each_pair do |key, value|
        key = convert_key(key)
        value = yield(key, self[key], value) if block_given? && key?(key)
        self[key] = convert_value(value)
      end
    end
  end

  self
end

#rassoc(value)

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 60

def rassoc(value)
  super(convert_value(value))
end

#reject(*args, &block)

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 175

def reject(*args, &block)
  return to_enum(:reject) unless block_given?

  dup.tap { |hash| hash.reject!(*args, &block) }
end

#replace(other_hash)

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 147

def replace(other_hash)
  super(other_hash.is_a?(self.class) ? other_hash : self.class[other_hash])
end

#select(*args, &block)

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 169

def select(*args, &block)
  return to_enum(:select) unless block_given?

  dup.tap { |hash| hash.select!(*args, &block) }
end

#slice(*keys)

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 113

def slice(*keys)
  keys.map!(&method(:convert_key))

  self.class[super(*keys)]
end

#store(key, value)

Alias for #[]=.

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 78

alias store []=

#transform_keys(&block)

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 160

def transform_keys(&block)
  dup.transform_keys!(&block)
end

#transform_keys!

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 164

def transform_keys!
  super
  super(&method(:convert_key))
end

#transform_values(&block)

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 151

def transform_values(&block)
  dup.transform_values!(&block)
end

#transform_values!

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 155

def transform_values!
  super
  super(&method(:convert_value))
end

#update(*other_hashes)

Alias for #merge!.

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 141

alias update merge!

#value?(value) ⇒ Boolean Also known as: #has_value?

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 92

def value?(value)
  super(convert_value(value))
end

#values_at(*keys)

[ GitHub ]

  
# File 'lib/sinatra/indifferent_hash.rb', line 119

def values_at(*keys)
  keys.map!(&method(:convert_key))

  super(*keys)
end