Class: OptSimple::Result
- Inherits:
-
Object
- Object
- OptSimple::Result
- Defined in:
- lib/opt_simple.rb
Overview
The Results after parsing the CL in a hash-like object with method syntax for getters/setters as well. Each Result that belong to the same Parameter are aliased to keep consistent
Instance Attribute Summary collapse
-
#positional_args ⇒ Object
Returns the value of attribute positional_args.
Instance Method Summary collapse
-
#[](key) ⇒ Object
hash notation getter.
-
#[]=(key, value) ⇒ Object
hash notation setter.
-
#add_alias(list) ⇒ Object
add a list of items that should be treated as the same key.
-
#add_vals_from_hash(hash) ⇒ Object
copy the values from hash into this Result object.
-
#aliases ⇒ Object
a list of all the aliases.
-
#include?(key) ⇒ Boolean
check to see if the ‘key’ option was set.
-
#initialize ⇒ Result
constructor
A new instance of Result.
-
#merge(other) ⇒ Object
merge non-destructively, and return the Result.
-
#merge!(other) ⇒ Object
merge into this Result and return the Result.
-
#method_missing(sym, *args, &block) ⇒ Object
this allows for method calls for getters/setters.
-
#to_s ⇒ Object
a hash looking return string.
Constructor Details
#initialize ⇒ Result
Returns a new instance of Result.
510 511 512 513 514 |
# File 'lib/opt_simple.rb', line 510 def initialize @name_to_aliases = {} @inside_hash = {} @positional_args = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
this allows for method calls for getters/setters
572 573 574 575 576 577 578 579 580 581 582 |
# File 'lib/opt_simple.rb', line 572 def method_missing(sym,*args,&block) sym_str = sym.to_s if @name_to_aliases.has_key?(sym) return @inside_hash[@name_to_aliases[sym]] elsif sym_str.end_with?('=') and @name_to_aliases.has_key?(sym_str[0..-2]) @inside_hash[@name_to_aliases[sym_str[0..-2]]] = args.first else super(sym,*args,&block) end end |
Instance Attribute Details
#positional_args ⇒ Object
Returns the value of attribute positional_args.
508 509 510 |
# File 'lib/opt_simple.rb', line 508 def positional_args @positional_args end |
Instance Method Details
#[](key) ⇒ Object
hash notation getter
527 528 529 |
# File 'lib/opt_simple.rb', line 527 def [](key) @inside_hash[@name_to_aliases[key]] end |
#[]=(key, value) ⇒ Object
hash notation setter
517 518 519 520 521 522 523 524 |
# File 'lib/opt_simple.rb', line 517 def []=(key,value) if @name_to_aliases.has_key?(key) @inside_hash[@name_to_aliases[key]] = value else add_alias(key) @inside_hash[[key].flatten] = value end end |
#add_alias(list) ⇒ Object
add a list of items that should be treated as the same key
532 533 534 535 536 537 538 539 |
# File 'lib/opt_simple.rb', line 532 def add_alias(list) alias_list = [list].flatten alias_list.each do | item | @name_to_aliases[item] = alias_list @name_to_aliases[item.to_s] = alias_list @name_to_aliases[item.to_sym] = alias_list end end |
#add_vals_from_hash(hash) ⇒ Object
copy the values from hash into this Result object
542 543 544 |
# File 'lib/opt_simple.rb', line 542 def add_vals_from_hash(hash) hash.keys.each { |k| self[k] = hash[k] } end |
#aliases ⇒ Object
a list of all the aliases
567 568 569 |
# File 'lib/opt_simple.rb', line 567 def aliases @name_to_aliases.values.uniq end |
#include?(key) ⇒ Boolean
check to see if the ‘key’ option was set
562 563 564 |
# File 'lib/opt_simple.rb', line 562 def include?(key) @inside_hash.include?(@name_to_aliases[key]) end |
#merge(other) ⇒ Object
merge non-destructively, and return the Result
547 548 549 550 551 |
# File 'lib/opt_simple.rb', line 547 def merge(other) r = Result.new r.merge! self r.merge! other end |
#merge!(other) ⇒ Object
merge into this Result and return the Result
554 555 556 557 558 559 |
# File 'lib/opt_simple.rb', line 554 def merge!(other) other.aliases.each do | a | add_alias(a) self[a.first] = other[a.first] end end |
#to_s ⇒ Object
a hash looking return string.
585 586 587 588 589 590 591 |
# File 'lib/opt_simple.rb', line 585 def to_s ret = "" aliases.each do | a | ret << "#{a}=>#{@inside_hash[a]}," end return "{#{ret[0..-2]}}\n#{@positional_args.inspect}\n" end |