Class: QB::Options::Option
- Inherits:
-
Object
- Object
- QB::Options::Option
- Includes:
- NRSER::Log::Mixin, OptionParserConcern
- Defined in:
- lib/qb/options/option.rb,
lib/qb/options/option/option_parser_concern.rb
Overview
Definitions
Defined Under Namespace
Modules: OptionParserConcern
Constant Summary collapse
- EXAMPLES_KEYS =
Constants
['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_name ⇒ Object
readonly
the name of the option in the qb metadata, equal to #meta['name'].
-
#role ⇒ Object
readonly
the role that this option is for.
-
#type ⇒ attr_type
readonly
TODO document
type
attribute. -
#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
-
#accept_false? ⇒ Boolean
Does the option accept
false
as value?. - #boolean? ⇒ Boolean
-
#description ⇒ String
Description of the option.
-
#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
Construction ======================================================================.
-
#meta(*keys, type: t.any, default: nil) ⇒ Object
Instance Methods ========================================================================.
- #meta?(*keys) ⇒ Boolean
-
#required? ⇒ Boolean
Is the option is required in the CLI?.
-
#save? ⇒ Boolean
Should we save the option value in
./.qb-options.yml
?. - #usage ⇒ Object
- #value_data ⇒ Object
Methods included from OptionParserConcern
#option_parser_add, #option_parser_args, #option_parser_bool_args, #option_parser_default, #option_parser_description, #option_parser_examples, #option_parser_format_multiline, #option_parser_non_bool_args, #option_parser_spacer, #option_parser_type, #option_parser_type_acceptable, #option_parser_value_name
Constructor Details
#initialize(role, meta, include_path) ⇒ Option
Construction
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/qb/options/option.rb', line 92 def initialize role, , include_path @role = role @meta = .with_indifferent_access @include_path = include_path @meta_name = .fetch 'name' @cli_name = if @include_path.empty? QB::Options.cli_ize_name @meta_name else QB::Options.cli_ize_name "#{ @include_path.join('-') }-#{ @meta_name }" end @var_name = if self.( :var_name ) # prefer an explicit, exact variable name if provided self.( :var_name, type: Types.var_name ) elsif role.var_prefix QB::Options.var_ize_name "#{ role.var_prefix }_#{ }" else QB::Options.var_ize_name end # Will be set when we find it out! @value = nil # Initialize `@type` var init_type! end |
Instance Attribute Details
#cli_name ⇒ Object (readonly)
the name that this option will be available in the cli as
73 74 75 |
# File 'lib/qb/options/option.rb', line 73 def cli_name @cli_name end |
#include_path ⇒ Object (readonly)
array of strings representing how this option was included empty for top-level options
67 68 69 |
# File 'lib/qb/options/option.rb', line 67 def include_path @include_path end |
#meta_name ⇒ Object (readonly)
the name of the option in the qb metadata, equal to #meta['name']
70 71 72 |
# File 'lib/qb/options/option.rb', line 70 def @meta_name end |
#role ⇒ Object (readonly)
the role that this option is for
63 64 65 |
# File 'lib/qb/options/option.rb', line 63 def role @role end |
#type ⇒ attr_type (readonly)
TODO document type
attribute.
86 87 88 |
# File 'lib/qb/options/option.rb', line 86 def type @type end |
#value ⇒ Object
the value of the option, or nil
if we never assign one
79 80 81 |
# File 'lib/qb/options/option.rb', line 79 def value @value end |
#var_name ⇒ Object (readonly)
the name that the value will be passed to ansible as
76 77 78 |
# File 'lib/qb/options/option.rb', line 76 def var_name @var_name end |
Instance Method Details
#accept_false? ⇒ Boolean
Does the option accept false
as value?
If it does, and is not a boolean option, we also accept a --no-<name>
option format to set the value to false
.
This is useful to explicitly tell QB "no, I don't want this", since we
treat nil
/null
as the same as absent, which will cause a
default value to be used (if available).
This feature does not apply to #boolean? options themselves, only options
that accept other values (though this method will of course return true
for #boolean? options, since they do accept false
).
305 306 307 308 309 310 311 |
# File 'lib/qb/options/option.rb', line 305 def accept_false? return true if [:accept_false] return false if type.is_a?( Class ) && type < NRSER::Props type.test?( false ) end |
#boolean? ⇒ Boolean
258 259 260 |
# File 'lib/qb/options/option.rb', line 258 def boolean? type == t.bool end |
#description ⇒ String
Description of the option.
250 251 252 253 254 255 |
# File 'lib/qb/options/option.rb', line 250 def description ( :description, default: "Set the #{ @var_name } role variable" ).to_s end |
#examples ⇒ Array<String>
get an array of examples for the option. returns []
if no examples
are defined.
285 286 287 |
# File 'lib/qb/options/option.rb', line 285 def examples Array ( *EXAMPLES_KEYS, type: (t.nil | t.str | t.array( t.str )) ) end |
#has_examples? ⇒ Boolean
test if the option has any examples.
276 277 278 |
# File 'lib/qb/options/option.rb', line 276 def has_examples? *EXAMPLES_KEYS end |
#meta(*keys, type: t.any, default: nil) ⇒ Object
Instance Methods
203 204 205 206 207 208 209 210 211 |
# File 'lib/qb/options/option.rb', line 203 def *keys, type: t.any, default: nil return @meta if keys.empty? keys.each do |key| return type.check!( @meta[key] ) unless @meta[key].nil? end type.check! default end |
#meta?(*keys) ⇒ Boolean
214 215 216 |
# File 'lib/qb/options/option.rb', line 214 def *keys keys.any? { |key| @meta.key? key } end |
#required? ⇒ Boolean
Is the option is required in the CLI?
232 233 234 |
# File 'lib/qb/options/option.rb', line 232 def required? :required, :require, type: t.bool, default: false end |
#save? ⇒ Boolean
Should we save the option value in ./.qb-options.yml
?
241 242 243 |
# File 'lib/qb/options/option.rb', line 241 def save? :save, type: t.bool, default: true end |
#usage ⇒ Object
263 264 265 266 267 268 269 |
# File 'lib/qb/options/option.rb', line 263 def usage if boolean? "--[no-]#{ cli_name }" else "--#{ cli_name }=#{ .upcase }" end end |
#value_data ⇒ Object
219 220 221 222 223 224 225 |
# File 'lib/qb/options/option.rb', line 219 def value_data if value.respond_to? :to_data value.to_data else value end end |