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.

Parameters:

  • naked_hash (Hash)

    the Hash object to wrap with an Indifferent



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



108
109
110
111
# File 'lib/hash_tools/indifferent.rb', line 108

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.

Parameters:

  • k

    the key to fetch

Returns:



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.

Parameters:

  • k

    the key to set

  • v

    the value to set

Returns:

  • v



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



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

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



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

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.

Parameters:

  • k

    the key to set

  • blk

    the block for no value

Returns:

  • v



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

Parameters:

  • k (String, Symbol)

    the key to check

Returns:

  • (Boolean)


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).

Returns:

  • (Array)

    an array of keys



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).



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

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

Returns:

  • (Boolean)


113
114
115
# File 'lib/hash_tools/indifferent.rb', line 113

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

#to_hashObject



117
118
119
# File 'lib/hash_tools/indifferent.rb', line 117

def to_hash
  __getobj__.to_hash
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.



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

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

#value_present?(k) ⇒ Boolean

Checks if the value at the given key is non-empty

Parameters:

  • k (String, Symbol)

    the key to check

Returns:

  • (Boolean)


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

def value_present?(k)
  return false unless key?(k)
  v = self[k]
  return false unless v
  return !v.to_s.empty?
end