try_chain 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/try_chain.rb +21 -0
- data/lib/try_chain/version.rb +1 -1
- data/spec/try_chain_spec.rb +23 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 860a265e5780c5528761f44f949e04751d3a41d2
|
4
|
+
data.tar.gz: b4b2f922ab71513630eca2896e3c540f09371f96
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 651632272c2fe6be77d97d1f8215f19080f07223a589d0012bb8096b7edf2824d143ff5e8975812fbfc1ff02f9323e4dea36d966e5e4af837a609f69ff2a9712
|
7
|
+
data.tar.gz: 525b87f19d85383389dfd53e501bfab796ca6fa5ec0d3b93d67713270aab3736301f62263d3dc3e267ee30bc361fe0ce810d5691326493ce922ddfcc4bfbbd8e
|
data/lib/try_chain.rb
CHANGED
@@ -9,9 +9,17 @@ module TryChain
|
|
9
9
|
return object.try_chain(chain)
|
10
10
|
end
|
11
11
|
|
12
|
+
def self.proxy_try_chain!(object, *chain)
|
13
|
+
object.extend(::TryChain::InstanceMethods)
|
14
|
+
|
15
|
+
return object.try_chain!(chain)
|
16
|
+
end
|
17
|
+
|
12
18
|
class << self
|
13
19
|
alias_method :call, :proxy_try_chain
|
14
20
|
alias_method :proxied_try_chain, :proxy_try_chain
|
21
|
+
alias_method :call!, :proxy_try_chain!
|
22
|
+
alias_method :proxied_try_chain!, :proxy_try_chain!
|
15
23
|
end
|
16
24
|
|
17
25
|
module InstanceMethods
|
@@ -27,5 +35,18 @@ module TryChain
|
|
27
35
|
result
|
28
36
|
end
|
29
37
|
end
|
38
|
+
|
39
|
+
def try_chain!(*symbols)
|
40
|
+
return nil if self.nil?
|
41
|
+
|
42
|
+
symbols = symbols.flatten
|
43
|
+
symbols.compact!
|
44
|
+
|
45
|
+
symbols.reduce(self) do |result, symbol|
|
46
|
+
result = result.try!(symbol)
|
47
|
+
break nil if result.nil?
|
48
|
+
result
|
49
|
+
end
|
50
|
+
end
|
30
51
|
end
|
31
52
|
end
|
data/lib/try_chain/version.rb
CHANGED
data/spec/try_chain_spec.rb
CHANGED
@@ -3,17 +3,39 @@ require 'spec_helper'
|
|
3
3
|
describe ::TryChain do
|
4
4
|
|
5
5
|
context "#proxy_try_chain" do
|
6
|
-
|
7
6
|
it "returns nil when nil given" do
|
8
7
|
::TryChain.proxy_try_chain(nil, :derp, :derp).should be_nil
|
9
8
|
end
|
10
9
|
|
10
|
+
it "returns nil when object does not respond to message" do
|
11
|
+
mock_it = double(Object)
|
12
|
+
::TryChain.proxy_try_chain(nil, :derp, :derp).should be_nil
|
13
|
+
end
|
14
|
+
|
11
15
|
it "returns nil when first level try returns nil" do
|
12
16
|
mock_it = double(Object)
|
13
17
|
mock_it.stub(:derp) { nil }
|
14
18
|
::TryChain.proxy_try_chain(mock_it, :derp, :derp).should be_nil
|
15
19
|
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "#proxy_try_chain!" do
|
23
|
+
it "returns nil when nil given" do
|
24
|
+
::TryChain.proxy_try_chain!(nil, :derp, :derp).should be_nil
|
25
|
+
end
|
26
|
+
|
27
|
+
it "throws eception when object does not respond to message" do
|
28
|
+
mock_it = double(Object)
|
29
|
+
expect {
|
30
|
+
::TryChain.proxy_try_chain!(mock_it, :derp, :derp).should be_nil
|
31
|
+
}.to raise_error
|
32
|
+
end
|
16
33
|
|
34
|
+
it "returns nil when first level try returns nil" do
|
35
|
+
mock_it = double(Object)
|
36
|
+
mock_it.stub(:derp) { nil }
|
37
|
+
::TryChain.proxy_try_chain!(mock_it, :derp, :derp).should be_nil
|
38
|
+
end
|
17
39
|
end
|
18
40
|
|
19
41
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: try_chain
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandon Dewitt
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|