Permalink
Browse files

replace File#save with overwrite option with File#save! method

  • Loading branch information...
1 parent 726c6b8 commit bc26cf3baf5f8a243aa7584fb10987c66017db2d @leejarvis leejarvis committed Mar 20, 2013
Showing with 22 additions and 9 deletions.
  1. +1 −3 CHANGELOG.rdoc
  2. +20 −5 lib/mechanize/file.rb
  3. +1 −1 test/test_mechanize_file.rb
View
@@ -6,9 +6,7 @@
* Mechanize#start and Mechanize#shutdown (Thanks, Damian Janowski!)
* Added Mechanize::Agent#allowed_error_codes for setting an Array
of status codes which should not raise an error. #248 Laurence Rowe.
- * `File.save` now takes an optional second Hash argument, this includes
- an `overwrite` option which when set to true will overwrite the original
- file if it exists, otherwise fall back to the default behaviour. #219
+ * Added `File.save!` for overwriting existing files #219.
* DirectorySaver::save_to now accepts an option to decode filename. #262
* element(s)_with methods now accept a :search option for doing xpath/css
selector searching. #287 Philippe Bourgau
View
@@ -50,15 +50,13 @@ def initialize uri = nil, response = nil, body = nil, code = nil
##
# Use this method to save the content of this object to +filename+.
- # If +options+ has an +overwrite+ key set to +true+, the saved file
- # will overwrite any original.
#
# file.save 'index.html'
# file.save 'index.html' # saves index.html.1
- # file.save 'index.html', :overwrite => true # overwrites index.html
+ # file.save 'index.html'
- def save filename = nil, options = {}
- filename = find_free_name filename unless options[:overwrite]
+ def save filename = nil
+ filename = find_free_name filename
dirname = File.dirname filename
FileUtils.mkdir_p dirname
@@ -70,5 +68,22 @@ def save filename = nil, options = {}
alias save_as save
+ ##
+ # Use this method to save the content of this object to +filename+.
+ # This method will overwrite any existing filename that exists with the
+ # same name.
+ #
+ # file.save 'index.html'
+ # file.save! 'index.html' # overwrite original file
+
+ def save! filename = nil
+ dirname = File.dirname filename
+ FileUtils.mkdir_p dirname
+
+ open filename, 'wb' do |f|
+ f.write body
+ end
+ end
+
end
@@ -85,7 +85,7 @@ def test_save_overwrite
assert File.exist? 'test.html'
- page.save 'test.html', :overwrite => true
+ page.save! 'test.html'
assert File.exist? 'test.html'
refute File.exist? 'test.html.1'

0 comments on commit bc26cf3

Please sign in to comment.