Module: Hanami::CLI::Registry

Defined in:
gems/gems/hanami-cli-0.3.0/lib/hanami/cli/registry.rb

Overview

Registry mixin

Since:

  • 0.1.0

Defined Under Namespace

Classes: Prefix

Constant Summary

COMMAND_NAME_SEPARATOR =

Since:

  • 0.1.0

" ".freeze

Instance Method Summary collapse

Instance Method Details

#after(command_name, callback = nil, &blk) ⇒ Object

Register an after callback.

Examples:

require "hanami/cli"

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

    class Hello < Hanami::CLI::Command
      def call(*)
        puts "hello"
      end
    end

    register "hello", Hello
    after "hello", -> { puts "world" }
  end
end

Register an object as callback

require "hanami/cli"

module Callbacks
  class World
    def call(*)
      puts "world"
    end
  end
end

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

    class Hello < Hanami::CLI::Command
      def call(*)
        puts "hello"
      end
    end

    register "hello", Hello
    after "hello", Callbacks::World.new
  end
end

Register a class as callback

require "hanami/cli"

module Callbacks
  class World
    def call(*)
      puts "world"
    end
  end
end

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

    class Hello < Hanami::CLI::Command
      def call(*)
        puts "hello"
      end
    end

    register "hello", Hello
    after "hello", Callbacks::World
  end
end

Parameters:

  • command_name (String)

    the name used for command registration

  • callback (Class, #call) (defaults to: nil)

    the callback object. If a class is given, it MUST respond to #call.

  • blk (Proc)

    the callback espressed as a block

Raises:

Since:

  • 0.2.0

def after(command_name, callback = nil, &blk)
  command(command_name).after_callbacks.append(&_callback(callback, blk))
end

#before(command_name, callback = nil, &blk) ⇒ Object

Register a before callback.

Examples:

require "hanami/cli"

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

    class Hello < Hanami::CLI::Command
      def call(*)
        puts "hello"
      end
    end

    register "hello", Hello
    before "hello", -> { puts "I'm about to say.." }
  end
end

Register an object as callback

require "hanami/cli"

module Callbacks
  class Hello
    def call(*)
      puts "world"
    end
  end
end

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

    class Hello < Hanami::CLI::Command
      def call(*)
        puts "I'm about to say.."
      end
    end

    register "hello", Hello
    before "hello", Callbacks::Hello.new
  end
end

Register a class as callback

require "hanami/cli"

module Callbacks
  class Hello
    def call(*)
      puts "world"
    end
  end
end

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

    class Hello < Hanami::CLI::Command
      def call(*)
        puts "I'm about to say.."
      end
    end

    register "hello", Hello
    before "hello", Callbacks::Hello
  end
end

Parameters:

  • command_name (String)

    the name used for command registration

  • callback (Class, #call) (defaults to: nil)

    the callback object. If a class is given, it MUST respond to #call.

  • blk (Proc)

    the callback espressed as a block

Raises:

Since:

  • 0.2.0

def before(command_name, callback = nil, &blk)
  command(command_name).before_callbacks.append(&_callback(callback, blk))
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

Parameters:

  • name (String)

    the command name

  • command (NilClass, Hanami::CLI::Command) (defaults to: nil)

    the optional command

  • aliases (Array<String>)

    an optional list of aliases

  • options (Hash)

    a set of options

Since:

  • 0.1.0

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