Class: Fuelcell::Action::OptsManager

Inherits:
Object
  • Object
show all
Defined in:
lib/fuelcell/action/opts_manager.rb

Overview

Used by the Command to manage adding option from its dsl. It is also used during option parsing to find options, add global options, or check if any required options have been missed

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptsManager

Returns a new instance of OptsManager.



9
10
11
# File 'lib/fuelcell/action/opts_manager.rb', line 9

def initialize
  @options = {}
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/fuelcell/action/opts_manager.rb', line 7

def options
  @options
end

Instance Method Details

#add(option, config = {}) ⇒ Object Also known as: opt



48
49
50
51
52
53
54
# File 'lib/fuelcell/action/opts_manager.rb', line 48

def add(option, config = {})
  option = create(option, config) if option.is_a?(String)
  if options.key?(option.name)
    fail "can not add option: duplicate exists with name #{option.name}"
  end
  options[option.name] = option
end

#callableHash

Check to determine if any of the options are callable, meaning do they point to another command or have a lambda to be executed

Returns:

  • (Hash)


25
26
27
28
29
# File 'lib/fuelcell/action/opts_manager.rb', line 25

def callable
  list = options.select { |_, opt| opt.callable? || opt.cmd_path? }
  _, value = list.first
  value
end

#callable?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/fuelcell/action/opts_manager.rb', line 17

def callable?
  !callable.nil?
end

#create(name, config = {}) ⇒ Object



79
80
81
# File 'lib/fuelcell/action/opts_manager.rb', line 79

def create(name, config = {})
  OptDefinition.new(name, config)
end

#eachObject



13
14
15
# File 'lib/fuelcell/action/opts_manager.rb', line 13

def each
  options.each {|_, option| yield option }
end

#find(name) ⇒ Object Also known as: find_opt, []



69
70
71
72
73
74
75
# File 'lib/fuelcell/action/opts_manager.rb', line 69

def find(name)
  target = false
  options.each do |(_, option)|
    target = option if option.name?(name)
  end
  target
end

#globalsObject



35
36
37
# File 'lib/fuelcell/action/opts_manager.rb', line 35

def globals
  options.select { |_, opt| opt.global? }
end

#missing_opts(names) {|list| ... } ⇒ Object

Yields:

  • (list)


39
40
41
42
43
44
45
46
# File 'lib/fuelcell/action/opts_manager.rb', line 39

def missing_opts(names)
  missing = required_opts.keys - names
  return [] if missing.empty?

  list = options.select { |(key, _)| missing.include?(key) }.values
  yield list if block_given?
  list
end

#remove(name) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fuelcell/action/opts_manager.rb', line 57

def remove(name)
  # looks like an option definition so lets use it's name
  if name.respond_to?(:name) && options.key?(name.name)
    return options.delete(name.name)
  end

  opt = find(name)
  return false unless opt

  options.delete(opt.name)
end

#required_optsObject



31
32
33
# File 'lib/fuelcell/action/opts_manager.rb', line 31

def required_opts
  options.select { |_, opt| opt.required? }
end