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.


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

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

  super(*args)
end

Class Method Details

.[](*args) ⇒ Object


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

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

Instance Method Details

#[](key) ⇒ Object


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

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

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


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

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

#assoc(key) ⇒ Object


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

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

#compactObject


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

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

#default(*args) ⇒ Object


57
58
59
60
61
# File 'lib/sinatra/indifferent_hash.rb', line 57

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

  super(*args)
end

#default=(value) ⇒ Object


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

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

#delete(key) ⇒ Object


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

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

#dig(key, *other_keys) ⇒ Object


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

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

#fetch(key, *args) ⇒ Object


75
76
77
78
79
# File 'lib/sinatra/indifferent_hash.rb', line 75

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

  super(convert_key(key), *args)
end

#fetch_values(*keys) ⇒ Object


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

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

  super(*keys)
end

#key(value) ⇒ Object


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

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

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

Returns:

  • (Boolean)

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

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

#merge(*other_hashes, &block) ⇒ Object


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

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

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


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/sinatra/indifferent_hash.rb', line 135

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


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

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

#reject(*args, &block) ⇒ Object


188
189
190
191
# File 'lib/sinatra/indifferent_hash.rb', line 188

def reject(*args, &block)
  return to_enum(:reject) unless block_given?
  dup.tap { |hash| hash.reject!(*args, &block) }
end

#replace(other_hash) ⇒ Object


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

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

#select(*args, &block) ⇒ Object


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

def select(*args, &block)
  return to_enum(:select) unless block_given?
  dup.tap { |hash| hash.select!(*args, &block) }
end

#slice(*keys) ⇒ Object


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

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

  self.class[super(*keys)]
end

#transform_keys(&block) ⇒ Object


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

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

#transform_keys!Object


177
178
179
180
# File 'lib/sinatra/indifferent_hash.rb', line 177

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

#transform_values(&block) ⇒ Object


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

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

#transform_values!Object


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

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

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

Returns:

  • (Boolean)

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

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

#values_at(*keys) ⇒ Object


129
130
131
132
133
# File 'lib/sinatra/indifferent_hash.rb', line 129

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

  super(*keys)
end