Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

update #k functionality to be consistent, understandable

  • Loading branch information...
commit 9ddd6d67c8b9c5bee521ca5a4f9e6fee4abdaec8 1 parent bbf6452
@davejacobs authored
View
4 README.md
@@ -193,10 +193,10 @@ The `o` method takes options, too, so you can add a prefix message to the output
Kill
</td>
<td>
- :max
+ :on
</td>
<td>
- raises Letters::KillError after a maximum number of calls
+ raises Letters::KillError at a specified number of calls
</td>
</tr>
View
2  lib/letters/config.rb
@@ -7,7 +7,7 @@ def self.defaults
d1: { dup: false },
d2: { format: "ap" },
f: { format: "yaml", name: "log" },
- k: { max: 0 },
+ k: { on: 0 },
l: { level: "info", format: "yaml" },
o: { format: "ap", stream: $stdout },
t: { time_format: "millis" }
View
16 lib/letters/core_ext.rb
@@ -1,6 +1,7 @@
require "letters/config"
require "letters/helpers"
require "letters/diff"
+require "letters/kill"
require "letters/time_formats"
require "letters/empty_error"
require "letters/kill_error"
@@ -96,13 +97,20 @@ def j(&block)
# Kill
def k(opts={})
opts = Letters.defaults_with(:k, opts)
+
+ # Support :max option until I can deprecate it
+ opts[:on] ||= opts[:max]
+
opts.merge! :error_class => KillError
tap do |o|
- @letters_kill_count ||= 0
- if @letters_kill_count >= opts[:max]
- raise opts[:error_class]
+ Letters.kill_count ||= 0
+
+ if Letters.kill_count >= opts[:on]
+ Letters.kill_count = 0
+ o.a(opts) { false }
end
- @letters_kill_count += 1
+
+ Letters.kill_count += 1
end
end
View
9 lib/letters/kill.rb
@@ -0,0 +1,9 @@
+module Letters
+ def self.kill_count=(count)
+ @@kill_count = count
+ end
+
+ def self.kill_count
+ @@kill_count if defined?(@@kill_count)
+ end
+end
View
15 spec/letters/core_ext_spec.rb
@@ -147,23 +147,20 @@ module Letters
end
describe "#k (kill)" do
- it 'raises a KillError immediately by default' do
+ it "raises a KillError immediately by default" do
lambda { hash.k }.should raise_error(KillError)
end
- it 'does not raises if number of calls are below max' do
- lambda { hash.k(max: 1) }.should_not raise_error
+ it "does not raises if number of calls are below the specified number" do
+ lambda { hash.k(:on => 1) }.should_not raise_error
end
- it 'raises a KillError if number of calls is above max' do
+ it "raises a KillError if number of calls has reached the specified number" do
count = 0
+ hash.k(:on => 2)
lambda do
- 10.times do
- hash.k(max: 5)
- count += 1
- end
+ hash.k(:on => 2)
end.should raise_error(KillError)
- count.should eq(5)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.