Module: Hanami::CLI::Registry

Defined in:
lib/hanami/cli/registry.rb

Overview

Registry mixin

Since:

  • 0.1.0

Defined Under Namespace

Classes: Prefix

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0


11
12
13
14
15
# File 'lib/hanami/cli/registry.rb', line 11

def self.extended(base)
  base.class_eval do
    @commands = CommandRegistry.new
  end
end

Instance Method Details

#get(arguments) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0


85
86
87
# File 'lib/hanami/cli/registry.rb', line 85

def get(arguments)
  @commands.get(arguments)
end

#register(name, command = nil, aliases: [], **options) ⇒ Object

Register a command

Examples:

Register a command

require "hanami/cli"

module Foo
  module Commands
    extend Hanami::CLI::Registry

    class Hello < Hanami::CLI::Command
    end

    register "hi", Hello
  end
end

Register a command with aliases

require "hanami/cli"

module Foo
  module Commands
    extend Hanami::CLI::Registry

    class Hello < Hanami::CLI::Command
    end

    register "hello", Hello, aliases: ["hi", "ciao"]
  end
end

Register a group of commands

require "hanami/cli"

module Foo
  module Commands
    extend Hanami::CLI::Registry

    module Generate
      class App < Hanami::CLI::Command
      end

      class Action < Hanami::CLI::Command
      end
    end

    register "generate", aliases: ["g"] do |prefix|
      prefix.register "app",    Generate::App
      prefix.register "action", Generate::Action
    end
  end
end

Since:

  • 0.1.0


75
76
77
78
79
80
81
# File 'lib/hanami/cli/registry.rb', line 75

def register(name, command = nil, aliases: [], **options)
  if block_given?
    yield Prefix.new(@commands, name, aliases)
  else
    @commands.set(name, command, aliases, **options)
  end
end