tap-if 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
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'` Then run `bundle install`.
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
1
+ 0.2.0
@@ -21,7 +21,7 @@ module TapIf
21
21
  # end
22
22
 
23
23
  def tap_if(*args, &block)
24
- args.empty? && self || args.any? && self.send(*args) ?
24
+ args.empty? && self || args.any? && self.respond_to?(args.first) && self.send(*args) ?
25
25
  self.tap(&block) : self
26
26
  end
27
27
  end
@@ -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
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "tap-if"
8
- s.version = "0.1.0"
8
+ s.version = "0.2.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"]
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.1.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: -1026261804445811438
111
+ hash: -1728875354126093151
112
112
  required_rubygems_version: !ruby/object:Gem::Requirement
113
113
  none: false
114
114
  requirements: