Class: QB::Options::Option
- Inherits:
-
Object
- Object
- QB::Options::Option
- Defined in:
- lib/qb/options/option.rb
Constant Summary collapse
- EXAMPLES_KEYS =
['examples', 'example']
Instance Attribute Summary collapse
-
#cli_name ⇒ Object
readonly
the name that this option will be available in the cli as.
-
#include_path ⇒ Object
readonly
array of strings representing how this option was included empty for top-level options.
-
#meta ⇒ Object
readonly
the entry from the qb metadata for this option.
-
#meta_name ⇒ Object
readonly
the name of the option in the qb metadata, equal to #meta['name'].
-
#value ⇒ Object
the value of the option, or
nil
if we never assign one. -
#var_name ⇒ Object
readonly
the name that the value will be passed to ansible as.
Instance Method Summary collapse
- #boolean? ⇒ Boolean
- #description ⇒ Object
-
#examples ⇒ Array<String>
get an array of examples for the option.
-
#has_examples? ⇒ Boolean
test if the option has any examples.
-
#initialize(role, meta, include_path) ⇒ Option
constructor
A new instance of Option.
-
#required? ⇒ Boolean
if the option is required in the cli.
-
#save? ⇒ Boolean
if we should save the option value in .qb-options.yml.
- #usage ⇒ Object
Constructor Details
#initialize(role, meta, include_path) ⇒ Option
Returns a new instance of Option.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/qb/options/option.rb', line 28 def initialize role, , include_path # @role = WeakRef.new role = @include_path = include_path = .fetch 'name' @cli_name = if @include_path.empty? Options.cli_ize_name else Options.cli_ize_name "#{ @include_path.join('-') }-#{ @meta_name }" end @var_name = if ['var_name'] # prefer an explicit, exact variable name if provided ['var_name'] elsif role.var_prefix Options.var_ize_name "#{ role.var_prefix }_#{ @meta_name }" else Options.var_ize_name end @value = nil end |
Instance Attribute Details
#cli_name ⇒ Object (readonly)
the name that this option will be available in the cli as
20 21 22 |
# File 'lib/qb/options/option.rb', line 20 def cli_name @cli_name end |
#include_path ⇒ Object (readonly)
array of strings representing how this option was included empty for top-level options
14 15 16 |
# File 'lib/qb/options/option.rb', line 14 def include_path @include_path end |
#meta ⇒ Object (readonly)
the entry from the qb metadata for this option
10 11 12 |
# File 'lib/qb/options/option.rb', line 10 def end |
#meta_name ⇒ Object (readonly)
the name of the option in the qb metadata, equal to #meta['name']
17 18 19 |
# File 'lib/qb/options/option.rb', line 17 def end |
#value ⇒ Object
the value of the option, or nil
if we never assign one
26 27 28 |
# File 'lib/qb/options/option.rb', line 26 def value @value end |
#var_name ⇒ Object (readonly)
the name that the value will be passed to ansible as
23 24 25 |
# File 'lib/qb/options/option.rb', line 23 def var_name @var_name end |
Instance Method Details
#boolean? ⇒ Boolean
77 78 79 80 81 82 |
# File 'lib/qb/options/option.rb', line 77 def boolean? ( ['type'].is_a?(String) && ['boolean', 'bool'].include?(['type'].downcase) ) end |
#description ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/qb/options/option.rb', line 63 def description value = 'description', "set the #{ @var_name } role variable" line_break = "\n" + "\t" * 5 if ['type'].is_a?(Hash) && ['type'].key?('one_of') value += " options:" + "#{ line_break }#{ @meta['type']['one_of'].join(line_break) }" end value end |
#examples ⇒ Array<String>
get an array of examples for the option. returns []
if no examples
are defined.
105 106 107 108 109 |
# File 'lib/qb/options/option.rb', line 105 def examples value = EXAMPLES_KEYS, [] if value.is_a? String then [value] else value end end |
#has_examples? ⇒ Boolean
test if the option has any examples.
96 97 98 |
# File 'lib/qb/options/option.rb', line 96 def has_examples? EXAMPLES_KEYS.any? {|key| .key? key} end |
#required? ⇒ Boolean
if the option is required in the cli
54 55 56 |
# File 'lib/qb/options/option.rb', line 54 def required? !!(['required', 'require'], false) end |
#save? ⇒ Boolean
if we should save the option value in .qb-options.yml
59 60 61 |
# File 'lib/qb/options/option.rb', line 59 def save? !!('save', true) end |
#usage ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/qb/options/option.rb', line 84 def usage if boolean? "--[no-]#{ cli_name }" else "--#{ cli_name }=#{ meta_name.upcase }" end end |