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.
- #callable? ⇒ Boolean
- #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 @options = {} 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 @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 .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
25 26 27 28 29 |
# File 'lib/fuelcell/action/opts_manager.rb', line 25 def callable list = .select { |_, opt| opt.callable? || opt.cmd_path? } _, value = list.first value end |
#callable? ⇒ 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 |
#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, []
69 70 71 72 73 74 75 |
# File 'lib/fuelcell/action/opts_manager.rb', line 69 def find(name) target = false .each do |(_, option)| target = option if option.name?(name) end target end |
#globals ⇒ Object
35 36 37 |
# File 'lib/fuelcell/action/opts_manager.rb', line 35 def globals .select { |_, opt| opt.global? } end |
#missing_opts(names) {|list| ... } ⇒ Object
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 = .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) && .key?(name.name) return .delete(name.name) end opt = find(name) return false unless opt .delete(opt.name) end |
#required_opts ⇒ Object
31 32 33 |
# File 'lib/fuelcell/action/opts_manager.rb', line 31 def required_opts .select { |_, opt| opt.required? } end |