Class: Puppet::Cleaner::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-cleaner/line.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parts) ⇒ Line

Returns a new instance of Line.



7
8
9
10
11
# File 'lib/puppet-cleaner/line.rb', line 7

def initialize(parts)
  @parts = parts.map {|part| Part.create(part) }
  @workers = Hash.new {|h, k| h[k] = []}
  @position = 0
end

Instance Attribute Details

#positionObject (readonly)

Returns the value of attribute position.



5
6
7
# File 'lib/puppet-cleaner/line.rb', line 5

def position
  @position
end

Instance Method Details

#advance!(n = 1) ⇒ Object



56
57
58
# File 'lib/puppet-cleaner/line.rb', line 56

def advance!(n = 1)
  @position += n
end

#append(pos, parts) ⇒ Object



104
105
106
# File 'lib/puppet-cleaner/line.rb', line 104

def append(pos, parts)
  @parts.insert(pos + 1, *parts)
end

#back!(n = 1) ⇒ Object



60
61
62
# File 'lib/puppet-cleaner/line.rb', line 60

def back!(n = 1)
  @position -= n
end

#currentObject



72
73
74
# File 'lib/puppet-cleaner/line.rb', line 72

def current
  @parts[@position]
end

#empty?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/puppet-cleaner/line.rb', line 64

def empty?
  @position >= 0 && @position >= size
end

#hire(workers) ⇒ Object



13
14
15
16
17
18
# File 'lib/puppet-cleaner/line.rb', line 13

def hire(workers)
  @workers.clear
  workers.each do |worker|
    worker.part_names.each {|part_name| @workers[part_name] << worker }
  end
end

#insert(pos, parts) ⇒ Object



100
101
102
# File 'lib/puppet-cleaner/line.rb', line 100

def insert(pos, parts)
  @parts.insert(pos, *parts)
end

#inspectObject



32
33
34
# File 'lib/puppet-cleaner/line.rb', line 32

def inspect
  @parts.inject("") {|m, part| m += "#{part.name.inspect}\t#{part.value.inspect}\n"}
end

#last?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/puppet-cleaner/line.rb', line 84

def last?
  @position == (size  - 1)
end

#lookup(name, ignore = []) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/puppet-cleaner/line.rb', line 88

def lookup(name, ignore = [])
  position = @position
  while position < size && !(ignore + [name]).include?(@parts[position].name)
    position += 1
  end
  position == size || @parts[position].name != name ? nil : position
end

#next(n = 1) ⇒ Object



76
77
78
# File 'lib/puppet-cleaner/line.rb', line 76

def next(n = 1)
  (@position + n) >= size ? NoPart : @parts[@position + n]
end

#partsObject



68
69
70
# File 'lib/puppet-cleaner/line.rb', line 68

def parts
  @parts
end

#prev(n = 1) ⇒ Object



80
81
82
# File 'lib/puppet-cleaner/line.rb', line 80

def prev(n = 1)
  (@position - n) < 0 ? NoPart : @parts[@position - n]
end

#remove(pos) ⇒ Object



96
97
98
# File 'lib/puppet-cleaner/line.rb', line 96

def remove(pos)
  @parts.delete_at(pos)
end

#reset!Object



52
53
54
# File 'lib/puppet-cleaner/line.rb', line 52

def reset!
  @position = 0
end

#showObject



36
37
38
# File 'lib/puppet-cleaner/line.rb', line 36

def show
  start {|part| context = {:before => prev, :after => self.next}; part.show(context) }
end

#sizeObject



48
49
50
# File 'lib/puppet-cleaner/line.rb', line 48

def size
  @parts.size
end

#startObject



40
41
42
43
44
45
46
# File 'lib/puppet-cleaner/line.rb', line 40

def start
  reset!
  while !empty?
    yield current
    advance!
  end
end

#transform!Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/puppet-cleaner/line.rb', line 20

def transform!
  start do |part|
    @workers[part.name].each do |worker|
      worker.operate(self)
      if part.object_id != current.object_id
        back!
        break
      end
    end
  end
end