Class: Optout::Option
- Inherits:
-
Object
- Object
- Optout::Option
- Defined in:
- lib/optout.rb
Instance Attribute Summary collapse
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.create(key, *args) ⇒ Object
Creates a subclass of
Option
.
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Check if the option contains a value.
-
#to_a ⇒ Object
Turn the option into an array that can be passed to an
exec
like function. -
#to_s ⇒ Object
Turn the option into a string that can be to passed to a
system
like function. -
#validate! ⇒ Object
Validate the option.
Instance Attribute Details
#index ⇒ Object (readonly)
Returns the value of attribute index.
361 362 363 |
# File 'lib/optout.rb', line 361 def index @index end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
359 360 361 |
# File 'lib/optout.rb', line 359 def key @key end |
#value ⇒ Object (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) = Hash === args.last ? args.pop : {} switch = args.shift Class.new(Option) do define_method(:initialize) do |*v| @key = key @switch = switch @value = v.shift || [:default] @joinon = String === [:multiple] ? [:multiple] : "," @index = [:index].to_i @separator = [:arg_separator] || " " @validators = [] @validators << Validator::Required.new([:required]) @validators << Validator::Multiple.new([:multiple]) # Could be an Array..? @validators << Validator.for([:validator]) if [: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.
448 449 450 |
# File 'lib/optout.rb', line 448 def empty? !@value || @value.to_s.empty? end |
#to_a ⇒ Object
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_s ⇒ Object
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 |