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



44
45
46
47
48
49
50
# File 'lib/fuelcell/action/opts_manager.rb', line 44

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)


21
22
23
24
25
# File 'lib/fuelcell/action/opts_manager.rb', line 21

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

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



75
76
77
# File 'lib/fuelcell/action/opts_manager.rb', line 75

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, []



65
66
67
68
69
70
71
# File 'lib/fuelcell/action/opts_manager.rb', line 65

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

#globalsObject



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

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

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

Yields:

  • (list)


35
36
37
38
39
40
41
42
# File 'lib/fuelcell/action/opts_manager.rb', line 35

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



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fuelcell/action/opts_manager.rb', line 53

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



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

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