Class: Hanami::CLI::CommandRegistry Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/cli/command_registry.rb

Overview

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

Command registry

Since:

  • 0.1.0

Defined Under Namespace

Classes: LookupResult, Node

Instance Method Summary collapse

Constructor Details

#initializeCommandRegistry

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.

Returns a new instance of CommandRegistry.

Since:

  • 0.1.0



13
14
15
# File 'lib/hanami/cli/command_registry.rb', line 13

def initialize
  @root = Node.new
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.

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength

Since:

  • 0.1.0



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hanami/cli/command_registry.rb', line 37

def get(arguments)
  node   = @root
  args   = []
  names  = []
  result = LookupResult.new(node, args, names, node.leaf?)

  arguments.each_with_index do |token, i|
    tmp = node.lookup(token)

    if tmp.nil?
      result = LookupResult.new(node, args, names, false)
      break
    elsif tmp.leaf?
      args   = arguments[i + 1..-1]
      names  = arguments[0..i]
      node   = tmp
      result = LookupResult.new(node, args, names, true)
      break
    else
      names  = arguments[0..i]
      node   = tmp
      result = LookupResult.new(node, args, names, node.leaf?)
    end
  end

  result
end

#set(name, command, aliases, **options) ⇒ 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



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hanami/cli/command_registry.rb', line 19

def set(name, command, aliases, **options)
  node = @root
  command = command_for(name, command, **options)
  name.split(/[[:space:]]/).each do |token|
    node = node.put(node, token)
  end

  node.aliases!(aliases)
  node.leaf!(command) unless command.nil?

  nil
end