Class: Optout::Option

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



361
362
363
# File 'lib/optout.rb', line 361

def index
  @index
end

#keyObject (readonly)

Returns the value of attribute key.



359
360
361
# File 'lib/optout.rb', line 359

def key
  @key
end

#valueObject (readonly)

Returns the value of attribute value.



360
361
362
# File 'lib/optout.rb', line 360

def value
  @value
end

Class Method Details

.create(key, *args) ⇒ Object

Creates a subclass of Option

Parameters

[key (Symbol)] The hash key that will be used to lookup and create this option. [switch (String)] Optional. [config (Hash)] Describe how to validate and create the option.

Examples

MyOption = Optout::Option.create(:quality, "-q", :arg_separator => "=", :validator => Fixnum) opt = MyOption.new(75) opt.empty? # false opt.validate! opt.to_s # "-q='75'"



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/optout.rb', line 380

def self.create(key, *args)
  options = Hash === args.last ? args.pop : {}
  switch  = args.shift

  Class.new(Option) do
    define_method(:initialize) do |*v|
      @key    = key
      @switch = switch
      @value  = v.shift || options[:default]
      @joinon = String === options[:multiple] ? options[:multiple] : ","
      @index  = options[:index].to_i
      @separator = options[:arg_separator] || " "
      
      @validators = []
      @validators << Validator::Required.new(options[:required])
      @validators << Validator::Multiple.new(options[:multiple])

      # Could be an Array..?
      @validators << Validator.for(options[:validator]) if options[:validator]
    end
  end
end

Instance Method Details

#empty?Boolean

Check if the option contains a value

Returns

false if the option's value is false, nil, or an empty String, true otherwise.

Returns:



448
449
450
# File 'lib/optout.rb', line 448

def empty?
  !@value || @value.to_s.empty?
end

#to_aObject

Turn the option into an array that can be passed to an exec like function. This does not validate the option. You must call validate!.

Examples

MyOption = Optout::Option.create(:level, "-L", %w(fatal info warn debug)) MyOption.new("debug").to_a

Returns: [ "-L", "debug" ]



435
436
437
438
439
# File 'lib/optout.rb', line 435

def to_a
  opt = create_opt_array
  opt = [ opt.join(@separator) ] unless @separator =~ /\A\s+\z/
  opt
end

#to_sObject

Turn the option into a string that can be to passed to a system like function. This does not validate the option. You must call validate!.

Examples

MyOption = Optout::Option.create(:level, "-L", %w(fatal info warn debug)) MyOption.new("debug").to_s

Returns: "-L 'debug'"



413
414
415
416
417
418
419
420
421
422
423
# File 'lib/optout.rb', line 413

def to_s
  opt = create_opt_array
  if opt.any?
    if opt.size == 1
      opt[0] = quote(opt[0]) unless @switch
    else
      opt[1] = quote(opt[1])
    end
  end
  opt.join(@separator)
end

#validate!Object

Validate the option

Errors

[OptionRequired] The option is missing a required value [OptionUnknown] The option contains an unknown key [OptionInvalid] The option contains a value that does not conform to the defined specification



461
462
463
# File 'lib/optout.rb', line 461

def validate!
  @validators.each { |v| v.validate!(self) }
end