Class: Thor::CoreExt::HashWithIndifferentAccess

Inherits:
HashWithIndifferentAccess
  • Object
show all
Defined in:
lib/thor/core_ext/hash_with_indifferent_access.rb

Overview

A hash with indifferent access and magic predicates.

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

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

Instance Method Summary collapse

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


29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/thor/core_ext/hash_with_indifferent_access.rb', line 29

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