Class: Karafka::Cli::Base
- Inherits:
-
Object
- Object
- Karafka::Cli::Base
- Includes:
- Thor::Shell
- Defined in:
- lib/karafka/cli/base.rb
Overview
Base class for all the command that we want to define This base class provides a nicer interface to Thor and allows to easier separate single independent commands In order to define a new command you need to:
- specify its desc
- implement call method
Instance Attribute Summary collapse
-
#cli ⇒ Object
readonly
We can use it to call other cli methods via this object.
Class Method Summary collapse
-
.bind_to(cli_class) ⇒ Object
This method will bind a given Cli command into Karafka Cli This method is a wrapper to way Thor defines its commands.
-
.desc(desc) ⇒ Object
Allows to set description of a given cli command.
-
.option(*option) ⇒ Object
Allows to set options for Thor cli.
Instance Method Summary collapse
-
#call ⇒ Object
This method should implement proper cli action.
-
#initialize(cli) ⇒ Base
constructor
A new instance of Base.
Constructor Details
#initialize(cli) ⇒ Base
Returns a new instance of Base.
27 28 29 |
# File 'lib/karafka/cli/base.rb', line 27 def initialize(cli) @cli = cli end |
Instance Attribute Details
#cli ⇒ Object (readonly)
We can use it to call other cli methods via this object
24 25 26 |
# File 'lib/karafka/cli/base.rb', line 24 def cli @cli end |
Class Method Details
.bind_to(cli_class) ⇒ Object
This method will bind a given Cli command into Karafka Cli This method is a wrapper to way Thor defines its commands
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/karafka/cli/base.rb', line 54 def bind_to(cli_class) cli_class.desc name, @desc (@options || []).each { |option| cli_class.option(*option) } context = self cli_class.send :define_method, name do |*args| context.new(self).call(*args) end end |
.desc(desc) ⇒ Object
Allows to set description of a given cli command
47 48 49 |
# File 'lib/karafka/cli/base.rb', line 47 def desc(desc) @desc ||= desc end |
.option(*option) ⇒ Object
Allows to set options for Thor cli
40 41 42 43 |
# File 'lib/karafka/cli/base.rb', line 40 def option(*option) @options ||= [] @options << option end |
Instance Method Details
#call ⇒ Object
This method should implement proper cli action
32 33 34 |
# File 'lib/karafka/cli/base.rb', line 32 def call raise NotImplementedError, 'Implement this in a subclass' end |