Class: QB::Options::Option

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

Constant Summary collapse

EXAMPLES_KEYS =
['examples', 'example']

Instance Attribute Summary collapse

Instance Method Summary collapse

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, meta, include_path
  # @role = WeakRef.new role
  @meta = meta
  @include_path = include_path
  
  @meta_name = meta.fetch 'name'
  
  @cli_name = if @include_path.empty?
    Options.cli_ize_name @meta_name
  else
    Options.cli_ize_name "#{ @include_path.join('-') }-#{ @meta_name }"
  end
  
  @var_name = if @meta['var_name']
    # prefer an explicit, exact variable name if provided
    @meta['var_name']
  elsif role.var_prefix
    Options.var_ize_name "#{ role.var_prefix }_#{ @meta_name }"
  else
    Options.var_ize_name @meta_name
  end
  
  @value = nil
end

Instance Attribute Details

#cli_nameObject (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_pathObject (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

#metaObject (readonly)

the entry from the qb metadata for this option



10
11
12
# File 'lib/qb/options/option.rb', line 10

def meta
  @meta
end

#meta_nameObject (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 meta_name
  @meta_name
end

#valueObject

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_nameObject (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

Returns:

  • (Boolean)


77
78
79
80
81
82
# File 'lib/qb/options/option.rb', line 77

def boolean?
  (
    meta['type'].is_a?(String) &&
    ['boolean', 'bool'].include?(meta['type'].downcase)
  )
end

#descriptionObject



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/qb/options/option.rb', line 63

def description
  value = meta_or 'description',
    "set the #{ @var_name } role variable"
  
  line_break = "\n" + "\t" * 5
    
  if @meta['type'].is_a?(Hash) && @meta['type'].key?('one_of')
    value += " options:" + 
      "#{ line_break }#{ @meta['type']['one_of'].join(line_break) }"
  end
  
  value
end

#examplesArray<String>

get an array of examples for the option. returns [] if no examples are defined.

Returns:

  • (Array<String>)


105
106
107
108
109
# File 'lib/qb/options/option.rb', line 105

def examples
  value = meta_or EXAMPLES_KEYS, []
  
  if value.is_a? String then [value] else value end
end

#has_examples?Boolean

test if the option has any examples.

Returns:

  • (Boolean)


96
97
98
# File 'lib/qb/options/option.rb', line 96

def has_examples?
  EXAMPLES_KEYS.any? {|key| meta.key? key}
end

#required?Boolean

if the option is required in the cli

Returns:

  • (Boolean)


54
55
56
# File 'lib/qb/options/option.rb', line 54

def required?
  !!meta_or(['required', 'require'], false)
end

#save?Boolean

if we should save the option value in .qb-options.yml

Returns:

  • (Boolean)


59
60
61
# File 'lib/qb/options/option.rb', line 59

def save?
  !!meta_or('save', true)
end

#usageObject



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