sethvargo-cookbooks/swap — pull request #9 by drywheat (2013-07-09 22:08:21+00:00)
Title: fixes #persist method

I noticed that the `#persist` method adds duplicate entries to the fstab for the configured swapfile.

##### For example:

*Before addition:*
```
# /etc/fstab: static file system information.
#
#
```

*After addtion:*
```
# /etc/fstab: static file system information.
/mnt/ephemeral/swapfile swap swap defaults 0 0
#
/mnt/ephemeral/swapfile swap swap defaults 0 0
#
/mnt/ephemeral/swapfile swap swap defaults 0 0
```

The `Chef::Util::FileEdit` class applies regex patterns to **each** line separately ([docs read "line by line"](http://rubydoc.info/gems/chef/Chef/Util/FileEdit#insert_line_after_match-instance_method)). This means that the current implementation of `#persist` is "no bueno".

```ruby
def persist
line = "#{@new_resource.path} swap swap defaults 0 0"
file = Chef::Util::FileEdit.new('/etc/fstab')

file.search_file_delete_line(line)
file.insert_line_after_match(/\Z/, line)
file.write_file
end
```

Unfortunately, there is no way to apply the end-of-string anchor to only the last line using this library. So, I skimmed the Chef source and implemented an alternative that emulates the same behavior without duplication.