maybe-chain 0.0.1 → 0.0.3

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.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Maybe::Chain
1
+ # maybe-chain
2
2
 
3
3
  ## Installation
4
4
 
@@ -20,7 +20,7 @@ Or install it yourself as:
20
20
  m1 = "a".to_maybe.upcase.gsub(/A/, "B")
21
21
 
22
22
  maybe(m1) do |str|
23
- puts str # => a
23
+ puts str # => B
24
24
  end
25
25
 
26
26
  m2 = nil.to_maybe.upcase.gsub(/A/, "B")
@@ -32,6 +32,18 @@ end
32
32
  maybe(m2, "a") do |str|
33
33
  puts str # => a
34
34
  end
35
+
36
+ m3 = [1,2,3].to_maybe.map {|i| i * 2}.reject {|i| i > 5}
37
+
38
+ maybe(m3) do |arr|
39
+ p arr # => [2, 4]
40
+ end
41
+
42
+ m4 = "a".to_maybe(NoMethodError).upcase.no_method.gsub(/A/, "B")
43
+
44
+ maybe(m4) do |str|
45
+ puts str # => No Execute
46
+ end
35
47
  ```
36
48
 
37
49
  ## Contributing
data/lib/maybe-chain.rb CHANGED
@@ -4,7 +4,15 @@ require "singleton"
4
4
 
5
5
  module MaybeChain
6
6
  class MaybeWrapper < Delegator
7
- def initialize(obj)
7
+ def initialize(obj, rescuables = [])
8
+ raise ArgumentError unless (rescuables.is_a?(Class) && rescuables <= Exception) || rescuables.is_a?(Array)
9
+
10
+ if rescuables.is_a?(Exception)
11
+ @rescuables = [rescuables]
12
+ else
13
+ @rescuables = rescuables
14
+ end
15
+
8
16
  if obj.nil?
9
17
  @obj = Nothing.instance
10
18
  else
@@ -18,14 +26,16 @@ module MaybeChain
18
26
 
19
27
  def method_missing(m, *args, &block)
20
28
  if nothing?
21
- self.__getobj__.__send__(m, *args, &block)
29
+ self.__getobj__.__send__(m, @rescuables, &block)
22
30
  else
23
- MaybeWrapper.new(super)
31
+ MaybeWrapper.new(super, @rescuables)
24
32
  end
33
+ rescue *@rescuables
34
+ MaybeWrapper.new(Nothing.instance, @rescuables)
25
35
  end
26
36
 
27
37
  def nothing?
28
- @obj.class == MaybeChain::Nothing
38
+ @obj.is_a? Nothing
29
39
  end
30
40
 
31
41
  def just?
@@ -45,13 +55,14 @@ module MaybeChain
45
55
  include Singleton
46
56
 
47
57
  def method_missing(method, *args, &block)
48
- MaybeWrapper.new(self)
58
+ rescuables = args.first || []
59
+ MaybeWrapper.new(self, rescuables)
49
60
  end
50
61
  end
51
62
 
52
63
  module ObjectExtend
53
- def to_maybe
54
- MaybeChain::MaybeWrapper.new(self)
64
+ def to_maybe(rescuables = [])
65
+ MaybeChain::MaybeWrapper.new(self, rescuables)
55
66
  end
56
67
  end
57
68
 
@@ -1,3 +1,3 @@
1
1
  module MaybeChain
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -3,6 +3,10 @@ $: << File.join(File.dirname(File.expand_path(__FILE__)), "..", "..", "lib")
3
3
  require "maybe-chain"
4
4
 
5
5
  describe MaybeChain do
6
+ before do
7
+ String.__send__ :include, MaybeChain::TestMethod
8
+ end
9
+
6
10
  describe "Object Extension" do
7
11
  subject { "a".to_maybe }
8
12
  it { should be_a(MaybeChain::MaybeWrapper) }
@@ -13,13 +17,53 @@ describe MaybeChain do
13
17
  its(:value) { should eq "A" }
14
18
 
15
19
  context "method returns nil" do
16
- let(:string) { "a".tap {|s| s.extend(MaybeChain::TestMethod)} }
20
+ let(:string) { "a" }
17
21
 
18
22
  subject { string.to_maybe.return_nil.upcase }
19
23
  its(:value) { should be_nil }
20
24
  end
25
+
26
+ context "given block" do
27
+ let(:array) { [1, 2, 3] }
28
+
29
+ subject { array.to_maybe.map {|i| i*2}.reject {|i| i > 5} }
30
+ its(:value) { should eq [2, 4] }
31
+ end
32
+
33
+ context "method raise Exception" do
34
+ context "to_maybe given rescuable Exception" do
35
+ let(:string) { "a" }
36
+
37
+ subject { string.to_maybe(NotImplementedError).raise_no_implement_error.upcase }
38
+ its(:value) { should be_nil }
39
+ end
40
+
41
+ context "to_maybe given Exception List" do
42
+ let(:string) { "a" }
43
+
44
+ subject { string.to_maybe([NotImplementedError, ArgumentError]).raise_no_implement_error.upcase }
45
+ its(:value) { should be_nil }
46
+ end
47
+
48
+ context "to_maybe given Exception List, and forward method raises Exception" do
49
+ let(:string) { "a" }
50
+
51
+ subject { string.to_maybe(NotImplementedError).upcase.raise_no_implement_error.downcase }
52
+ its(:value) { should be_nil }
53
+ end
54
+
55
+ context "to_maybe given Exception List, but different Exception raised" do
56
+ let(:string) { "a" }
57
+
58
+ it do
59
+ expect { string.to_maybe([ArgumentError]).raise_no_implement_error.upcase }.to raise_error(NotImplementedError)
60
+ end
61
+ end
62
+ end
21
63
  end
22
64
 
65
+ describe
66
+
23
67
  describe "maybe extraction" do
24
68
  include Kernel
25
69
 
@@ -52,4 +96,8 @@ module MaybeChain::TestMethod
52
96
  def return_nil
53
97
  nil
54
98
  end
99
+
100
+ def raise_no_implement_error
101
+ raise NotImplementedError
102
+ end
55
103
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maybe-chain
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
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: 2012-11-06 00:00:00.000000000 Z
12
+ date: 2012-11-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -71,12 +71,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
71
71
  - - ! '>='
72
72
  - !ruby/object:Gem::Version
73
73
  version: '0'
74
+ segments:
75
+ - 0
76
+ hash: 3746417588349921974
74
77
  required_rubygems_version: !ruby/object:Gem::Requirement
75
78
  none: false
76
79
  requirements:
77
80
  - - ! '>='
78
81
  - !ruby/object:Gem::Version
79
82
  version: '0'
83
+ segments:
84
+ - 0
85
+ hash: 3746417588349921974
80
86
  requirements: []
81
87
  rubyforge_project:
82
88
  rubygems_version: 1.8.23
@@ -85,4 +91,3 @@ specification_version: 3
85
91
  summary: In method chain, This gem makes failure more graceful
86
92
  test_files:
87
93
  - spec/lib/maybe-chain_spec.rb
88
- has_rdoc: