Class: HashTools::Indifferent

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/hash_tools/indifferent.rb

Overview

A tiny version of HashWithIndifferentAccess. Works like a wrapper proxy around a Ruby Hash. Does not support all of the methods for a Ruby Hash object, but nevertheless can be useful for checking params and for working with parsed JSON.

Instance Method Summary collapse

Constructor Details

#initialize(naked_hash) ⇒ Indifferent

Create a new Indifferent by supplying a Ruby Hash object to wrap. The Hash being wrapped is not going to be altered or copied.



11
12
13
# File 'lib/hash_tools/indifferent.rb', line 11

def initialize(naked_hash)
  __setobj__(naked_hash)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



98
99
100
101
# File 'lib/hash_tools/indifferent.rb', line 98

def method_missing(method_name, *args)
  return self[method_name] if key?(method_name) && args.empty?
  super
end

Instance Method Details

#[](k) ⇒ Object

Get a value from the Hash, bu supplying a Symbol or a String Key presence is verified by first trying a Symbol, and then a String.



20
21
22
23
# File 'lib/hash_tools/indifferent.rb', line 20

def [](k)
  v = __getobj__[__transform_key__(k)]
  __rewrap__(v)
end

#[]=(k, v) ⇒ Object

Set a value in the Hash, bu supplying a Symbol or a String as a key. Key presence is verified by first trying a Symbol, and then a String.



31
32
33
# File 'lib/hash_tools/indifferent.rb', line 31

def []=(k, v)
  __getobj__[ __transform_key__(k) ] = v
end

#each(&blk) ⇒ Object

Yields each key - value pair of the indifferent. If the value is a Hash as well, that hash will be wrapped in an Indifferent before returning



62
63
64
65
66
# File 'lib/hash_tools/indifferent.rb', line 62

def each(&blk)
  __getobj__.each do |k, v|
    blk.call([__transform_key__(k), __rewrap__(v)])
  end
end

#each_pairObject

Yields each key - value pair of the indifferent. If the value is a Hash as well, that hash will be wrapped in an Indifferent before returning



70
71
72
73
74
75
76
# File 'lib/hash_tools/indifferent.rb', line 70

def each_pair
  o = __getobj__
  keys.each do | k |
    value = o[__transform_key__(k)]
    yield(k, __rewrap__(value))
  end
end

#fetch(k, &blk) ⇒ Object

Fetch a value, by supplying a Symbol or a String as a key. Key presence is verified by first trying a Symbol, and then a String.



41
42
43
44
# File 'lib/hash_tools/indifferent.rb', line 41

def fetch(k, &blk)
  v = __getobj__.fetch( __transform_key__(k) , &blk)
  __rewrap__(v)
end

#key?(k) ⇒ Boolean Also known as: has_key?

Checks for key presence whether the key is a String or a Symbol



56
57
58
# File 'lib/hash_tools/indifferent.rb', line 56

def key?(k)
  __getobj__.has_key?( __transform_key__(k))
end

#keysArray

Get the keys of the Hash. The keys are returned as-is (both Symbols and Strings).



49
50
51
# File 'lib/hash_tools/indifferent.rb', line 49

def keys
  __getobj__.keys.map{|k| __transform_key__(k) }
end

#map(&blk) ⇒ Object

Maps over keys and values of the Hash. The key class will be preserved (i.e. within the block the keys will be either Strings or Symbols depending on what is used in the underlying Hash).



81
82
83
84
85
86
# File 'lib/hash_tools/indifferent.rb', line 81

def map(&blk)
  keys.map do |k| 
    tk = __transform_key__(k)
    yield [tk, __rewrap__(__getobj__[tk])]
  end
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean



103
104
105
# File 'lib/hash_tools/indifferent.rb', line 103

def respond_to_missing?(method_name, include_private=false)
  key?(method_name)
end

#to_json(*serializer_state) ⇒ Object

There is a quirk whereby the delegate library will not pass to_json to the contained Hash, and the Indifferent would the JSON-serialize as a String. We have to forward this method explicitly.

In general, the method will never be called by the user directly but will instead be excercised by JSON.dump() and friends.



94
95
96
# File 'lib/hash_tools/indifferent.rb', line 94

def to_json(*serializer_state)
  to_h.to_json(*serializer_state)
end