maybe-chain 0.0.3 → 0.0.4
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/.travis.yml +5 -0
- data/README.md +1 -1
- data/Rakefile +3 -0
- data/lib/maybe-chain.rb +41 -23
- data/lib/maybe-chain/version.rb +1 -1
- data/maybe-chain.gemspec +1 -0
- data/spec/lib/maybe-chain_spec.rb +26 -4
- metadata +21 -4
data/.travis.yml
ADDED
data/README.md
CHANGED
data/Rakefile
CHANGED
data/lib/maybe-chain.rb
CHANGED
@@ -13,17 +13,21 @@ module MaybeChain
|
|
13
13
|
@rescuables = rescuables
|
14
14
|
end
|
15
15
|
|
16
|
-
|
17
|
-
@obj = Nothing.instance
|
18
|
-
else
|
19
|
-
@obj = obj
|
20
|
-
end
|
16
|
+
@obj = obj.nil? ? Nothing.instance : obj
|
21
17
|
end
|
22
18
|
|
23
19
|
def __getobj__
|
24
20
|
@obj
|
25
21
|
end
|
26
22
|
|
23
|
+
def inspect
|
24
|
+
"<Maybe: #{@obj.inspect}>"
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_s
|
28
|
+
@obj.to_s
|
29
|
+
end
|
30
|
+
|
27
31
|
def method_missing(m, *args, &block)
|
28
32
|
if nothing?
|
29
33
|
self.__getobj__.__send__(m, @rescuables, &block)
|
@@ -31,7 +35,7 @@ module MaybeChain
|
|
31
35
|
MaybeWrapper.new(super, @rescuables)
|
32
36
|
end
|
33
37
|
rescue *@rescuables
|
34
|
-
|
38
|
+
build_nothing
|
35
39
|
end
|
36
40
|
|
37
41
|
def nothing?
|
@@ -43,17 +47,36 @@ module MaybeChain
|
|
43
47
|
end
|
44
48
|
|
45
49
|
def value
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
50
|
+
nothing? ? nil : @obj
|
51
|
+
end
|
52
|
+
|
53
|
+
def lift(method_name, *args, &block)
|
54
|
+
return build_nothing if nothing?
|
55
|
+
return build_nothing if args.any?(&:nothing?)
|
56
|
+
|
57
|
+
extracts = args.map {|arg| arg.is_a?(MaybeChain::MaybeWrapper) ? arg.value : arg}
|
58
|
+
MaybeWrapper.new(value.__send__(method_name, *extracts, &block), @rescuables)
|
59
|
+
rescue *@rescuables
|
60
|
+
build_nothing
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
def build_nothing
|
65
|
+
self.class.new(Nothing.instance, @rescuables)
|
51
66
|
end
|
52
67
|
end
|
53
68
|
|
54
69
|
class Nothing
|
55
70
|
include Singleton
|
56
71
|
|
72
|
+
def inspect
|
73
|
+
"Nothing"
|
74
|
+
end
|
75
|
+
|
76
|
+
def to_s
|
77
|
+
"Nothing"
|
78
|
+
end
|
79
|
+
|
57
80
|
def method_missing(method, *args, &block)
|
58
81
|
rescuables = args.first || []
|
59
82
|
MaybeWrapper.new(self, rescuables)
|
@@ -67,22 +90,17 @@ module MaybeChain
|
|
67
90
|
end
|
68
91
|
|
69
92
|
module KernelExtend
|
70
|
-
def maybe(maybe_wrapper, default = nil, &block)
|
71
|
-
if maybe_wrapper.just?
|
72
|
-
block.call(maybe_wrapper.value)
|
73
|
-
elsif default
|
74
|
-
block.call(default)
|
75
|
-
end
|
76
|
-
end
|
77
93
|
end
|
78
94
|
end
|
79
95
|
|
80
96
|
Object.__send__ :include, MaybeChain::ObjectExtend
|
81
97
|
|
82
98
|
module Kernel
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
99
|
+
def maybe(maybe_wrapper, default = nil, &block)
|
100
|
+
if maybe_wrapper.just?
|
101
|
+
block.call(maybe_wrapper.value)
|
102
|
+
elsif default
|
103
|
+
block.call(default)
|
104
|
+
end
|
105
|
+
end
|
88
106
|
end
|
data/lib/maybe-chain/version.rb
CHANGED
data/maybe-chain.gemspec
CHANGED
@@ -62,11 +62,7 @@ describe MaybeChain do
|
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
|
-
describe
|
66
|
-
|
67
65
|
describe "maybe extraction" do
|
68
|
-
include Kernel
|
69
|
-
|
70
66
|
context "maybe is just" do
|
71
67
|
let(:maybe_obj) { "a".to_maybe.upcase }
|
72
68
|
it "execute block" do
|
@@ -92,6 +88,32 @@ describe MaybeChain do
|
|
92
88
|
end
|
93
89
|
end
|
94
90
|
|
91
|
+
describe "#lift" do
|
92
|
+
subject { 1.to_maybe.lift(:+, 2.to_maybe) }
|
93
|
+
|
94
|
+
it { should be_a(MaybeChain::MaybeWrapper) }
|
95
|
+
|
96
|
+
its(:value) { should eq 3 }
|
97
|
+
|
98
|
+
context "given block" do
|
99
|
+
subject { [1, 2, 3].to_maybe.lift(:inject, 0.to_maybe) do |acc, i|
|
100
|
+
acc += i
|
101
|
+
end }
|
102
|
+
|
103
|
+
it { should be_a(MaybeChain::MaybeWrapper) }
|
104
|
+
|
105
|
+
its(:value) { should eq 6 }
|
106
|
+
end
|
107
|
+
|
108
|
+
context "arguments have nothing" do
|
109
|
+
subject { 1.to_maybe.lift(:+, nil.to_maybe) }
|
110
|
+
|
111
|
+
it { should be_a(MaybeChain::MaybeWrapper) }
|
112
|
+
|
113
|
+
its(:value) { should eq nil }
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
95
117
|
module MaybeChain::TestMethod
|
96
118
|
def return_nil
|
97
119
|
nil
|
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.
|
4
|
+
version: 0.0.4
|
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-
|
12
|
+
date: 2012-11-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -27,6 +27,22 @@ dependencies:
|
|
27
27
|
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: 2.11.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec-formatter-git_auto_commit
|
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'
|
30
46
|
- !ruby/object:Gem::Dependency
|
31
47
|
name: rake
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|
@@ -51,6 +67,7 @@ extensions: []
|
|
51
67
|
extra_rdoc_files: []
|
52
68
|
files:
|
53
69
|
- .gitignore
|
70
|
+
- .travis.yml
|
54
71
|
- Gemfile
|
55
72
|
- LICENSE
|
56
73
|
- README.md
|
@@ -73,7 +90,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
73
90
|
version: '0'
|
74
91
|
segments:
|
75
92
|
- 0
|
76
|
-
hash:
|
93
|
+
hash: -173041830012916601
|
77
94
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
95
|
none: false
|
79
96
|
requirements:
|
@@ -82,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
99
|
version: '0'
|
83
100
|
segments:
|
84
101
|
- 0
|
85
|
-
hash:
|
102
|
+
hash: -173041830012916601
|
86
103
|
requirements: []
|
87
104
|
rubyforge_project:
|
88
105
|
rubygems_version: 1.8.23
|