Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

fallback to other filenames if 'log' is a directory

  • Loading branch information...
commit bdfa74236e7b7422d111c2465b4710df42add92d 1 parent c2d0022
David Jacobs authored

Showing 2 changed files with 15 additions and 1 deletion. Show diff stats Hide diff stats

  1. +4 1 lib/letters/core_ext.rb
  2. +11 0 spec/letters/core_ext_spec.rb
5 lib/letters/core_ext.rb
@@ -74,7 +74,10 @@ def e(opts={})
74 74 def f(opts={})
75 75 opts = { format: "yaml", name: "log" }.merge opts
76 76 tap do |o|
77   - File.open(opts[:name], "w+") do |file|
  77 + suffixes = [""] + (1..50).to_a
  78 + deduper = suffixes.detect {|x| !File.directory? "#{opts[:name]}#{x}" }
  79 +
  80 + File.open("#{opts[:name]}#{deduper}", "w+") do |file|
78 81 # Override :stream
79 82 opts.merge! :stream => file
80 83 Helpers.message opts
11 spec/letters/core_ext_spec.rb
@@ -120,6 +120,17 @@ module Letters
120 120 File.read("log").chomp.should == hash.awesome_inspect
121 121 end
122 122 end
  123 +
  124 + describe "when 'log' is a directory" do
  125 + before { FileUtils.mkdir "log" }
  126 + after { FileUtils.rm_rf "log" }
  127 +
  128 + it "appends the first integer that de-dupes the name" do
  129 + File.exist?("log1").should be_false
  130 + lambda { hash.f }.should_not raise_error
  131 + File.exist?("log1").should be_true
  132 + end
  133 + end
123 134 end
124 135
125 136 describe "#j (jump)" do

0 comments on commit bdfa742

Please sign in to comment.
Something went wrong with that request. Please try again.