Class: Mercenary::Option
- Inherits:
-
Object
- Object
- Mercenary::Option
- Defined in:
- lib/mercenary/option.rb
Instance Attribute Summary collapse
-
#config_key ⇒ Object
readonly
Returns the value of attribute config_key.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#long ⇒ Object
readonly
Returns the value of attribute long.
-
#return_type ⇒ Object
readonly
Returns the value of attribute return_type.
-
#short ⇒ Object
readonly
Returns the value of attribute short.
Instance Method Summary collapse
-
#eql?(other) ⇒ Boolean
Public: Check equivalence of two Options based on equivalence of their instance variables.
-
#for_option_parser ⇒ Object
Public: Fetch the array containing the info OptionParser is interested in.
-
#formatted_switches ⇒ Object
Public: Build a beautifully-formatted string representation of the switches.
-
#hash ⇒ Object
Public: Hash based on the hash value of instance variables.
-
#initialize(config_key, info) ⇒ Option
constructor
Public: Create a new Option.
-
#switches ⇒ Object
Public: Fetch an array of switches, including the short and long versions.
-
#to_s ⇒ Object
Public: Build a string representation of this option including the switches and description.
Constructor Details
#initialize(config_key, info) ⇒ Option
Public: Create a new Option
config_key - the key in the config hash to which the value of this option
will map
info - an array containing first the switches, then an optional
return type (e.g. Array), then a description of the option
Returns nothing
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/mercenary/option.rb', line 15 def initialize(config_key, info) @config_key = config_key while arg = info.shift begin @return_type = Object.const_get(arg.to_s) next rescue NameError end if arg.start_with?("-") if arg.start_with?("--") @long = arg else @short = arg end next end @description = arg end end |
Instance Attribute Details
#config_key ⇒ Object (readonly)
Returns the value of attribute config_key.
5 6 7 |
# File 'lib/mercenary/option.rb', line 5 def config_key @config_key end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
5 6 7 |
# File 'lib/mercenary/option.rb', line 5 def description @description end |
#long ⇒ Object (readonly)
Returns the value of attribute long.
5 6 7 |
# File 'lib/mercenary/option.rb', line 5 def long @long end |
#return_type ⇒ Object (readonly)
Returns the value of attribute return_type.
5 6 7 |
# File 'lib/mercenary/option.rb', line 5 def return_type @return_type end |
#short ⇒ Object (readonly)
Returns the value of attribute short.
5 6 7 |
# File 'lib/mercenary/option.rb', line 5 def short @short end |
Instance Method Details
#eql?(other) ⇒ Boolean
Public: Check equivalence of two Options based on equivalence of their
instance variables
Returns true if all the instance variables are equal, false otherwise
73 74 75 76 77 78 |
# File 'lib/mercenary/option.rb', line 73 def eql?(other) return false unless self.class.eql?(other.class) instance_variables.map do |var| instance_variable_get(var).eql?(other.instance_variable_get(var)) end.all? end |
#for_option_parser ⇒ Object
Public: Fetch the array containing the info OptionParser is interested in
Returns the array which OptionParser#on wants
38 39 40 |
# File 'lib/mercenary/option.rb', line 38 def for_option_parser [short, long, return_type, description].flatten.reject { |o| o.to_s.empty? } end |
#formatted_switches ⇒ Object
Public: Build a beautifully-formatted string representation of the switches
Returns a formatted string representation of the switches
53 54 55 56 57 58 |
# File 'lib/mercenary/option.rb', line 53 def formatted_switches [ switches.first.rjust(10), switches.last.ljust(13), ].join(", ").gsub(%r! , !, " ").gsub(%r!, !, " ") end |
#hash ⇒ Object
Public: Hash based on the hash value of instance variables
Returns a Fixnum which is unique to this Option based on the instance variables
63 64 65 66 67 |
# File 'lib/mercenary/option.rb', line 63 def hash instance_variables.map do |var| instance_variable_get(var).hash end.reduce(:^) end |
#switches ⇒ Object
Public: Fetch an array of switches, including the short and long versions
Returns an array of two strings. An empty string represents no switch in that position.
84 85 86 |
# File 'lib/mercenary/option.rb', line 84 def switches [short, long].map(&:to_s) end |
#to_s ⇒ Object
Public: Build a string representation of this option including the
switches and description
Returns a string representation of this option
46 47 48 |
# File 'lib/mercenary/option.rb', line 46 def to_s "#{formatted_switches} #{description}" end |