Class: Sinatra::IndifferentHash

Inherits:
Hash
  • Object
show all
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(: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.


44
45
46
47
48
# File 'lib/sinatra/indifferent_hash.rb', line 44

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

  super(*args)
end

Class Method Details

.[](*args) ⇒ Object


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

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

Instance Method Details

#[](key) ⇒ Object


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

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

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


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

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

#assoc(key) ⇒ Object


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

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

#compactObject


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

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

#default(*args) ⇒ Object


50
51
52
53
54
# File 'lib/sinatra/indifferent_hash.rb', line 50

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

  super(*args)
end

#default=(value) ⇒ Object


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

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

#delete(key) ⇒ Object


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

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

#dig(key, *other_keys) ⇒ Object

Added in Ruby 2.3


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

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

#fetch(key, *args) ⇒ Object


68
69
70
71
72
# File 'lib/sinatra/indifferent_hash.rb', line 68

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

  super(convert_key(key), *args)
end

#fetch_values(*keys) ⇒ Object


111
112
113
114
115
# File 'lib/sinatra/indifferent_hash.rb', line 111

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

  super(*keys)
end

#key(value) ⇒ Object


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

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

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

Returns:

  • (Boolean)

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

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

#merge(*other_hashes, &block) ⇒ Object


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

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

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


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/sinatra/indifferent_hash.rb', line 129

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) ⇒ Object


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

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

#reject(*args, &block) ⇒ Object


179
180
181
182
183
# File 'lib/sinatra/indifferent_hash.rb', line 179

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

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

#replace(other_hash) ⇒ Object


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

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

#select(*args, &block) ⇒ Object


173
174
175
176
177
# File 'lib/sinatra/indifferent_hash.rb', line 173

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

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

#slice(*keys) ⇒ Object


117
118
119
120
121
# File 'lib/sinatra/indifferent_hash.rb', line 117

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

  self.class[super(*keys)]
end

#transform_keys(&block) ⇒ Object


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

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

#transform_keys!Object


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

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

#transform_values(&block) ⇒ Object


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

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

#transform_values!Object


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

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

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

Returns:

  • (Boolean)

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

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

#values_at(*keys) ⇒ Object


123
124
125
126
127
# File 'lib/sinatra/indifferent_hash.rb', line 123

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

  super(*keys)
end