Skip to content

Commit

Permalink
fallback to other filenames if 'log' is a directory
Browse files Browse the repository at this point in the history
  • Loading branch information
davejacobs committed Oct 2, 2012
1 parent c2d0022 commit bdfa742
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/letters/core_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ def e(opts={})
def f(opts={})
opts = { format: "yaml", name: "log" }.merge opts
tap do |o|
File.open(opts[:name], "w+") do |file|
suffixes = [""] + (1..50).to_a
deduper = suffixes.detect {|x| !File.directory? "#{opts[:name]}#{x}" }

File.open("#{opts[:name]}#{deduper}", "w+") do |file|
# Override :stream
opts.merge! :stream => file
Helpers.message opts
Expand Down
11 changes: 11 additions & 0 deletions spec/letters/core_ext_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ module Letters
File.read("log").chomp.should == hash.awesome_inspect
end
end

describe "when 'log' is a directory" do
before { FileUtils.mkdir "log" }
after { FileUtils.rm_rf "log" }

it "appends the first integer that de-dupes the name" do
File.exist?("log1").should be_false
lambda { hash.f }.should_not raise_error
File.exist?("log1").should be_true
end
end
end

describe "#j (jump)" do
Expand Down

0 comments on commit bdfa742

Please sign in to comment.