tap-if 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +8 -1
- data/VERSION +1 -1
- data/lib/tap_unless.rb +12 -0
- data/spec/lib/tap_if_spec.rb +2 -1
- data/spec/lib/tap_unless_spec.rb +107 -0
- data/spec/spec_helper.rb +0 -1
- data/tap-if.gemspec +4 -2
- metadata +5 -3
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#Tap If [![Build Status](https://travis-ci.org/bnorton/tap_if.png?branch=master)](https://travis-ci.org/bnorton/tap_if)
|
1
|
+
#Tap If and Tap Unless [![Build Status](https://travis-ci.org/bnorton/tap_if.png?branch=master)](https://travis-ci.org/bnorton/tap_if)
|
2
2
|
|
3
3
|
## Install
|
4
4
|
|
@@ -10,6 +10,7 @@ Then run:
|
|
10
10
|
|
11
11
|
Before you use it:
|
12
12
|
`require 'tap_if'`
|
13
|
+
`require 'tap_unless'`
|
13
14
|
|
14
15
|
##Usage
|
15
16
|
|
@@ -41,6 +42,12 @@ end
|
|
41
42
|
AccountUser.where(:account_id => account.id, :user_id => user.id).tap_if(:empty?) do |user|
|
42
43
|
account.users << user
|
43
44
|
end
|
45
|
+
|
46
|
+
# OR
|
47
|
+
|
48
|
+
AccountUser.where(:account_id => account.id, :user_id => user.id).tap_unless(:any?) do |user|
|
49
|
+
account.users << user
|
50
|
+
end
|
44
51
|
```
|
45
52
|
|
46
53
|
##The Motivation
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.5.0
|
data/lib/tap_unless.rb
ADDED
data/spec/lib/tap_if_spec.rb
CHANGED
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'tap_unless'
|
3
|
+
|
4
|
+
describe :tap_unless do
|
5
|
+
before do
|
6
|
+
@foo = mock("foo", :bar => nil)
|
7
|
+
@block = lambda {|*| @foo.bar }
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should be a method on all objects" do
|
11
|
+
Object.new.methods.should include(:tap_unless)
|
12
|
+
Object.methods.should include(:tap_unless)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe 'when the block takes no arguments' do
|
16
|
+
it 'should execute the block' do
|
17
|
+
@foo.should_receive(:bar)
|
18
|
+
|
19
|
+
false.tap_unless { @foo.bar }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'when the block takes some arguments' do
|
24
|
+
before do
|
25
|
+
@value = nil
|
26
|
+
@block = lambda {|t| @value = t; @foo.bar }
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should execute the block' do
|
30
|
+
@foo.should_receive(:bar)
|
31
|
+
|
32
|
+
false.tap_unless(&@block)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should yield the caller' do
|
36
|
+
false.tap_unless(&@block)
|
37
|
+
|
38
|
+
@value.should == false
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "when the method evaluates to true" do
|
43
|
+
[[Array.new, :empty?], [{:f => :d}, :key?, :f]].each do |type, *args|
|
44
|
+
it "should not tap #{type.inspect}.#{args.first}" do
|
45
|
+
@foo.should_not_receive(:bar)
|
46
|
+
|
47
|
+
type.tap_unless(*args, &@block)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should return the value" do
|
51
|
+
type.tap_unless(*args, &@block).object_id.should == type.object_id
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "when the method evaluates to false" do
|
57
|
+
[[Array.new, :any?], [{:f => :d}, :key?, :x]].each do |type, *args|
|
58
|
+
it "should tap #{type.inspect}.#{args.first}" do
|
59
|
+
@foo.should_receive(:bar)
|
60
|
+
|
61
|
+
type.tap_unless(*args, &@block)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should return the value" do
|
65
|
+
type.tap_unless(*args, &@block).object_id.should == type.object_id
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe "when the method is not defined" do
|
71
|
+
[[nil, :any?], [[], :keys]].each do |type, *args|
|
72
|
+
it "should not tap #{type.inspect}.#{args.first}" do
|
73
|
+
@foo.should_not_receive(:bar)
|
74
|
+
|
75
|
+
type.tap_unless(*args, &@block)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "when the target is truthy" do
|
81
|
+
[true, "", 1, [], {}, 0, :foo].each do |type|
|
82
|
+
it "should not tap #{type.inspect}" do
|
83
|
+
@foo.should_not_receive(:bar)
|
84
|
+
|
85
|
+
type.tap_unless(&@block)
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should return the value" do
|
89
|
+
type.tap_unless(&@block).object_id.should == type.object_id
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe "when the target is falsey" do
|
95
|
+
[false, nil, def method; end].each do |type|
|
96
|
+
it "should tap #{type.inspect}" do
|
97
|
+
@foo.should_receive(:bar)
|
98
|
+
|
99
|
+
type.tap_unless(&@block)
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should return the value" do
|
103
|
+
type.tap_unless(&@block).object_id.should == type.object_id
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
data/spec/spec_helper.rb
CHANGED
data/tap-if.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "tap-if"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.5.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["bnorton"]
|
12
|
-
s.date = "2013-02-
|
12
|
+
s.date = "2013-02-09"
|
13
13
|
s.description = "Object#tap_if clarifies control flow in many circumstances."
|
14
14
|
s.email = "brian.nort@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -25,7 +25,9 @@ Gem::Specification.new do |s|
|
|
25
25
|
"Rakefile",
|
26
26
|
"VERSION",
|
27
27
|
"lib/tap_if.rb",
|
28
|
+
"lib/tap_unless.rb",
|
28
29
|
"spec/lib/tap_if_spec.rb",
|
30
|
+
"spec/lib/tap_unless_spec.rb",
|
29
31
|
"spec/spec_helper.rb",
|
30
32
|
"tap-if.gemspec"
|
31
33
|
]
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tap-if
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -91,7 +91,9 @@ files:
|
|
91
91
|
- Rakefile
|
92
92
|
- VERSION
|
93
93
|
- lib/tap_if.rb
|
94
|
+
- lib/tap_unless.rb
|
94
95
|
- spec/lib/tap_if_spec.rb
|
96
|
+
- spec/lib/tap_unless_spec.rb
|
95
97
|
- spec/spec_helper.rb
|
96
98
|
- tap-if.gemspec
|
97
99
|
homepage: http://github.com/bnorton/tap-if
|
@@ -109,7 +111,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
109
111
|
version: '0'
|
110
112
|
segments:
|
111
113
|
- 0
|
112
|
-
hash:
|
114
|
+
hash: 87678209533408017
|
113
115
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
116
|
none: false
|
115
117
|
requirements:
|