Class: Eco::CLI::Config::UseCases

Inherits:
Object
  • Object
show all
Includes:
Help
Defined in:
lib/eco/cli/config/use_cases.rb

Defined Under Namespace

Classes: ActiveCase, CaseConfig

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(core_config:) ⇒ UseCases

Returns a new instance of UseCases.



23
24
25
26
# File 'lib/eco/cli/config/use_cases.rb', line 23

def initialize(core_config:)
  @core_config  = core_config
  @linked_cases = {}
end

Instance Attribute Details

#core_configObject (readonly)

Returns the value of attribute core_config.



6
7
8
# File 'lib/eco/cli/config/use_cases.rb', line 6

def core_config
  @core_config
end

Instance Method Details

#active(io:) ⇒ Hash

Note:
  • this method will sort the active usecases by the position they hold in the command line

Scopes/identifies which usecases are being invoked from the command line

Parameters:

Returns:

  • (Hash)

    where keys are cases and values a Hash with option String and callback



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/eco/cli/config/use_cases.rb', line 72

def active(io:)
  validate_io!(io)
  return @active_cases if @active_cases

  @active_cases = @linked_cases.each_with_object({}) do |(option_case, data), active_cases|
    next unless SCR.get_arg(option_case)
    next unless (usecase = get_usecase(io: io, data: data))

    index = SCR.get_arg_index(option_case)
    active_cases[usecase] = ActiveCase.new(index, option_case, data.callback)
  end.sort_by {|_c, d| d.index}.to_h
end

#add(option_case, type, desc = nil, case_name: nil, &callback) ⇒ Object

Integrates a usecase to the command line.

Parameters:

  • option_case (String)

    the command line option to invoke the usecase.

  • type (Symbol)

    the type of usecase.

  • desc (String) (defaults to: nil)

    description of the case.

  • case_name (String, nil) (defaults to: nil)

    the name of the usecase as defined.



47
48
49
50
51
52
53
54
55
# File 'lib/eco/cli/config/use_cases.rb', line 47

def add(option_case, type, desc = nil, case_name: nil, &callback)
  Eco::API::UseCases::UseCase.validate_type(type)
  unless block_given?
    raise "You must specify a valid 'case_name' when no block is provided" unless case_name
    raise "'case_name' expected to be a String. Given: #{case_name.class}" unless case_name.is_a?(String)
  end
  puts "Overriding CLI case '#{option_case}'" if @linked_cases.key?(option_case)
  @linked_cases[option_case] = CaseConfig.new(self, option_case, type, desc, case_name, callback)
end

#cli_apply(io:) ⇒ Object

Note:

it only applies to use cases that have been defined via class

This method runs on use cases that have lazy configurations



59
60
61
62
63
64
65
# File 'lib/eco/cli/config/use_cases.rb', line 59

def cli_apply(io:)
  io.session.usecases.each do |usecase|
    next unless usecase.respond_to?(:classed_definition)
    next unless (original_case = usecase.classed_definition)
    original_case.cli_apply!
  end
end

#help(refine: nil) ⇒ String

Returns summary of the use cases.

Returns:

  • (String)

    summary of the use cases.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/eco/cli/config/use_cases.rb', line 29

def help(refine: nil)
  refinement = refine.is_a?(String)? " (containing: '#{refine}')" : ""
  ["The following are the available use cases#{refinement}:"].then do |lines|
    max_len = keys_max_len(@linked_cases.keys)
    @linked_cases.keys.sort.select do |key|
      !refine.is_a?(String) || key.include?(refine)
    end.each do |option_case|
      lines << help_line(option_case, @linked_cases[option_case].description, max_len)
    end
    lines
  end.join("\n")
end

#process(io:) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/eco/cli/config/use_cases.rb', line 85

def process(io:)
  validate_io!(io)
  processed = false
  active(io: io).each do |usecase, data|
    raise "Something went wrong when scoping active cases" unless data
    processed = true
    io = case_io(io: io, usecase: usecase)
    # some usecases have a callback to collect the parameters
    data.callback&.call(*io.params)
    io = usecase.launch(io: io)
  end
  processed
end