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)



49
50
51
52
53
54
55
# File 'lib/figgy/hash.rb', line 49

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



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

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

#merge(other) ⇒ Object



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

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

#merge!(other) ⇒ Object



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

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

#respond_to_missing?(m) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#values_at(*indices) ⇒ Object



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

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