Class: Dry::CLI::CommandRegistry Private

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/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: Chain, 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



14
15
16
17
# File 'lib/dry/cli/command_registry.rb', line 14

def initialize
  @_mutex = Mutex.new
  @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

Since:

  • 0.1.0



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dry/cli/command_registry.rb', line 42

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

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

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

    result
  end
end

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dry/cli/command_registry.rb', line 21

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

    node.aliases!(aliases)
    node.hidden!(hidden)
    if command
      node.leaf!(command)
      node.subcommands!(command)
    end

    nil
  end
end