Class: ThorEnhance::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/thor_enhance/tree.rb

Constant Summary collapse

DEFAULT_IGNORE_COMMANDS =
["help", "thor_enhance_autogenerate"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command:, base:, parent: nil) ⇒ Tree

command: Thor::Command struct base: Root level class where the command is from parent: 1 level up if nested subcommand



37
38
39
40
41
42
43
44
45
46
# File 'lib/thor_enhance/tree.rb', line 37

def initialize(command:, base:, parent: nil)
  @parent = parent
  @base = base
  @command = command
  @children = {}

  if !base.subcommand_classes.nil? && base.subcommand_classes[command.name]
    @children = self.class.tree(parent: self, base: base.subcommand_classes[command.name])
  end
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



32
33
34
# File 'lib/thor_enhance/tree.rb', line 32

def base
  @base
end

#childrenObject (readonly)

Returns the value of attribute children.



32
33
34
# File 'lib/thor_enhance/tree.rb', line 32

def children
  @children
end

#commandObject (readonly)

Returns the value of attribute command.



32
33
34
# File 'lib/thor_enhance/tree.rb', line 32

def command
  @command
end

#parentObject (readonly)

Returns the value of attribute parent.



32
33
34
# File 'lib/thor_enhance/tree.rb', line 32

def parent
  @parent
end

Class Method Details

.add_ignore_commands(command) ⇒ Object



7
8
9
10
11
12
# File 'lib/thor_enhance/tree.rb', line 7

def self.add_ignore_commands(command)
  return false if ignore_commands.include?(command)

  ignore_commands << command
  true
end

.ignore_commandsObject



14
15
16
# File 'lib/thor_enhance/tree.rb', line 14

def self.ignore_commands
  @ignore_commands ||= DEFAULT_IGNORE_COMMANDS.dup
end

.reset_ignore_commands!Object



18
19
20
# File 'lib/thor_enhance/tree.rb', line 18

def self.reset_ignore_commands!
  @ignore_commands = DEFAULT_IGNORE_COMMANDS.dup
end

.tree(base:, parent: nil) ⇒ Object

Raises:



22
23
24
25
26
27
28
29
30
# File 'lib/thor_enhance/tree.rb', line 22

def self.tree(base:, parent: nil)
  raise TreeFailure, "#{base} does not respond to all_commands. Unable to continue" unless base.respond_to?(:all_commands)

  base.all_commands.map do |k, command|
    next if ignore_commands.include?(k)

    [k, new(command: command, base: base, parent: parent)]
  end.compact.to_h
end

Instance Method Details

#children?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/thor_enhance/tree.rb', line 48

def children?
  children.count > 0
end