Class: Fuelcell::Action::OptsManager
- Inherits:
-
Object
- Object
- Fuelcell::Action::OptsManager
- 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
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #add(option, config = {}) ⇒ Object (also: #opt)
-
#callable ⇒ Hash
Check to determine if any of the options are callable, meaning do they point to another command or have a lambda to be executed.
- #create(name, config = {}) ⇒ Object
- #each ⇒ Object
- #find(name) ⇒ Object (also: #find_opt, #[])
- #globals ⇒ Object
-
#initialize ⇒ OptsManager
constructor
A new instance of OptsManager.
- #missing_opts(names) {|list| ... } ⇒ Object
- #remove(name) ⇒ Object
- #required_opts ⇒ Object
Constructor Details
#initialize ⇒ OptsManager
Returns a new instance of OptsManager.
9 10 11 |
# File 'lib/fuelcell/action/opts_manager.rb', line 9 def initialize = {} end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/fuelcell/action/opts_manager.rb', line 7 def 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 .key?(option.name) fail "can not add option: duplicate exists with name #{option.name}" end [option.name] = option end |
#callable ⇒ Hash
Check to determine if any of the options are callable, meaning do they point to another command or have a lambda to be executed
21 22 23 24 25 |
# File 'lib/fuelcell/action/opts_manager.rb', line 21 def callable list = .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 |
#each ⇒ Object
13 14 15 |
# File 'lib/fuelcell/action/opts_manager.rb', line 13 def each .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 .each do |(_, option)| target = option if option.name?(name) end target end |
#globals ⇒ Object
31 32 33 |
# File 'lib/fuelcell/action/opts_manager.rb', line 31 def globals .select { |_, opt| opt.global? } end |
#missing_opts(names) {|list| ... } ⇒ Object
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 = .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) && .key?(name.name) return .delete(name.name) end opt = find(name) return false unless opt .delete(opt.name) end |
#required_opts ⇒ Object
27 28 29 |
# File 'lib/fuelcell/action/opts_manager.rb', line 27 def required_opts .select { |_, opt| opt.required? } end |