Class: AppKernel::Function::Options::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/appkernel/function.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, modifiers = {}) ⇒ Option

Returns a new instance of Option.


261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/appkernel/function.rb', line 261

def initialize(name, modifiers = {})
  if name.kind_of?(Option)
    modifiers = modifiers.merge(name.modifiers)
  end
  @name = name.to_sym
  @modifiers = modifiers
  @index = modifiers[:index]
  @required = modifiers[:required] == true
  @lookup = modifiers[:lookup] || modifiers[:parse]
  @type = modifiers[:type]
  @list = modifiers[:list]
  @greedy = modifiers[:greedy]
  begin
    @default = resolve(modifiers[:default])            
  rescue StandardError => e
    raise IllegalOptionError, "invalid default value for option '#{name}': #{e.message}"
  end
  
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.


259
260
261
# File 'lib/appkernel/function.rb', line 259

def default
  @default
end

#indexObject (readonly)

Returns the value of attribute index.


259
260
261
# File 'lib/appkernel/function.rb', line 259

def index
  @index
end

#modifiersObject (readonly)

Returns the value of attribute modifiers.


259
260
261
# File 'lib/appkernel/function.rb', line 259

def modifiers
  @modifiers
end

#nameObject (readonly)

Returns the value of attribute name.


259
260
261
# File 'lib/appkernel/function.rb', line 259

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.


259
260
261
# File 'lib/appkernel/function.rb', line 259

def type
  @type
end

Instance Method Details

#default?Boolean

Returns:


285
286
287
# File 'lib/appkernel/function.rb', line 285

def default?
  !@default.nil?
end

#greedy?Boolean

Returns:


293
294
295
# File 'lib/appkernel/function.rb', line 293

def greedy?
  @greedy
end

#list?Boolean

Returns:


289
290
291
# File 'lib/appkernel/function.rb', line 289

def list?
  @list
end

#required?Boolean

Returns:


281
282
283
# File 'lib/appkernel/function.rb', line 281

def required?
  @required
end

#resolve(o, single = false) ⇒ Object


301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/appkernel/function.rb', line 301

def resolve(o, single = false)
  if o.nil? then nil
  elsif @list && !single
    o.kind_of?(Array) ? o.map {|v| resolve(v,true)} : [resolve(o, true)]  
  elsif @type
    if @type.kind_of?(Class) && o.kind_of?(@type) then o
    elsif @type.kind_of?(Enumerable) && @type.detect {|t| o.kind_of?(t)} then o
    elsif @lookup
      @lookup.call(o)
    elsif @type.respond_to?(:to_option)
      begin
        @type.to_option(o)
      rescue StandardError => e
        raise ArgumentError, "don't know how to convert #{o} into #{@type}: #{e}"
      end
    else
      raise ArgumentError, "don't know how to convert #{o} into #{@type}"
    end
  else
    @lookup ? @lookup.call(o) : o
  end                    
end

#to_symObject


297
298
299
# File 'lib/appkernel/function.rb', line 297

def to_sym
  @name
end