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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 14d6a789436ec1604723c95725e3487392024b83
4
- data.tar.gz: d52cd81401591846e63d971fa3502fc6dc5b5052
3
+ metadata.gz: 860a265e5780c5528761f44f949e04751d3a41d2
4
+ data.tar.gz: b4b2f922ab71513630eca2896e3c540f09371f96
5
5
  SHA512:
6
- metadata.gz: 64a0bd52f604658ff9c5ceab1fe7652ec413f4fa3110ce52e3bdacbe23673899ad04a70b46cb7d62a0a6d49f167bcedf7a4a222dd184e6600eec87902519a328
7
- data.tar.gz: 3f82ad4034acf35be5d78da858a80cc5d2bf847f448de0f02a31102a6e92eaf87270baa434745d4be83ee704df95a13bc6b20d9a83e4f9691ec91dd9fd01304d
6
+ metadata.gz: 651632272c2fe6be77d97d1f8215f19080f07223a589d0012bb8096b7edf2824d143ff5e8975812fbfc1ff02f9323e4dea36d966e5e4af837a609f69ff2a9712
7
+ data.tar.gz: 525b87f19d85383389dfd53e501bfab796ca6fa5ec0d3b93d67713270aab3736301f62263d3dc3e267ee30bc361fe0ce810d5691326493ce922ddfcc4bfbbd8e
@@ -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
@@ -1,3 +1,3 @@
1
1
  module TryChain
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -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.1
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-15 00:00:00.000000000 Z
11
+ date: 2013-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport