Class: HashTools::Indifferent

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

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



14
15
16
# File 'lib/hash_tools/indifferent.rb', line 14

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



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

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

  super
end

Instance Method Details

#[](key) ⇒ 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:



23
24
25
26
# File 'lib/hash_tools/indifferent.rb', line 23

def [](key)
  value = __getobj__[__transform_key__(key)]
  __rewrap__(value)
end

#[]=(key, value) ⇒ 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



34
35
36
# File 'lib/hash_tools/indifferent.rb', line 34

def []=(key, value)
  __getobj__[__transform_key__(key)] = value
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



77
78
79
80
81
# File 'lib/hash_tools/indifferent.rb', line 77

def each(&blk)
  __getobj__.each do |key, value|
    blk.call([__transform_key__(key), __rewrap__(value)])
  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



85
86
87
88
89
90
91
# File 'lib/hash_tools/indifferent.rb', line 85

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

#fetch(key, &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



44
45
46
47
# File 'lib/hash_tools/indifferent.rb', line 44

def fetch(key, &blk)
  value = __getobj__.fetch(__transform_key__(key), &blk)
  __rewrap__(value)
end

#key?(key) ⇒ 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)


59
60
61
# File 'lib/hash_tools/indifferent.rb', line 59

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

#keysArray

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

Returns:

  • (Array)

    an array of keys



52
53
54
# File 'lib/hash_tools/indifferent.rb', line 52

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

#mapObject

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



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

def map
  keys.map do |key|
    transform_key = __transform_key__(key)
    yield [transform_key, __rewrap__(__getobj__[transform_key])]
  end
end

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

Returns:

  • (Boolean)


119
120
121
# File 'lib/hash_tools/indifferent.rb', line 119

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

#to_hashObject



123
124
125
# File 'lib/hash_tools/indifferent.rb', line 123

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.



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

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

#value_present?(key) ⇒ Boolean

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

Parameters:

  • k (String, Symbol)

    the key to check

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
# File 'lib/hash_tools/indifferent.rb', line 66

def value_present?(key)
  return false unless key?(key)

  value = self[key]
  return false unless value

  !value.to_s.empty?
end