Module: Hashie::Extensions::IndifferentAccess

Defined in:
lib/hashie/extensions/indifferent_access.rb

Overview

IndifferentAccess gives you the ability to not care whether your hash has string or symbol keys. Made famous in Rails for accessing query and POST parameters, this is a handy tool for making sure your hash has maximum utility.

One unique feature of this mixin is that it will recursively inject itself into sub-hash instances without modifying the actual class of the sub-hash.

Examples:

class MyHash < Hash
  include Hashie::Extensions::MergeInitializer
  include Hashie::Extensions::IndifferentAccess
end

h = MyHash.new(:foo => 'bar', 'baz' => 'blip')
h['foo'] # => 'bar'
h[:foo]  # => 'bar'
h[:baz]  # => 'blip'
h['baz'] # => 'blip'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/hashie/extensions/indifferent_access.rb', line 26

def self.included(base)
  base.class_eval do
    alias_method :regular_writer, :[]=
    alias_method :[]=, :indifferent_writer
    alias_method :store, :indifferent_writer
    %w(default update replace fetch delete key? values_at).each do |m|
      alias_method "regular_#{m}", m
      alias_method m, "indifferent_#{m}"
    end

    %w(include? member? has_key?).each do |key_alias|
      alias_method key_alias, :indifferent_key?
    end

    class << self
      def [](*)
        super.convert!
      end

      def try_convert(*)
        (hash = super) && self[hash]
      end
    end
  end
end

.inject(hash) ⇒ Object

Injects indifferent access into a duplicate of the hash provided. See #inject!



62
63
64
# File 'lib/hashie/extensions/indifferent_access.rb', line 62

def self.inject(hash)
  inject!(hash.dup)
end

.inject!(hash) ⇒ Object

This will inject indifferent access into an instance of a hash without modifying the actual class. This is what allows IndifferentAccess to spread to sub-hashes.



55
56
57
58
# File 'lib/hashie/extensions/indifferent_access.rb', line 55

def self.inject!(hash)
  (class << hash; self; end).send :include, IndifferentAccess
  hash.convert!
end

Instance Method Details

#convert!Object

Iterates through the keys and values, reconverting them to their proper indifferent state. Used when IndifferentAccess is injecting itself into member hashes.



73
74
75
76
77
78
# File 'lib/hashie/extensions/indifferent_access.rb', line 73

def convert!
  keys.each do |k|
    regular_writer convert_key(k), convert_value(regular_delete(k))
  end
  self
end

#convert_key(key) ⇒ Object



66
67
68
# File 'lib/hashie/extensions/indifferent_access.rb', line 66

def convert_key(key)
  key.to_s
end

#convert_value(value) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/hashie/extensions/indifferent_access.rb', line 80

def convert_value(value)
  if hash_lacking_indifference?(value)
    IndifferentAccess.inject(value.dup)
  elsif value.is_a?(::Array)
    value.dup.replace(value.map { |e| convert_value(e) })
  else
    value
  end
end

#indifferent_access?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/hashie/extensions/indifferent_access.rb', line 122

def indifferent_access?
  true
end

#indifferent_default(key = nil) ⇒ Object



90
91
92
93
# File 'lib/hashie/extensions/indifferent_access.rb', line 90

def indifferent_default(key = nil)
  return self[convert_key(key)] if key?(key)
  regular_default(key)
end

#indifferent_delete(key) ⇒ Object



110
111
112
# File 'lib/hashie/extensions/indifferent_access.rb', line 110

def indifferent_delete(key)
  regular_delete convert_key(key)
end

#indifferent_fetch(key, *args) ⇒ Object



106
107
108
# File 'lib/hashie/extensions/indifferent_access.rb', line 106

def indifferent_fetch(key, *args)
  regular_fetch convert_key(key), *args
end

#indifferent_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/hashie/extensions/indifferent_access.rb', line 114

def indifferent_key?(key)
  regular_key? convert_key(key)
end

#indifferent_replace(other_hash) ⇒ Object



126
127
128
129
130
# File 'lib/hashie/extensions/indifferent_access.rb', line 126

def indifferent_replace(other_hash)
  (keys - other_hash.keys).each { |key| delete(key) }
  other_hash.each { |key, value| self[key] = value }
  self
end

#indifferent_update(other_hash) ⇒ Object



95
96
97
98
99
100
# File 'lib/hashie/extensions/indifferent_access.rb', line 95

def indifferent_update(other_hash)
  return regular_update(other_hash) if hash_with_indifference?(other_hash)
  other_hash.each_pair do |k, v|
    self[k] = v
  end
end

#indifferent_values_at(*indices) ⇒ Object



118
119
120
# File 'lib/hashie/extensions/indifferent_access.rb', line 118

def indifferent_values_at(*indices)
  indices.map { |i| self[i] }
end

#indifferent_writer(key, value) ⇒ Object



102
103
104
# File 'lib/hashie/extensions/indifferent_access.rb', line 102

def indifferent_writer(key, value)
  regular_writer convert_key(key), convert_value(value)
end