Class: Kube::Cluster::CommandNode

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/kube/cluster/command_node.rb

Constant Summary collapse

BLANK_DATA =
{ commands: [], resource: nil, args: [], flags: {} }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_node:, command_data: BLANK_DATA) ⇒ CommandNode

Returns a new instance of CommandNode.



12
13
14
15
# File 'lib/kube/cluster/command_node.rb', line 12

def initialize(current_node:, command_data: BLANK_DATA)
  @current_node = current_node
  @command_data = command_data
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



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
76
# File 'lib/kube/cluster/command_node.rb', line 42

def method_missing(name, *args, &block)
  segment = name.to_s.tr("_", "-")
  child   = @current_node.children[segment]

  if child&.command?
    self.class.new(
      current_node: child,
      command_data: @command_data.merge(
        commands: @command_data[:commands] + [segment],
        args:     @command_data[:args] + args.map(&:to_s)
      )
    )
  elsif child&.resource?
    self.class.new(
      current_node: child,
      command_data: @command_data.merge(
        resource: (@command_data[:resource] || ResourceSelector.new) + [segment],
        args:     @command_data[:args] + args.map(&:to_s)
      )
    )
  elsif @current_node.resource? || @current_node.children.empty?
    # Leaf command or already in resource mode — free-form resource segment
    self.class.new(
      current_node: TreeNode.new(name: segment, type: :resource),
      command_data: @command_data.merge(
        resource: (@command_data[:resource] || ResourceSelector.new) + [segment],
        args:     @command_data[:args] + args.map(&:to_s)
      )
    )
  elsif Enumerable.method_defined?(name)
    to_a.public_send(name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#command_dataObject (readonly)

Returns the value of attribute command_data.



10
11
12
# File 'lib/kube/cluster/command_node.rb', line 10

def command_data
  @command_data
end

Instance Method Details

#each(&block) ⇒ Object



26
27
28
# File 'lib/kube/cluster/command_node.rb', line 26

def each(&block)
  to_a.each(&block)
end

#flag(key, value = nil) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/kube/cluster/command_node.rb', line 17

def flag(key, value = nil)
  self.class.new(
    current_node: @current_node,
    command_data: @command_data.merge(
      flags: @command_data[:flags].merge(key.to_s.tr("_", "-") => value)
    )
  )
end

#inspectObject



38
39
40
# File 'lib/kube/cluster/command_node.rb', line 38

def inspect
  "#<#{self.class.name} #{to_s}>"
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
# File 'lib/kube/cluster/command_node.rb', line 78

def respond_to_missing?(name, include_private = false)
  segment = name.to_s.tr("_", "-")

  @current_node.children.key?(segment) ||
    @current_node.resource? ||
    @current_node.children.empty? ||
    Enumerable.method_defined?(name) ||
    super
end

#to_aObject



30
31
32
# File 'lib/kube/cluster/command_node.rb', line 30

def to_a
  QueryBuilder.new(@command_data).to_a
end

#to_sObject



34
35
36
# File 'lib/kube/cluster/command_node.rb', line 34

def to_s
  QueryBuilder.new(@command_data).to_s
end