Class: Thor::CoreExt::HashWithIndifferentAccess

Inherits:
Hash
  • Object
show all
Defined in:
lib/daemon_kit/vendor/thor-0.13.6/lib/thor/core_ext/hash_with_indifferent_access.rb

Overview

A hash with indifferent access and magic predicates.

hash = Thor::CoreExt::HashWithIndifferentAccess.new 'foo' => 'bar', 'baz' => 'bee', 'force' => true

hash[:foo]  #=> 'bar'
hash['foo'] #=> 'bar'
hash.foo?   #=> true

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ HashWithIndifferentAccess

:nodoc:



14
15
16
17
18
19
# File 'lib/daemon_kit/vendor/thor-0.13.6/lib/thor/core_ext/hash_with_indifferent_access.rb', line 14

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)

Magic predicates. For instance:

options.force?                  # => !!options['force']
options.shebang                 # => "/usr/lib/local/ruby"
options.test_framework?(:rspec) # => options[:test_framework] == :rspec


60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/daemon_kit/vendor/thor-0.13.6/lib/thor/core_ext/hash_with_indifferent_access.rb', line 60

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



21
22
23
# File 'lib/daemon_kit/vendor/thor-0.13.6/lib/thor/core_ext/hash_with_indifferent_access.rb', line 21

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

#[]=(key, value) ⇒ Object



25
26
27
# File 'lib/daemon_kit/vendor/thor-0.13.6/lib/thor/core_ext/hash_with_indifferent_access.rb', line 25

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

#delete(key) ⇒ Object



29
30
31
# File 'lib/daemon_kit/vendor/thor-0.13.6/lib/thor/core_ext/hash_with_indifferent_access.rb', line 29

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

#merge(other) ⇒ Object



37
38
39
# File 'lib/daemon_kit/vendor/thor-0.13.6/lib/thor/core_ext/hash_with_indifferent_access.rb', line 37

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

#merge!(other) ⇒ Object



41
42
43
44
45
46
# File 'lib/daemon_kit/vendor/thor-0.13.6/lib/thor/core_ext/hash_with_indifferent_access.rb', line 41

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

#values_at(*indices) ⇒ Object



33
34
35
# File 'lib/daemon_kit/vendor/thor-0.13.6/lib/thor/core_ext/hash_with_indifferent_access.rb', line 33

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