Class: Capissh::Command::Tree

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/capissh/command/tree.rb

Defined Under Namespace

Classes: Branch, ConditionBranch

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) {|_self| ... } ⇒ Tree

Returns a new instance of Tree.

Yields:

  • (_self)

Yield Parameters:



94
95
96
97
98
# File 'lib/capissh/command/tree.rb', line 94

def initialize(config)
  @configuration = config
  @branches = []
  yield self if block_given?
end

Instance Attribute Details

#branchesObject (readonly)

Returns the value of attribute branches.



5
6
7
# File 'lib/capissh/command/tree.rb', line 5

def branches
  @branches
end

#configurationObject (readonly)

Returns the value of attribute configuration.



4
5
6
# File 'lib/capissh/command/tree.rb', line 4

def configuration
  @configuration
end

#fallbackObject (readonly)

Returns the value of attribute fallback.



6
7
8
# File 'lib/capissh/command/tree.rb', line 6

def fallback
  @fallback
end

Class Method Details

.twig(config, command, &block) ⇒ Object

A tree with only one branch.



90
91
92
# File 'lib/capissh/command/tree.rb', line 90

def self.twig(config, command, &block)
  new(config) { |t| t.else(command, &block) }
end

Instance Method Details

#base_command_and_callback(server) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/capissh/command/tree.rb', line 121

def base_command_and_callback(server)
  branches_for(server).map do |branch|
    command = branch.command
    if configuration
      command = configuration.placeholder_callback.call(command, server)
    end
    [command, branch.callback]
  end
end

#branches_for(server) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/capissh/command/tree.rb', line 108

def branches_for(server)
  seen_last = false
  matches = branches.select do |branch|
    success = !seen_last && !branch.skip? && branch.match(server)
    seen_last = success && branch.last?
    success
  end

  matches << fallback if matches.empty? && fallback

  return matches
end

#each {|fallback| ... } ⇒ Object

Yields:



131
132
133
134
135
# File 'lib/capissh/command/tree.rb', line 131

def each
  branches.each { |branch| yield branch }
  yield fallback if fallback
  return self
end

#else(command, &block) ⇒ Object



104
105
106
# File 'lib/capissh/command/tree.rb', line 104

def else(command, &block)
  @fallback = Branch.new(command, {}, block)
end

#when(condition, command, options = {}, &block) ⇒ Object



100
101
102
# File 'lib/capissh/command/tree.rb', line 100

def when(condition, command, options={}, &block)
  branches << ConditionBranch.new(configuration, condition, command, options, block)
end