Class: Chef::Util::Editor

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/util/editor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines) ⇒ Editor

Returns a new instance of Editor.



24
25
26
# File 'lib/chef/util/editor.rb', line 24

def initialize(lines)
  @lines = lines.to_a.clone
end

Instance Attribute Details

#linesObject (readonly)

Returns the value of attribute lines.



22
23
24
# File 'lib/chef/util/editor.rb', line 22

def lines
  @lines
end

Instance Method Details

#append_line_after(search, line_to_append) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/chef/util/editor.rb', line 28

def append_line_after(search, line_to_append)
  lines = []

  @lines.each do |line|
    lines << line
    lines << line_to_append if line.match(search)
  end

  (lines.length - @lines.length).tap { @lines = lines }
end

#append_line_if_missing(search, line_to_append) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/chef/util/editor.rb', line 39

def append_line_if_missing(search, line_to_append)
  count = 0

  unless @lines.find { |line| line.match(search) }
    count = 1
    @lines << line_to_append
  end

  count
end

#remove_lines(search) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/chef/util/editor.rb', line 50

def remove_lines(search)
  count = 0

  @lines.delete_if do |line|
    count += 1 if line.match(search)
  end

  count
end

#replace(search, replace) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/chef/util/editor.rb', line 60

def replace(search, replace)
  count = 0

  @lines.map! do |line|
    if line.match(search)
      count += 1
      line.gsub!(search, replace)
    else
      line
    end
  end

  count
end

#replace_lines(search, replace) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/chef/util/editor.rb', line 75

def replace_lines(search, replace)
  count = 0

  @lines.map! do |line|
    if line.match(search)
      count += 1
      replace
    else
      line
    end
  end

  count
end