fc5cfc8cca
* Implement a simple destroy method. * Add tests for it * Refactor code, so file is actually read only once. However, due to the nature how provider tests are run, we need to ensure that the file is read before we open it to write it.
27 lines
500 B
Ruby
27 lines
500 B
Ruby
Puppet::Type.type(:file_line).provide(:ruby) do
|
|
|
|
def exists?
|
|
lines.find do |line|
|
|
line.chomp == resource[:line].chomp
|
|
end
|
|
end
|
|
|
|
def create
|
|
File.open(resource[:path], 'a') do |fh|
|
|
fh.puts resource[:line]
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
local_lines = lines
|
|
File.open(resource[:path],'w') do |fh|
|
|
fh.write(local_lines.reject{|l| l.chomp == resource[:line] }.join(''))
|
|
end
|
|
end
|
|
|
|
private
|
|
def lines
|
|
@lines ||= File.readlines(resource[:path])
|
|
end
|
|
|
|
end
|