tap-if 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'http://rubygems.org'
2
+
3
+ group :development, :test do
4
+ gem 'rspec'
5
+ gem 'jeweler'
6
+ gem 'psych'
7
+ gem 'rake'
8
+ end
9
+
@@ -0,0 +1,32 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ git (1.2.5)
6
+ jeweler (1.8.4)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rdoc
11
+ json (1.7.6)
12
+ psych (1.3.4)
13
+ rake (10.0.3)
14
+ rdoc (3.12)
15
+ json (~> 1.4)
16
+ rspec (2.12.0)
17
+ rspec-core (~> 2.12.0)
18
+ rspec-expectations (~> 2.12.0)
19
+ rspec-mocks (~> 2.12.0)
20
+ rspec-core (2.12.1)
21
+ rspec-expectations (2.12.0)
22
+ diff-lcs (~> 1.1.3)
23
+ rspec-mocks (2.12.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ jeweler
30
+ psych
31
+ rake
32
+ rspec
@@ -0,0 +1,20 @@
1
+ Copyright 2013 Brian Norton
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, clone, copy, modify, merge, publish,
7
+ fork, distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,35 @@
1
+ # Tap If
2
+
3
+ ## Install
4
+
5
+ In your `Gemfile` add: `gem 'tap_if'` Then run `bundle install`.
6
+
7
+ ##Examples
8
+
9
+ Delegates to Object#tap if the `caller` is truthy or given the `method name + args` evaluate to
10
+ a truthy value. Useful for clarity - always return the caller but only
11
+ execute the block when the condition passes.
12
+
13
+ ```ruby
14
+ # Update the user's account token if the user is an admin of the account.
15
+
16
+ User.find(user_id).tap_if(:admin?, account) do |user|
17
+ user.update_token(account)
18
+ end
19
+
20
+ # Only update twitter/facebook if the post actually publishes.
21
+
22
+ def publish
23
+ (post.pending? && post.update_attributes(:published => true)).tap_if do
24
+ the_update = "New blog post: #{post.title[0..100]}... #{post.link}"
25
+
26
+ Twitter.update(the_update)
27
+ Facebook.update(the_update)
28
+ end
29
+ end
30
+ ```
31
+
32
+ ## License
33
+
34
+ License: MIT-LICENSE (LICENSE.md)
35
+
@@ -0,0 +1,35 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+ require 'bundler'
4
+
5
+ begin
6
+ Bundler.setup(:default, :development, :test)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts 'Run `bundle install` to install missing gems'
10
+ exit e.status_code
11
+ end
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
+ require 'jeweler'
25
+ Jeweler::Tasks.new do |gem|
26
+ gem.name = 'tap-if'
27
+ gem.homepage = 'http://github.com/bnorton/tap-if'
28
+ gem.license = 'MIT'
29
+ gem.summary = 'Tap into an object but only execute the code if truthy.'
30
+ gem.description = 'Object#tap_if clarifies control flow in many circumstances.'
31
+ gem.email = 'brian.nort@gmail.com'
32
+ gem.authors = %w(bnorton)
33
+ end
34
+
35
+ Jeweler::RubygemsDotOrgTasks.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,29 @@
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.
5
+ #
6
+ # Update the user's account token if the user is an admin of the account.
7
+ #
8
+ # User.find(user_id).tap_if(:admin?, account) do |user|
9
+ # user.update_token(account)
10
+ # end
11
+ #
12
+ # Only update twitter/facebook if the post actually publishes.
13
+ #
14
+ # def publish
15
+ # (post.pending? && post.update_attributes(:published => true)).tap_if do
16
+ # the_update = "New blog post: #{post.title[0..100]}... #{post.link}"
17
+ #
18
+ # Twitter.update(the_update)
19
+ # Facebook.update(the_update)
20
+ # end
21
+ # end
22
+
23
+ def tap_if(*args, &block)
24
+ args.empty? && self || args.any? && self.send(*args) ?
25
+ self.tap(&block) : self
26
+ end
27
+ end
28
+
29
+ Object.send(:include, TapIf)
@@ -0,0 +1,69 @@
1
+ require "spec_helper"
2
+
3
+ describe :tap_if do
4
+ before do
5
+ @foo = mock("foo", :bar => nil)
6
+ @block = lambda {|*| @foo.bar }
7
+ end
8
+
9
+ it "should be a method on all objects" do
10
+ Object.new.methods.should include(:tap_if)
11
+ Object.methods.should include(:tap_if)
12
+ end
13
+
14
+ describe "when the method evaluates to true" do
15
+ [[Array.new, :empty?], [{:f => :d}, :key?, :f]].each do |type, *args|
16
+ it "should tap #{type.inspect}.#{args.first}" do
17
+ @foo.should_receive(:bar)
18
+
19
+ type.tap_if(*args, &@block)
20
+ end
21
+
22
+ it "should return the value" do
23
+ type.tap_if(*args, &@block).object_id.should == type.object_id
24
+ end
25
+ end
26
+ end
27
+
28
+ describe "when the method evaluates to false" do
29
+ [[Array.new, :any?], [{:f => :d}, :key?, :x]].each do |type, *args|
30
+ it "should not tap #{type.inspect}.#{args.first}" do
31
+ @foo.should_not_receive(:bar)
32
+
33
+ type.tap_if(*args, &@block)
34
+ end
35
+
36
+ it "should return the value" do
37
+ type.tap_if(*args, &@block).object_id.should == type.object_id
38
+ end
39
+ end
40
+ end
41
+
42
+ describe "when the target is truthy" do
43
+ [true, "", 1, [], {}, 0, :foo].each do |type|
44
+ it "should tap #{type.inspect}" do
45
+ @foo.should_receive(:bar)
46
+
47
+ type.tap_if(&@block)
48
+ end
49
+
50
+ it "should return the value" do
51
+ type.tap_if(&@block).object_id.should == type.object_id
52
+ end
53
+ end
54
+ end
55
+
56
+ describe "when the target is falsey" do
57
+ [false, nil, def method; end].each do |type|
58
+ it "should not tap #{type.inspect}" do
59
+ @foo.should_not_receive(:bar)
60
+
61
+ type.tap_if(&@block)
62
+ end
63
+
64
+ it "should return the value" do
65
+ type.tap_if(&@block).object_id.should == type.object_id
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,8 @@
1
+ ENV["RAILS_ENV"] ||= 'test'
2
+
3
+ require 'rspec'
4
+ require 'tap_if'
5
+
6
+ RSpec.configure do |c|
7
+ c.mock_with :rspec
8
+ end
@@ -0,0 +1,58 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "tap-if"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["bnorton"]
12
+ s.date = "2013-01-24"
13
+ s.description = "Object#tap_if clarifies control flow in many circumstances."
14
+ s.email = "brian.nort@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.md",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ "Gemfile",
21
+ "Gemfile.lock",
22
+ "LICENSE.md",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/tap_if.rb",
27
+ "spec/lib/tap_if_spec.rb",
28
+ "spec/spec_helper.rb",
29
+ "tap-if.gemspec"
30
+ ]
31
+ s.homepage = "http://github.com/bnorton/tap-if"
32
+ s.licenses = ["MIT"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = "1.8.24"
35
+ s.summary = "Tap into an object but only execute the code if truthy."
36
+
37
+ if s.respond_to? :specification_version then
38
+ s.specification_version = 3
39
+
40
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
41
+ s.add_development_dependency(%q<rspec>, [">= 0"])
42
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
43
+ s.add_development_dependency(%q<psych>, [">= 0"])
44
+ s.add_development_dependency(%q<rake>, [">= 0"])
45
+ else
46
+ s.add_dependency(%q<rspec>, [">= 0"])
47
+ s.add_dependency(%q<jeweler>, [">= 0"])
48
+ s.add_dependency(%q<psych>, [">= 0"])
49
+ s.add_dependency(%q<rake>, [">= 0"])
50
+ end
51
+ else
52
+ s.add_dependency(%q<rspec>, [">= 0"])
53
+ s.add_dependency(%q<jeweler>, [">= 0"])
54
+ s.add_dependency(%q<psych>, [">= 0"])
55
+ s.add_dependency(%q<rake>, [">= 0"])
56
+ end
57
+ end
58
+
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tap-if
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - bnorton
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: jeweler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: psych
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Object#tap_if clarifies control flow in many circumstances.
79
+ email: brian.nort@gmail.com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files:
83
+ - LICENSE.md
84
+ - README.md
85
+ files:
86
+ - Gemfile
87
+ - Gemfile.lock
88
+ - LICENSE.md
89
+ - README.md
90
+ - Rakefile
91
+ - VERSION
92
+ - lib/tap_if.rb
93
+ - spec/lib/tap_if_spec.rb
94
+ - spec/spec_helper.rb
95
+ - tap-if.gemspec
96
+ homepage: http://github.com/bnorton/tap-if
97
+ licenses:
98
+ - MIT
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ segments:
110
+ - 0
111
+ hash: -1026261804445811438
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 1.8.24
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: Tap into an object but only execute the code if truthy.
124
+ test_files: []