Class: Figgy::Hash

Inherits:
Hash
  • Object
show all
Defined in:
lib/figgy/hash.rb

Overview

Stolen from Thor::CoreExt::HashWithIndifferentAccess It’s smaller and more grokkable than ActiveSupport’s.

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Hash

Returns a new instance of Hash.



5
6
7
8
9
10
# File 'lib/figgy/hash.rb', line 5

def initialize(hash = {})
  super()
  hash.each do |key, value|
    self[convert_key(key)] = value
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object (protected)



61
62
63
64
65
66
67
# File 'lib/figgy/hash.rb', line 61

def method_missing(m, *args, &block)
  if m.to_s.end_with? "="
    self[m.to_s.chop] = args.shift
  else
    self[m]
  end
end

Instance Method Details

#[](key) ⇒ Object



12
13
14
# File 'lib/figgy/hash.rb', line 12

def [](key)
  super(convert_key(key))
end

#[]=(key, value) ⇒ Object



16
17
18
# File 'lib/figgy/hash.rb', line 16

def []=(key, value)
  super(convert_key(key), value)
end

#delete(key) ⇒ Object



24
25
26
# File 'lib/figgy/hash.rb', line 24

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

#dig(*keys) ⇒ Object



43
44
45
# File 'lib/figgy/hash.rb', line 43

def dig(*keys)
  super(*keys.map { |k| convert_key(k) })
end

#fetch(key, *extras) ⇒ Object



20
21
22
# File 'lib/figgy/hash.rb', line 20

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

#merge(other) ⇒ Object



32
33
34
# File 'lib/figgy/hash.rb', line 32

def merge(other)
  dup.merge!(other)
end

#merge!(other) ⇒ Object



36
37
38
39
40
41
# File 'lib/figgy/hash.rb', line 36

def merge!(other)
  other.each do |key, value|
    self[convert_key(key)] = value
  end
  self
end

#respond_to?(m) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/figgy/hash.rb', line 47

def respond_to?(m, *)
  super || key?(convert_key(m))
end

#respond_to_missing?(m) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/figgy/hash.rb', line 51

def respond_to_missing?(m, *)
  key?(convert_key(m)) || super
end

#values_at(*indices) ⇒ Object



28
29
30
# File 'lib/figgy/hash.rb', line 28

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