Class: Mercenary::Option

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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 a description of the option

Returns nothing



11
12
13
14
15
# File 'lib/mercenary/option.rb', line 11

def initialize(config_key, info)
  @config_key = config_key
  @description = info.last unless info.last.start_with?("-")
  set_switches(info.take(info.size - [@description].reject(&:nil?).size))
end

Instance Attribute Details

#config_keyObject (readonly)

Returns the value of attribute config_key.



3
4
5
# File 'lib/mercenary/option.rb', line 3

def config_key
  @config_key
end

#descriptionObject (readonly)

Returns the value of attribute description.



3
4
5
# File 'lib/mercenary/option.rb', line 3

def description
  @description
end

#switchesObject (readonly)

Returns the value of attribute switches.



3
4
5
# File 'lib/mercenary/option.rb', line 3

def switches
  @switches
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

Returns:

  • (Boolean)


55
56
57
58
59
60
# File 'lib/mercenary/option.rb', line 55

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_parserObject

Public: Fetch the array containing the info OptionParser is interested in

Returns the array which OptionParser#on wants



20
21
22
# File 'lib/mercenary/option.rb', line 20

def for_option_parser
  [switches.reject(&:empty?), description].reject{ |o| o.nil? || o.empty? }.flatten
end

#formatted_switchesObject

Public: Build a beautifully-formatted string representation of the switches

Returns a formatted string representation of the switches



35
36
37
38
39
40
# File 'lib/mercenary/option.rb', line 35

def formatted_switches
  [
    switches.first.rjust(10),
    switches.last.ljust(13)
  ].join(", ").gsub(/ , /, '   ').gsub(/,   /, '    ')
end

#hashObject

Public: Hash based on the hash value of instance variables

Returns a Fixnum which is unique to this Option based on the instance variables



45
46
47
48
49
# File 'lib/mercenary/option.rb', line 45

def hash
  instance_variables.map do |var|
    instance_variable_get(var).hash
  end.reduce(:&)
end

#to_sObject

Public: Build a string representation of this option including the

switches and description

Returns a string representation of this option



28
29
30
# File 'lib/mercenary/option.rb', line 28

def to_s
  "#{formatted_switches}  #{description}"
end