Class: Pushwagner::HashWithIndifferentAccess

Inherits:
Hash
  • Object
show all
Defined in:
lib/pushwagner/ext.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ HashWithIndifferentAccess

Returns a new instance of HashWithIndifferentAccess.



3
4
5
6
7
8
# File 'lib/pushwagner/ext.rb', line 3

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(method, *args, &block) ⇒ Object (protected)



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pushwagner/ext.rb', line 46

def method_missing(method, *args, &block)
  method = method.to_s
  if method =~ /^(\w+)\?$/
    if args.empty?
      !!self[$1]
    else
      self[$1] == args.first
    end
  else
    self[method]
  end
end

Instance Method Details

#[](key) ⇒ Object



10
11
12
# File 'lib/pushwagner/ext.rb', line 10

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

#[]=(key, value) ⇒ Object



14
15
16
# File 'lib/pushwagner/ext.rb', line 14

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

#delete(key) ⇒ Object



18
19
20
# File 'lib/pushwagner/ext.rb', line 18

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

#merge(other) ⇒ Object



26
27
28
# File 'lib/pushwagner/ext.rb', line 26

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

#merge!(other) ⇒ Object



30
31
32
33
34
35
# File 'lib/pushwagner/ext.rb', line 30

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

#to_hashObject



37
38
39
# File 'lib/pushwagner/ext.rb', line 37

def to_hash
  Hash.new(default).merge!(self)
end

#values_at(*indices) ⇒ Object



22
23
24
# File 'lib/pushwagner/ext.rb', line 22

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