Module: EacCli::RunnerWith::Subcommands

Defined in:
lib/eac_cli/runner_with/subcommands.rb,
lib/eac_cli/runner_with/subcommands/definition_concern.rb

Defined Under Namespace

Modules: DefinitionConcern

Constant Summary collapse

EXTRA_AVAILABLE_SUBCOMMANDS_METHOD_NAME =
:extra_available_subcommands

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object



49
50
51
52
53
54
# File 'lib/eac_cli/runner_with/subcommands.rb', line 49

def method_missing(method_name, *arguments, &block)
  return run_with_subcommand(*arguments, &block) if
  run_with_subcommand_alias_run?(method_name)

  super
end

Class Method Details

.runner?(object) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/eac_cli/runner_with/subcommands.rb', line 12

def runner?(object)
  ::EacCli::Runner.runner?(object)
end

Instance Method Details

#available_subcommandsObject



25
26
27
# File 'lib/eac_cli/runner_with/subcommands.rb', line 25

def available_subcommands
  @available_subcommands ||= available_subcommands_auto.merge(available_subcommands_extra)
end

#available_subcommands_autoObject



29
30
31
32
33
34
# File 'lib/eac_cli/runner_with/subcommands.rb', line 29

def available_subcommands_auto
  self.class.constants
      .map { |name| [name.to_s.underscore.gsub('_', '-'), self.class.const_get(name)] }
      .select { |c| ::EacCli::RunnerWith::Subcommands.runner?(c[1]) }
      .to_h.with_indifferent_access
end

#available_subcommands_extraObject



36
37
38
39
40
41
42
# File 'lib/eac_cli/runner_with/subcommands.rb', line 36

def available_subcommands_extra
  if respond_to?(EXTRA_AVAILABLE_SUBCOMMANDS_METHOD_NAME, true)
    send(EXTRA_AVAILABLE_SUBCOMMANDS_METHOD_NAME)
  else
    {}
  end
end

#help_extra_textObject



44
45
46
47
# File 'lib/eac_cli/runner_with/subcommands.rb', line 44

def help_extra_text
  (['Subcommands:'] + available_subcommands.keys.sort.map { |s| "  #{s}" })
    .map { |v| "#{v}\n" }.join
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/eac_cli/runner_with/subcommands.rb', line 56

def respond_to_missing?(method_name, include_private = false)
  run_with_subcommand_alias_run?(method_name) || super
end

#run_subcommand?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/eac_cli/runner_with/subcommands.rb', line 80

def run_subcommand?
  subcommand_name.present?
end

#run_with_subcommandObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/eac_cli/runner_with/subcommands.rb', line 60

def run_with_subcommand
  if run_subcommand?
    if subcommand_runner.respond_to?(:run_run)
      subcommand_runner.run_run
    else
      subcommand_runner.run
    end
  else
    run_without_subcommand
  end
end

#run_with_subcommand_alias_run?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/eac_cli/runner_with/subcommands.rb', line 72

def run_with_subcommand_alias_run?(method_name)
  subcommands? && method_name.to_sym == :run
end

#run_without_subcommandObject



76
77
78
# File 'lib/eac_cli/runner_with/subcommands.rb', line 76

def run_without_subcommand
  "Method #{__method__} should be overrided in #{self.class.name}"
end

#subcommand_argsObject



88
89
90
# File 'lib/eac_cli/runner_with/subcommands.rb', line 88

def subcommand_args
  parsed.fetch(::EacCli::Definition::SUBCOMMAND_ARGS_ARG)
end

#subcommand_classObject



92
93
94
95
96
97
98
99
100
# File 'lib/eac_cli/runner_with/subcommands.rb', line 92

def subcommand_class
  available_subcommands[subcommand_name].if_present { |v| return v }

  raise(::EacCli::Parser::Error.new(
          self.class.runner_definition, runner_context.argv,
          "Subcommand \"#{subcommand_name}\" not found " \
            "(Available: #{available_subcommands.keys}"
        ))
end

#subcommand_nameObject



102
103
104
# File 'lib/eac_cli/runner_with/subcommands.rb', line 102

def subcommand_name
  parsed.fetch(::EacCli::Definition::SUBCOMMAND_NAME_ARG)
end

#subcommand_programObject



106
107
108
# File 'lib/eac_cli/runner_with/subcommands.rb', line 106

def subcommand_program
  subcommand_name
end

#subcommand_runnerObject



110
111
112
113
114
115
116
# File 'lib/eac_cli/runner_with/subcommands.rb', line 110

def subcommand_runner
  @subcommand_runner ||= subcommand_class.create(
    argv: subcommand_args,
    program_name: subcommand_program,
    parent: self
  )
end

#subcommands?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/eac_cli/runner_with/subcommands.rb', line 84

def subcommands?
  self.class.runner_definition.subcommands?
end