tap-if 0.2.0 → 0.4.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.
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - rbx-19mode
5
+ branches:
6
+ only:
7
+ - master
8
+ notifications:
9
+ email:
10
+ recipients:
11
+ - brian.nort@gmail.com
12
+ matrix:
13
+ allow_failures:
14
+ - rvm: rbx-19mode
data/README.md CHANGED
@@ -1,15 +1,21 @@
1
- # Tap If
1
+ #Tap If [![Build Status](https://travis-ci.org/bnorton/tap_if.png?branch=master)](https://travis-ci.org/bnorton/tap_if)
2
2
 
3
- ## Install / Use
3
+ ## Install
4
4
 
5
- In your `Gemfile` add: `gem 'tap_if'`
6
- Then run `bundle install`.
7
- Add `require 'tap_if'` before you use it.
5
+ In your Gemfile:
6
+ `gem 'tap_if'`
8
7
 
9
- ##Examples
8
+ Then run:
9
+ `$ bundle install`
10
10
 
11
- Delegates to Object#tap if the `caller` is truthy or given the `method name + args` evaluate to
12
- a truthy value. Useful for clarity - always return the caller but only
11
+ Before you use it:
12
+ `require 'tap_if'`
13
+
14
+ ##Usage
15
+
16
+ Yields to the block if the `caller` is truthy or given the `method name + args` evaluate to
17
+ a truthy value.
18
+ Useful for clarity - always return the caller but only
13
19
  execute the block when the condition passes.
14
20
 
15
21
  ```ruby
@@ -19,7 +25,7 @@ User.find(user_id).tap_if(:admin?, account) do |user|
19
25
  user.update_token(account)
20
26
  end
21
27
 
22
- # Only update twitter/facebook if the post actually publishes.
28
+ # Only update twitter/facebook if the post actually updates/publishes.
23
29
 
24
30
  def publish
25
31
  (post.pending? && post.update_attributes(:published => true)).tap_if do
@@ -29,6 +35,12 @@ def publish
29
35
  Facebook.update(the_update)
30
36
  end
31
37
  end
38
+
39
+ # Only add a user to an account if the user is not a member.
40
+
41
+ AccountUser.where(:account_id => account.id, :user_id => user.id).tap_if(:empty?) do |user|
42
+ account.users << user
43
+ end
32
44
  ```
33
45
 
34
46
  ##The Motivation
data/Rakefile CHANGED
@@ -10,17 +10,6 @@ rescue Bundler::BundlerError => e
10
10
  exit e.status_code
11
11
  end
12
12
 
13
- require 'rake'
14
- require 'rdoc/task'
15
- require 'rake/testtask'
16
-
17
- Rake::TestTask.new(:test) do |t|
18
- t.libs << 'lib'
19
- t.libs << 'spec'
20
- t.pattern = 'spec/**/*_spec.rb'
21
- t.verbose = false
22
- end
23
-
24
13
  require 'jeweler'
25
14
  Jeweler::Tasks.new do |gem|
26
15
  gem.name = 'tap-if'
@@ -33,3 +22,7 @@ Jeweler::Tasks.new do |gem|
33
22
  end
34
23
 
35
24
  Jeweler::RubygemsDotOrgTasks.new
25
+
26
+ require 'rspec/core/rake_task'
27
+ RSpec::Core::RakeTask.new(:spec)
28
+ task :default => :spec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.4.0
@@ -1,7 +1,9 @@
1
1
  module TapIf
2
- # Delegates to Object#tap if the `caller` is truthy or given the `method name + args` evaluate to
3
- # a truthy value. Useful for clarity - always return the caller but only
4
- # execute the block when the condition passes.
2
+ # Executes the given block if the `caller` is truthy or given the `method name + args` evaluate to
3
+ # a truthy value.
4
+ #
5
+ # Useful for clarity - always return the caller but only
6
+ # execute the block when the condition passes.
5
7
  #
6
8
  # Update the user's account token if the user is an admin of the account.
7
9
  #
@@ -20,9 +22,10 @@ module TapIf
20
22
  # end
21
23
  # end
22
24
 
23
- def tap_if(*args, &block)
24
- args.empty? && self || args.any? && self.respond_to?(args.first) && self.send(*args) ?
25
- self.tap(&block) : self
25
+ def tap_if(*args)
26
+ yield self if (args.empty? && self || args.any? && respond_to?(args.first) && send(*args))
27
+
28
+ self
26
29
  end
27
30
  end
28
31
 
@@ -11,6 +11,33 @@ describe :tap_if do
11
11
  Object.methods.should include(:tap_if)
12
12
  end
13
13
 
14
+ describe 'when the block takes no arguments' do
15
+ it 'should execute the block' do
16
+ @foo.should_receive(:bar)
17
+
18
+ true.tap_if { @foo.bar }
19
+ end
20
+ end
21
+
22
+ describe 'when the block takes some arguments' do
23
+ before do
24
+ @value = nil
25
+ @block = lambda {|t| @value = t; @foo.bar }
26
+ end
27
+
28
+ it 'should execute the block' do
29
+ @foo.should_receive(:bar)
30
+
31
+ true.tap_if(&@block)
32
+ end
33
+
34
+ it 'should yield the caller' do
35
+ "foo-true".tap_if(&@block)
36
+
37
+ @value.should == "foo-true"
38
+ end
39
+ end
40
+
14
41
  describe "when the method evaluates to true" do
15
42
  [[Array.new, :empty?], [{:f => :d}, :key?, :f]].each do |type, *args|
16
43
  it "should tap #{type.inspect}.#{args.first}" do
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "tap-if"
8
- s.version = "0.2.0"
8
+ s.version = "0.4.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-01-24"
12
+ s.date = "2013-02-06"
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 = [
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
17
17
  "README.md"
18
18
  ]
19
19
  s.files = [
20
+ ".travis.yml",
20
21
  "Gemfile",
21
22
  "Gemfile.lock",
22
23
  "LICENSE.md",
@@ -31,7 +32,7 @@ Gem::Specification.new do |s|
31
32
  s.homepage = "http://github.com/bnorton/tap-if"
32
33
  s.licenses = ["MIT"]
33
34
  s.require_paths = ["lib"]
34
- s.rubygems_version = "1.8.24"
35
+ s.rubygems_version = "1.8.25"
35
36
  s.summary = "Tap into an object but only execute the code if truthy."
36
37
 
37
38
  if s.respond_to? :specification_version then
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.2.0
4
+ version: 0.4.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-01-24 00:00:00.000000000 Z
12
+ date: 2013-02-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -83,6 +83,7 @@ extra_rdoc_files:
83
83
  - LICENSE.md
84
84
  - README.md
85
85
  files:
86
+ - .travis.yml
86
87
  - Gemfile
87
88
  - Gemfile.lock
88
89
  - LICENSE.md
@@ -108,7 +109,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
108
109
  version: '0'
109
110
  segments:
110
111
  - 0
111
- hash: -1728875354126093151
112
+ hash: -2778854339072605407
112
113
  required_rubygems_version: !ruby/object:Gem::Requirement
113
114
  none: false
114
115
  requirements:
@@ -117,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
118
  version: '0'
118
119
  requirements: []
119
120
  rubyforge_project:
120
- rubygems_version: 1.8.24
121
+ rubygems_version: 1.8.25
121
122
  signing_key:
122
123
  specification_version: 3
123
124
  summary: Tap into an object but only execute the code if truthy.