Class: Puppet::Cleaner::AlignFarrow

Inherits:
Worker
  • Object
show all
Defined in:
lib/puppet-cleaner/workers/alignfarrow.rb

Instance Method Summary collapse

Methods inherited from Worker

#get_param

Instance Method Details

#get_params(line) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/puppet-cleaner/workers/alignfarrow.rb', line 15

def get_params(line)
  depth = 1
  max = 0
  pos = line.position
  params = []
  
  loop do
    pos += 1
    break if depth == 0 || pos >= line.parts.size
    case line.parts[pos].name
    when :LBRACE
      depth += 1
    when :RBRACE
      depth -= 1
    when :FARROW
      next if depth != 1
  
      if line.parts[pos - 1].name == :BLANK
        name = line.parts[pos - 2]
        before = line.parts[pos - 1]
      else
        name = line.parts[pos - 1]
        before = Part.create([:BLANK, {:value => ''}])
        line.insert(pos, [before])
        pos += 1
      end
  
      if line.parts[pos + 1].name == :BLANK
        line.parts[pos + 1].value = ' ' if line.parts[pos + 1].value != ' '
      else
        line.append(pos, Part.create([:BLANK, {:value => ' '}]))
      end
  
      max = name.to_s.size if name.to_s.size > max
      params << {:name => name, :before => before}
    end
  end
  [max, params]
end

#operate(line) ⇒ Object



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

def operate(line)
  max, params = get_params(line)
  params.each do |param|
    nblanks = max - param[:name].to_s.size + 1
    param[:before].value = ' '*nblanks if param[:before].value.size != nblanks
  end
end

#part_namesObject



3
4
5
# File 'lib/puppet-cleaner/workers/alignfarrow.rb', line 3

def part_names
  [:LBRACE]
end