Permalink
Browse files

Merge pull request #10 from blambeau/kill

add a `k` (Kill) letter.
  • Loading branch information...
2 parents 5054db0 + 3de8725 commit 58f26121dab1f8ab61880239257f396e73d2f5fb @davejacobs committed Oct 4, 2012
Showing with 39 additions and 2 deletions.
  1. +1 −0 README.md
  2. +14 −0 lib/letters/core_ext.rb
  3. +4 −0 lib/letters/kill_error.rb
  4. +20 −2 spec/letters/core_ext_spec.rb
View
1 README.md
@@ -64,6 +64,7 @@ The `o` method takes options, too, so you can add a prefix message to the output
- `e` (Empty) raises a Letters::EmptyError if its receiver is empty
- `f` (File) writes its receiver into a file in a given format
- `j` (Jump) executes its block in the context of its receiver
+- `k` (Kill) raises Letters::KillError after a maximum number of calls
- `l` (Logger) logs its receivers on the available logger instance
- `m` (Taint) taints (or untaints) its receiver
- `n` (Nil) raises a Letters::NilError if its receiver is nil
View
14 lib/letters/core_ext.rb
@@ -2,6 +2,7 @@
require "letters/diff"
require "letters/assertion_error"
require "letters/empty_error"
+require "letters/kill_error"
require "letters/nil_error"
require "letters/time_formats"
@@ -93,6 +94,19 @@ def j(&block)
end
end
+ # Kill
+ def k(opts={})
+ opts = { max: 0 }.merge(opts)
+ opts.merge! :error_class => KillError
+ tap do |o|
+ @letters_kill_count ||= 0
+ if @letters_kill_count >= opts[:max]
+ raise opts[:error_class]
+ end
+ @letters_kill_count += 1
+ end
+ end
+
# Log
def l(opts={})
opts = { level: "info", format: "yaml" }.merge opts
View
4 lib/letters/kill_error.rb
@@ -0,0 +1,4 @@
+module Letters
+ class KillError < RuntimeError
+ end
+end
View
22 spec/letters/core_ext_spec.rb
@@ -15,15 +15,15 @@ module Letters
FileUtils.rm_rf "tmp"
end
- it "all letter methods but #e and #n return the original object" do
+ it "all letter methods but #e, #k and #n return the original object" do
# Prevent output and debugging
Helpers.should_receive(:call_debugger).any_number_of_times
$stdout.should_receive(:puts).any_number_of_times
hash.should_receive(:system).any_number_of_times
Helpers.should_receive(:change_safety).any_number_of_times
("a".."z").to_a.reject do |letter|
- letter =~ /[ejn]/
+ letter =~ /[ekjn]/
end.select do |letter|
hash.respond_to? letter
end.each do |letter|
@@ -146,6 +146,24 @@ module Letters
end
end
+ describe "#k (kill)" 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
+ end
+
+ it 'raises a KillError if number of calls is above max' do
+ h, count = hash, 0
+ lambda{
+ 10.times{ h.k(max: 5); count += 1; }
+ }.should raise_error(KillError)
+ count.should eq(5)
+ end
+ end
+
describe "#l (log)" do
it "logs the object if a logger is present and then returns the object" do
logger = double 'logger'

0 comments on commit 58f2612

Please sign in to comment.