Class: Karafka::Cli::Base
- Inherits:
-
Object
- Object
- Karafka::Cli::Base
- 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.
23 24 25 |
# File 'lib/karafka/cli/base.rb', line 23 def initialize(cli) @cli = cli end |
Instance Attribute Details
#cli ⇒ Object (readonly)
We can use it to call other cli methods via this object
20 21 22 |
# File 'lib/karafka/cli/base.rb', line 20 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
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/karafka/cli/base.rb', line 50 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
43 44 45 |
# File 'lib/karafka/cli/base.rb', line 43 def desc(desc) @desc ||= desc end |
.option(*option) ⇒ Object
Allows to set options for Thor cli
36 37 38 39 |
# File 'lib/karafka/cli/base.rb', line 36 def option(*option) @options ||= [] @options << option end |
Instance Method Details
#call ⇒ Object
This method should implement proper cli action
28 29 30 |
# File 'lib/karafka/cli/base.rb', line 28 def call raise NotImplementedError, 'Implement this in a subclass' end |