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
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 collapse

Instance Method Summary collapse

Class Method Details

.[](*args) ⇒ Object



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

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

Instance Method Details

#[](key) ⇒ Object



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

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

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



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

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

#assoc(key) ⇒ Object



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

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

#compactObject



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

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

#default(*args) ⇒ Object



46
47
48
49
50
# File 'lib/sinatra/indifferent_hash.rb', line 46

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

  super(*args)
end

#default=(value) ⇒ Object



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

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

#delete(key) ⇒ Object



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

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

#dig(key, *other_keys) ⇒ Object

Added in Ruby 2.3



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

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

#except(*keys) ⇒ Object



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

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

  self.class[super(*keys)]
end

#fetch(key, *args) ⇒ Object



64
65
66
67
68
# 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) ⇒ Object



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

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

  super(*keys)
end

#key(value) ⇒ Object



80
81
82
# 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?

Returns:

  • (Boolean)


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

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

#merge(*other_hashes, &block) ⇒ Object



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

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

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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# 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) ⇒ Object



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

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

#reject(*args, &block) ⇒ Object



175
176
177
178
179
# 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) ⇒ Object



147
148
149
# 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) ⇒ Object



169
170
171
172
173
# 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) ⇒ Object



113
114
115
116
117
# File 'lib/sinatra/indifferent_hash.rb', line 113

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

  self.class[super(*keys)]
end

#transform_keys(&block) ⇒ Object



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

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

#transform_keys!Object



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

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

#transform_values(&block) ⇒ Object



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

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

#transform_values!Object



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

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

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

Returns:

  • (Boolean)


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

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

#values_at(*keys) ⇒ Object



119
120
121
122
123
# File 'lib/sinatra/indifferent_hash.rb', line 119

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

  super(*keys)
end