Class: Commander::Command::Options

Inherits:
Object
  • Object
show all
Includes:
Blank
Defined in:
lib/commander/command.rb

Overview

Options struct.

An open-ended, OpenStruct-like object populated by #option and #proxy_option_struct. Reading an unset attribute returns nil; writing any attribute (via some_attr=) stores it. Almost all of Object's own instance methods are stripped out (see Blank) so that option names never collide with built-ins like send or class.

Instance Method Summary collapse

Methods included from Blank

included

Constructor Details

#initializeOptions

Returns a new instance of Options.



32
33
34
# File 'lib/commander/command.rb', line 32

def initialize
  @table = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

Reads or writes an arbitrary option. options.foo reads the :foo entry; options.foo = 1 writes it.



45
46
47
# File 'lib/commander/command.rb', line 45

def method_missing(meth, *args)
  meth.to_s =~ /=$/ ? @table[meth.to_s.chop.to_sym] = args.first : @table[meth]
end

Instance Method Details

#__hash__Object

The underlying Hash of option name (Symbol) => value pairs.



38
39
40
# File 'lib/commander/command.rb', line 38

def __hash__
  @table
end

#default(defaults = {}) ⇒ Object

Merge defaults into this struct, without overwriting any options already set.



52
53
54
# File 'lib/commander/command.rb', line 52

def default(defaults = {})
  @table = defaults.merge! @table
end

#inspectObject



56
57
58
# File 'lib/commander/command.rb', line 56

def inspect
  "<Commander::Command::Options #{ __hash__.map { |k, v| "#{k}=#{v.inspect}" }.join(', ') }>"
end