Class: Bundler::Thor::CoreExt::HashWithIndifferentAccess

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

Overview

A hash with indifferent access and magic predicates.

hash = Bundler::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:



12
13
14
15
16
17
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb', line 12

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) ⇒ Object (protected)

Magic predicates. For instance:

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


83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb', line 83

def method_missing(method, *args)
  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



19
20
21
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb', line 19

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

#[]=(key, value) ⇒ Object



23
24
25
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb', line 23

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

#delete(key) ⇒ Object



27
28
29
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb', line 27

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

#fetch(key, *args) ⇒ Object



31
32
33
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb', line 31

def fetch(key, *args)
  super(convert_key(key), *args)
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb', line 35

def key?(key)
  super(convert_key(key))
end

#merge(other) ⇒ Object



43
44
45
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb', line 43

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

#merge!(other) ⇒ Object



47
48
49
50
51
52
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb', line 47

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

#replace(other_hash) ⇒ Object



62
63
64
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb', line 62

def replace(other_hash)
  super(other_hash)
end

#reverse_merge(other) ⇒ Object



54
55
56
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb', line 54

def reverse_merge(other)
  self.class.new(other).merge(self)
end

#reverse_merge!(other_hash) ⇒ Object



58
59
60
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb', line 58

def reverse_merge!(other_hash)
  replace(reverse_merge(other_hash))
end

#to_hashObject

Convert to a Hash with String keys.



67
68
69
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb', line 67

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

#values_at(*indices) ⇒ Object



39
40
41
# File 'lib/bundler/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb', line 39

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