Class: Dynflow::Utils::IndifferentHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/dynflow/utils/indifferent_hash.rb

Overview

Heavily inpired by ActiveSupport::HashWithIndifferentAccess, reasons we don’t want to use the original implementation:

1. we don't want any core_ext extensions
2. some users are not happy about seeing the ActiveSupport as
our depednency

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constructor = {}) ⇒ IndifferentHash

Returns a new instance of IndifferentHash.



10
11
12
13
14
15
16
17
# File 'lib/dynflow/utils/indifferent_hash.rb', line 10

def initialize(constructor = {})
  if constructor.respond_to?(:to_hash)
    super()
    update(constructor)
  else
    super(constructor)
  end
end

Class Method Details

.[](*args) ⇒ Object



27
28
29
# File 'lib/dynflow/utils/indifferent_hash.rb', line 27

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

Instance Method Details

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



34
35
36
# File 'lib/dynflow/utils/indifferent_hash.rb', line 34

def []=(key, value)
  regular_writer(convert_key(key), convert_value(value, for: :assignment))
end

#deep_stringify_keysObject



101
# File 'lib/dynflow/utils/indifferent_hash.rb', line 101

def deep_stringify_keys; dup end

#deep_stringify_keys!Object



99
# File 'lib/dynflow/utils/indifferent_hash.rb', line 99

def deep_stringify_keys!; self end

#default(key = nil) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/dynflow/utils/indifferent_hash.rb', line 19

def default(key = nil)
  if key.is_a?(Symbol) && include?(key = key.to_s)
    self[key]
  else
    super
  end
end

#delete(key) ⇒ Object



94
95
96
# File 'lib/dynflow/utils/indifferent_hash.rb', line 94

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

#dupObject



72
73
74
75
76
# File 'lib/dynflow/utils/indifferent_hash.rb', line 72

def dup
  self.class.new(self).tap do |new_hash|
    new_hash.default = default
  end
end

#fetch(key, *extras) ⇒ Object



64
65
66
# File 'lib/dynflow/utils/indifferent_hash.rb', line 64

def fetch(key, *extras)
  super(convert_key(key), *extras)
end

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

Returns:

  • (Boolean)


56
57
58
# File 'lib/dynflow/utils/indifferent_hash.rb', line 56

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

#merge(hash, &block) ⇒ Object



78
79
80
# File 'lib/dynflow/utils/indifferent_hash.rb', line 78

def merge(hash, &block)
  self.dup.update(hash, &block)
end

#regular_updateObject



32
# File 'lib/dynflow/utils/indifferent_hash.rb', line 32

alias_method :regular_update, :update

#regular_writerObject



31
# File 'lib/dynflow/utils/indifferent_hash.rb', line 31

alias_method :regular_writer, :[]=

#reject(*args, &block) ⇒ Object



108
109
110
# File 'lib/dynflow/utils/indifferent_hash.rb', line 108

def reject(*args, &block)
  dup.tap { |hash| hash.reject!(*args, &block) }
end

#replace(other_hash) ⇒ Object



90
91
92
# File 'lib/dynflow/utils/indifferent_hash.rb', line 90

def replace(other_hash)
  super(self.class.new_from_hash_copying_default(other_hash))
end

#reverse_merge(other_hash) ⇒ Object



82
83
84
# File 'lib/dynflow/utils/indifferent_hash.rb', line 82

def reverse_merge(other_hash)
  super(self.class.new_from_hash_copying_default(other_hash))
end

#reverse_merge!(other_hash) ⇒ Object



86
87
88
# File 'lib/dynflow/utils/indifferent_hash.rb', line 86

def reverse_merge!(other_hash)
  replace(reverse_merge( other_hash ))
end

#select(*args, &block) ⇒ Object



104
105
106
# File 'lib/dynflow/utils/indifferent_hash.rb', line 104

def select(*args, &block)
  dup.tap { |hash| hash.select!(*args, &block) }
end

#stringify_keysObject



100
# File 'lib/dynflow/utils/indifferent_hash.rb', line 100

def stringify_keys; dup end

#stringify_keys!Object



98
# File 'lib/dynflow/utils/indifferent_hash.rb', line 98

def stringify_keys!; self end

#to_hashObject

Convert to a regular hash with string keys.



113
114
115
116
117
118
119
# File 'lib/dynflow/utils/indifferent_hash.rb', line 113

def to_hash
  _new_hash = Hash.new(default)
  each do |key, value|
    _new_hash[key] = convert_value(value, for: :to_hash)
  end
  _new_hash
end

#to_options!Object



102
# File 'lib/dynflow/utils/indifferent_hash.rb', line 102

def to_options!; self end

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



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dynflow/utils/indifferent_hash.rb', line 40

def update(other_hash)
  if other_hash.is_a? IndifferentHash
    super(other_hash)
  else
    other_hash.to_hash.each_pair do |key, value|
      if block_given? && key?(key)
        value = yield(convert_key(key), self[key], value)
      end
      regular_writer(convert_key(key), convert_value(value))
    end
    self
  end
end

#values_at(*indices) ⇒ Object



68
69
70
# File 'lib/dynflow/utils/indifferent_hash.rb', line 68

def values_at(*indices)
  indices.collect { |key| self[convert_key(key)] }
end