Class: Eco::CLI::Config::OptionsSet
- Inherits:
-
Object
- Object
- Eco::CLI::Config::OptionsSet
- Includes:
- Help
- Defined in:
- lib/eco/cli/config/options_set.rb
Defined Under Namespace
Classes: OptConfig
Instance Attribute Summary collapse
-
#core_config ⇒ Object
readonly
Returns the value of attribute core_config.
Instance Method Summary collapse
- #active_namespaces ⇒ Object
- #active_options ⇒ Object
- #add(option, desc = nil, namespace: :general) ⇒ Object
- #all_options ⇒ Object
- #any_non_general_space_active? ⇒ Boolean
-
#help(refine: nil) ⇒ String
Summary of the options.
-
#initialize(core_config:) ⇒ OptionsSet
constructor
A new instance of OptionsSet.
-
#options_args(namespaces) ⇒ Array<String>
All the argument of the options in
namespaces
. - #process(io:) ⇒ Object
Constructor Details
#initialize(core_config:) ⇒ OptionsSet
Returns a new instance of OptionsSet.
11 12 13 14 |
# File 'lib/eco/cli/config/options_set.rb', line 11 def initialize(core_config:) @core_config = core_config @sets = {} end |
Instance Attribute Details
#core_config ⇒ Object (readonly)
Returns the value of attribute core_config.
6 7 8 |
# File 'lib/eco/cli/config/options_set.rb', line 6 def core_config @core_config end |
Instance Method Details
#active_namespaces ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/eco/cli/config/options_set.rb', line 99 def active_namespaces @active_namespaces ||= [].tap do |active| active << :general other = (namespaces - [:general]).select {|nm| SCR.arg?(nm)} active.concat(other) end end |
#active_options ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/eco/cli/config/options_set.rb', line 73 def @active_options ||= sets.select do |namespace, opts_set| active_namespace?(namespace) end.each_with_object([]) do |(namespace, opts_set), | opts_set.each do |arg, option| << option if active_option?(arg, namespace) end end end |
#add(option, desc = nil, namespace: :general) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/eco/cli/config/options_set.rb', line 47 def add(option, desc = nil, namespace: :general) raise "Missing block to define the options builder" unless block_given? opts = [option].flatten.compact unless opts.empty? callback = Proc.new opts.each do |opt| puts "Overriding option '#{option}' in '#{namespace}' namespace" if option_exists?(opt, namespace) (namespace)[opt] = OptConfig.new(opt, namespace, desc, callback) end end self end |
#all_options ⇒ Object
83 84 85 86 87 |
# File 'lib/eco/cli/config/options_set.rb', line 83 def sets.each_with_object([]) do |(namespace, opts_set), | << opts_set.values end end |
#any_non_general_space_active? ⇒ Boolean
95 96 97 |
# File 'lib/eco/cli/config/options_set.rb', line 95 def any_non_general_space_active? (active_namespaces - [:general]).length > 0 end |
#help(refine: nil) ⇒ String
Returns summary of the options.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/eco/cli/config/options_set.rb', line 17 def help(refine: nil) indent = 2 spaces = any_non_general_space_active? ? active_namespaces : namespaces refinement = refine.is_a?(String)? " (containing: '#{refine}')" : "" ["The following are the available options#{refinement}:"].yield_self do |lines| max_len = keys_max_len((spaces)) + indent spaces.each do |namespace| is_general = (namespace == :general) str_indent = is_general ? "" : " " * indent lines << help_line(namespace, "", max_len) unless is_general (namespace).select do |arg, option| !refine.is_a?(String) || option.name.include?(refine) end.each do |arg, option| lines << help_line(" " * indent + "#{option.name}", option.description, max_len) end end lines end.join("\n") end |
#options_args(namespaces) ⇒ Array<String>
Returns all the argument of the options in namespaces
.
38 39 40 41 42 |
# File 'lib/eco/cli/config/options_set.rb', line 38 def (namespaces) namespaces.each_with_object([]) do |space, args| args.concat((space).keys) end.uniq end |
#process(io:) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/eco/cli/config/options_set.rb', line 61 def process(io:) unless io && io.is_a?(Eco::API::UseCases::BaseIO) raise "You need to provide Eco::API::UseCases::BaseIO object. Given: #{io.class}" end .each do |option| option.callback.call(io., io.session) end io. end |