Class: Argy::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/argy/options.rb

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ Options

Returns a new instance of Options.



3
4
5
# File 'lib/argy/options.rb', line 3

def initialize(values)
  @values = values
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object (private)



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/argy/options.rb', line 25

def method_missing(meth, *args)
  query = meth[-1] == "?"
  key = query ? meth[0..-2].to_sym : meth.to_sym

  return super unless @values.key?(key)

  unless args.empty?
    raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 0)"
  end

  query ? !!@values[key] : @values[key]
end

Instance Method Details

#[](key) ⇒ Object



11
12
13
# File 'lib/argy/options.rb', line 11

def [](key)
  @values[key]
end

#fetch(*args, &block) ⇒ Object



15
16
17
# File 'lib/argy/options.rb', line 15

def fetch(*args, &block)
  @values.fetch(*args, &block)
end

#to_hObject



7
8
9
# File 'lib/argy/options.rb', line 7

def to_h
  @values
end