Class: Thor::CoreExt::HashWithIndifferentAccess
- Inherits:
-
Hash
- Object
- Hash
- Thor::CoreExt::HashWithIndifferentAccess
show all
- Defined in:
- lib/wtch/vendor/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] hash['foo'] hash.foo?
Instance Method Summary
collapse
Constructor Details
14
15
16
17
18
19
|
# File 'lib/wtch/vendor/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
Magic predicates. For instance:
options.force? options.shebang options.test_framework?(:rspec)
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/wtch/vendor/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/wtch/vendor/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/wtch/vendor/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/wtch/vendor/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/wtch/vendor/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/wtch/vendor/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/wtch/vendor/thor/core_ext/hash_with_indifferent_access.rb', line 33
def values_at(*indices)
indices.collect { |key| self[convert_key(key)] }
end
|