Class: Apiculture::IndifferentHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/apiculture/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(: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.new(: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 collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ IndifferentHash

Returns a new instance of IndifferentHash.



43
44
45
# File 'lib/apiculture/indifferent_hash.rb', line 43

def initialize(*args)
  super(*args.map(&method(:convert_value)))
end

Class Method Details

.[](*args) ⇒ Object



39
40
41
# File 'lib/apiculture/indifferent_hash.rb', line 39

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

Instance Method Details

#[](key) ⇒ Object



67
68
69
# File 'lib/apiculture/indifferent_hash.rb', line 67

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

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



71
72
73
# File 'lib/apiculture/indifferent_hash.rb', line 71

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

#assoc(key) ⇒ Object



55
56
57
# File 'lib/apiculture/indifferent_hash.rb', line 55

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

#default(*args) ⇒ Object



47
48
49
# File 'lib/apiculture/indifferent_hash.rb', line 47

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

#default=(value) ⇒ Object



51
52
53
# File 'lib/apiculture/indifferent_hash.rb', line 51

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

#delete(key) ⇒ Object



95
96
97
# File 'lib/apiculture/indifferent_hash.rb', line 95

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

#dig(key, *other_keys) ⇒ Object



99
100
101
# File 'lib/apiculture/indifferent_hash.rb', line 99

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

#fetch(key, *args) ⇒ Object



63
64
65
# File 'lib/apiculture/indifferent_hash.rb', line 63

def fetch(key, *args)
  super(convert_key(key), *args.map(&method(:convert_value)))
end

#fetch_values(*keys) ⇒ Object



103
104
105
# File 'lib/apiculture/indifferent_hash.rb', line 103

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

#key(value) ⇒ Object



77
78
79
# File 'lib/apiculture/indifferent_hash.rb', line 77

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

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

Returns:

  • (Boolean)


81
82
83
# File 'lib/apiculture/indifferent_hash.rb', line 81

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

#merge(other_hash, &block) ⇒ Object



130
131
132
# File 'lib/apiculture/indifferent_hash.rb', line 130

def merge(other_hash, &block)
  dup.merge!(other_hash, &block)
end

#merge!(other_hash) ⇒ Object Also known as: update



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/apiculture/indifferent_hash.rb', line 116

def merge!(other_hash)
  return super if other_hash.is_a?(self.class)

  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

  self
end

#rassoc(value) ⇒ Object



59
60
61
# File 'lib/apiculture/indifferent_hash.rb', line 59

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

#replace(other_hash) ⇒ Object



134
135
136
# File 'lib/apiculture/indifferent_hash.rb', line 134

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

#slice(*keys) ⇒ Object



107
108
109
110
# File 'lib/apiculture/indifferent_hash.rb', line 107

def slice(*keys)
  keys.map!(&method(:convert_key))
  self.class[super(*keys)]
end

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

Returns:

  • (Boolean)


89
90
91
# File 'lib/apiculture/indifferent_hash.rb', line 89

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

#values_at(*keys) ⇒ Object



112
113
114
# File 'lib/apiculture/indifferent_hash.rb', line 112

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