Class: Autoproj::BuildOption
- Inherits:
-
Object
- Object
- Autoproj::BuildOption
- Defined in:
- lib/autoproj/build_option.rb
Overview
Definition of an autoproj option as defined by Configuration#declare
Constant Summary collapse
- TRUE_STRINGS =
%w[on yes y true]
- FALSE_STRINGS =
%w[off no n false]
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#validator ⇒ Object
readonly
Returns the value of attribute validator.
Class Method Summary collapse
Instance Method Summary collapse
-
#ask(current_value, doc = nil) ⇒ Object
Ask the user for the setting of this option by providing the current value as input and falling back to default values if needed.
- #doc ⇒ Object
-
#ensure_value(current_value) ⇒ value, Boolean
Return either the current value if it is not nil, or use a default value.
-
#initialize(name, type, options, validator) ⇒ BuildOption
constructor
A new instance of BuildOption.
- #short_doc ⇒ Object
- #validate(value) ⇒ Object
Constructor Details
#initialize(name, type, options, validator) ⇒ BuildOption
Returns a new instance of BuildOption.
13 14 15 16 17 18 19 20 21 |
# File 'lib/autoproj/build_option.rb', line 13 def initialize(name, type, , validator) @name = name.to_str @type = type.to_str @options = .to_hash @validator = validator.to_proc if validator unless BuildOption.respond_to?("validate_#{type}") raise ConfigError.new, "invalid option type #{type}" end end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/autoproj/build_option.rb', line 5 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/autoproj/build_option.rb', line 7 def @options end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/autoproj/build_option.rb', line 6 def type @type end |
#validator ⇒ Object (readonly)
Returns the value of attribute validator.
9 10 11 |
# File 'lib/autoproj/build_option.rb', line 9 def validator @validator end |
Class Method Details
.validate_boolean(value, options) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/autoproj/build_option.rb', line 92 def self.validate_boolean(value, ) if TRUE_STRINGS.include?(value.downcase) true elsif FALSE_STRINGS.include?(value.downcase) false else raise InputError, "invalid boolean value '#{value}', accepted values are '#{TRUE_STRINGS.join(', ')}' for true, and '#{FALSE_STRINGS.join(', ')} for false" end end |
.validate_string(value, options) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/autoproj/build_option.rb', line 102 def self.validate_string(value, ) if (possible_values = [:possible_values]) if [:lowercase] value = value.downcase elsif [:uppercase] value = value.upcase end unless possible_values.include?(value) raise InputError, "invalid value '#{value}', accepted values are '#{possible_values.join("', '")}' (without the quotes)" end end value end |
Instance Method Details
#ask(current_value, doc = nil) ⇒ Object
Ask the user for the setting of this option by providing the current value as input and falling back to default values if needed
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/autoproj/build_option.rb', line 73 def ask(current_value, doc = nil) value, = ensure_value(current_value) STDOUT.print " #{doc || self.doc} [#{value}] " STDOUT.flush answer = STDIN.readline.chomp answer = value if answer == "" validate(answer) rescue InputError => e Autoproj.("invalid value: #{e.}", :red) retry end |
#doc ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/autoproj/build_option.rb', line 36 def doc doc = ([:doc] || "#{name} (no documentation for this option)") if doc.respond_to?(:to_ary) # multi-line first_line = doc[0] remaining = doc[1..-1] if remaining.empty? first_line else remaining = remaining.join("\n").split("\n").join("\n ") "#{Autoproj.color(first_line, :bold)}\n#{remaining}" end else doc end end |
#ensure_value(current_value) ⇒ value, Boolean
Return either the current value if it is not nil, or use a default value
default value
57 58 59 60 61 62 63 64 65 |
# File 'lib/autoproj/build_option.rb', line 57 def ensure_value(current_value) if !current_value.nil? [current_value.to_s, false] elsif [:default] [[:default].to_str, true] else ["", true] end end |
#short_doc ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/autoproj/build_option.rb', line 23 def short_doc if (short_doc = [:short_doc]) short_doc elsif (doc = [:doc]) if doc.respond_to?(:to_ary) then doc.first else doc end else "#{name} (no documentation for this option)" end end |
#validate(value) ⇒ Object
86 87 88 89 90 |
# File 'lib/autoproj/build_option.rb', line 86 def validate(value) value = BuildOption.send("validate_#{type}", value, ) value = validator[value] if validator value end |