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

- (Hash) initialize(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

- (Object) method_missing(m, *args, &block) (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

- (Object) [](key)



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

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

- (Object) []=(key, value)



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

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

- (Object) delete(key)



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

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

- (Object) merge(other)



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

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

- (Object) merge!(other)



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

- (Boolean) respond_to_missing?(m)



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

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

- (Object) values_at(*indices)



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

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