tap-if 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +26 -2
- data/VERSION +1 -1
- data/lib/tap_if.rb +1 -1
- data/spec/lib/tap_if_spec.rb +10 -0
- data/tap-if.gemspec +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# Tap If
|
2
2
|
|
3
|
-
## Install
|
3
|
+
## Install / Use
|
4
4
|
|
5
|
-
In your `Gemfile` add: `gem 'tap_if'`
|
5
|
+
In your `Gemfile` add: `gem 'tap_if'`
|
6
|
+
Then run `bundle install`.
|
7
|
+
Add `require 'tap_if'` before you use it.
|
6
8
|
|
7
9
|
##Examples
|
8
10
|
|
@@ -29,6 +31,28 @@ def publish
|
|
29
31
|
end
|
30
32
|
```
|
31
33
|
|
34
|
+
##The Motivation
|
35
|
+
|
36
|
+
I found myself assigning a variable and doing something to it if a condition passed.
|
37
|
+
This pattern leaves dangling `ifs` on the end of lines and is simply less clear than
|
38
|
+
code that will execute if the line is reached.
|
39
|
+
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
# I found myself doing (a contrived version):
|
43
|
+
|
44
|
+
user = User.where(:email => email).first
|
45
|
+
user.update_attributes(:status => "active") if user.present? && user.admin?
|
46
|
+
|
47
|
+
#But now I can do:
|
48
|
+
|
49
|
+
User.where(:email => email).first.tap_if(:admin?) do |user|
|
50
|
+
user.update_attributes(:status => "active")
|
51
|
+
end
|
52
|
+
|
53
|
+
# In essence we always update the user assuming we get there.
|
54
|
+
```
|
55
|
+
|
32
56
|
## License
|
33
57
|
|
34
58
|
License: MIT-LICENSE (LICENSE.md)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/tap_if.rb
CHANGED
data/spec/lib/tap_if_spec.rb
CHANGED
@@ -39,6 +39,16 @@ describe :tap_if do
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
+
describe "when the method is not defined" do
|
43
|
+
[[nil, :any?], [[], :keys]].each do |type, *args|
|
44
|
+
it "should not tap #{type.inspect}.#{args.first}" do
|
45
|
+
@foo.should_not_receive(:bar)
|
46
|
+
|
47
|
+
type.tap_if(*args, &@block)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
42
52
|
describe "when the target is truthy" do
|
43
53
|
[true, "", 1, [], {}, 0, :foo].each do |type|
|
44
54
|
it "should tap #{type.inspect}" do
|
data/tap-if.gemspec
CHANGED
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.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -108,7 +108,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
108
108
|
version: '0'
|
109
109
|
segments:
|
110
110
|
- 0
|
111
|
-
hash: -
|
111
|
+
hash: -1728875354126093151
|
112
112
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
113
|
none: false
|
114
114
|
requirements:
|