Class: Commander::Command::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/rhc/commands.rb

Instance Method Summary collapse

Constructor Details

#initialize(init = nil) ⇒ Options

Returns a new instance of Options.



118
119
120
121
122
# File 'lib/rhc/commands.rb', line 118

def initialize(init=nil)
  @defaults = {}
  @table = {}
  default(init) if init
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rhc/commands.rb', line 126

def method_missing meth, *args, &block
  if meth.to_s =~ /^\w+=$/
    raise ArgumentError, "Options does not support #{meth} without a single argument" if args.length != 1
    self[meth.to_s.chop] = args.first
  elsif meth.to_s =~ /^\w+$/
    if !@table.has_key?(meth) && !@defaults.has_key?(meth)
      begin; return super; rescue NoMethodError; nil; end
    end
    raise ArgumentError, "Options does not support #{meth} with arguments" if args.length != 0
    self[meth]
  else
    super
  end
end

Instance Method Details

#==(other) ⇒ Object



155
156
157
# File 'lib/rhc/commands.rb', line 155

def ==(other)
  @table == other.instance_variable_get(:@table)
end

#[](meth) ⇒ Object



146
147
148
149
150
151
# File 'lib/rhc/commands.rb', line 146

def [](meth)
  k = meth.to_sym
  value = @table.has_key?(k) ? @table[k] : @defaults[k]
  value = value.call if value.is_a? Proc
  value
end

#[]=(meth, value) ⇒ Object



143
144
145
# File 'lib/rhc/commands.rb', line 143

def []=(meth, value)
  @table[meth.to_sym] = value
end

#__explicit__Object



152
153
154
# File 'lib/rhc/commands.rb', line 152

def __explicit__
  @table
end

#__hash__Object



164
165
166
# File 'lib/rhc/commands.rb', line 164

def __hash__
  @defaults.merge(@table)
end

#__replace__(options) ⇒ Object



161
162
163
# File 'lib/rhc/commands.rb', line 161

def __replace__(options)
  @table = __to_hash__(options)
end

#__to_hash__(obj) ⇒ Object



167
168
169
# File 'lib/rhc/commands.rb', line 167

def __to_hash__(obj)
  Options === obj ? obj.__hash__ : obj
end

#default(defaults = {}) ⇒ Object



158
159
160
# File 'lib/rhc/commands.rb', line 158

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

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/rhc/commands.rb', line 123

def respond_to?(meth)
  super || meth.to_s =~ /^\w+(=)?$/
end

#respond_to_missing?(meth, private_method = false) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/rhc/commands.rb', line 140

def respond_to_missing?(meth, private_method = false)
  meth.to_s =~ /^\w+(=)?$/
end