deterministic 0.2.0 → 0.3.0

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: a99a96ae91c0ed68fd47e14ba991e03c1302349c
4
- data.tar.gz: c9de7570b1ef16aab2b10ee4aa97f927d7661048
3
+ metadata.gz: 73c7677d88bfefa44b35f3001d00eec4ae4d57ba
4
+ data.tar.gz: c7ea57c2f75c73203303428eb241401951b6e974
5
5
  SHA512:
6
- metadata.gz: c15b124c022ae997e29be36a63237cb50963268c6642d6f55c6f3a5e79f5de047b76182d3eb3d69b57b3a4e6ef207e9ace98b3579613154788fb706429cd750f
7
- data.tar.gz: efe04519bfbca769720df381304134c72b4e21ebea770aabb1f0dff73dadca7bb98e0735f90ae0f569e7182fdba7ffd6d4f736ba5c238053a589e5cd55e42324
6
+ metadata.gz: 54f36f7a7868f74c04335c3e4d989ee1f5df9263df5bd917f947fed073ae18dc2c218716e56b34eb565f2efcedbad1e4fdcb97b7a695bbc24590bab37b0c5a41
7
+ data.tar.gz: 3f6065134f25fbe1e36e432466d8c7463557674169911aceeefdfc14860ec7903568f08896eb19a3cdeca61fb8eb09170904b54e6974880dc3e787c2c5a9ea53
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Deterministic
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/deterministic.png)](http://badge.fury.io/rb/deterministic)
4
+
3
5
  This is a spiritual successor of the [Monadic gem](http://github.com/pzol/monadic). The goal of the rewrite is to get away from a bit to forceful aproach I took in Monadic, especially when it comes to coercing monads, but also a more practical but at the same time more strict adherence to monad laws.
4
6
 
5
7
  This gem is still __WORK IN PROGRESS__.
@@ -153,6 +155,7 @@ class UnderTest
153
155
  end
154
156
 
155
157
  ut = UnderTest.new
158
+ ut.test # => Success(1)
156
159
  ```
157
160
 
158
161
  To add it to all classes
@@ -173,6 +176,7 @@ class UnderTest
173
176
  end
174
177
 
175
178
  ut = UnderTest.new
179
+ ut.test # => Success(1)
176
180
  ```
177
181
 
178
182
  or use it on built-in classes
@@ -181,14 +185,30 @@ or use it on built-in classes
181
185
  require "deterministic/core_ext/object/either"
182
186
  h = {a:1}
183
187
  h.attempt_all do
184
- try { |s| self[:a] + 1}
185
- end
188
+ try { |s| s[:a] + 1}
189
+ end # => Success(2)
190
+ ```
191
+
192
+ ## Maybe
193
+ The simplest NullObject wrapper there can be. It adds `#some?` and `#none?` to `Object` though.
194
+
195
+ ```ruby
196
+ maybe(nil).foo # => None
197
+ maybe(nil).foo.bar # => None
198
+
199
+ maybe({a: 1})[:a] # => 1
200
+
201
+ maybe(nil).none? # => true
202
+ maybe({}).none? # => false
203
+
204
+ maybe(nil).some? # => false
205
+ maybe({}).some? # => true
186
206
  ```
187
207
 
188
208
  ## Inspirations
189
209
  * My [Monadic gem](http://github.com/pzol/monadic) of course
190
210
  * `#attempt_all` was somewhat inspired by [An error monad in Clojure](http://brehaut.net/blog/2011/error_monads)
191
- * [Pithyless' rumblings](https://gist.github.com/pithyless/2216519)
211
+ * [Pithyless' rumblings](https://gist.github.com/pithyless/2216519)
192
212
  * [either by rsslldnphy](https://github.com/rsslldnphy/either)
193
213
  * [Functors, Applicatives, And Monads In Pictures](http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html)
194
214
 
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
- spec.add_development_dependency "rspec", "~>3.0.0.beta1"
23
+ spec.add_development_dependency "rspec", "~>3.0.0.beta2"
24
24
  spec.add_development_dependency "guard"
25
25
  spec.add_development_dependency "guard-bundler"
26
26
  spec.add_development_dependency "guard-rspec"
@@ -0,0 +1,38 @@
1
+ # The simplest NullObject there can be
2
+ class None
3
+ def self.instance
4
+ @instance ||= None.new
5
+ end
6
+
7
+ # def respond_to_missing
8
+
9
+ def method_missing(*args)
10
+ self
11
+ end
12
+
13
+ def none?
14
+ true
15
+ end
16
+
17
+ def some?
18
+ false
19
+ end
20
+
21
+ def respond_to?(m)
22
+ true
23
+ end
24
+ end
25
+
26
+ class Object
27
+ def none?
28
+ false
29
+ end
30
+
31
+ def some?
32
+ true
33
+ end
34
+ end
35
+
36
+ def maybe(obj)
37
+ obj.nil? ? None.instance : obj
38
+ end
@@ -1,3 +1,3 @@
1
1
  module Deterministic
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -24,7 +24,7 @@ describe Deterministic::CoreExt::Either do
24
24
  h = {a: 1}
25
25
  h.extend(Deterministic::CoreExt::Either)
26
26
  res = h.attempt_all do
27
- try { |s| self[:a] + 1}
27
+ try { |s| s[:a] + 1}
28
28
  end
29
29
 
30
30
  expect(res).to eq Deterministic::Success(2)
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+ require 'deterministic/maybe'
3
+
4
+ describe 'maybe' do
5
+ it "does something" do
6
+ expect(maybe(nil).foo).to be_none
7
+ expect(maybe(nil).foo.bar.baz).to be_none
8
+ expect(maybe(nil).fetch(:a)).to be_none
9
+ expect(maybe(1)).to be_some
10
+ expect(maybe({a: 1}).fetch(:a)).to eq 1
11
+ expect(maybe({a: 1})[:a]).to eq 1
12
+ expect(maybe("a").upcase).to eq "A"
13
+ expect(maybe("a")).not_to be_none
14
+ end
15
+
16
+ it 'Nothing#to_s' do
17
+ expect(None.to_s).to eq 'None'
18
+ end
19
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deterministic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Zolnierek
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-27 00:00:00.000000000 Z
11
+ date: 2014-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - ~>
46
46
  - !ruby/object:Gem::Version
47
- version: 3.0.0.beta1
47
+ version: 3.0.0.beta2
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
- version: 3.0.0.beta1
54
+ version: 3.0.0.beta2
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: guard
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -132,6 +132,7 @@ files:
132
132
  - lib/deterministic/either/failure.rb
133
133
  - lib/deterministic/either/match.rb
134
134
  - lib/deterministic/either/success.rb
135
+ - lib/deterministic/maybe.rb
135
136
  - lib/deterministic/monad.rb
136
137
  - lib/deterministic/version.rb
137
138
  - spec/examples/bookings_spec.rb
@@ -142,6 +143,7 @@ files:
142
143
  - spec/lib/deterministic/either/match_spec.rb
143
144
  - spec/lib/deterministic/either/success_spec.rb
144
145
  - spec/lib/deterministic/either_spec.rb
146
+ - spec/lib/deterministic/maybe_spec.rb
145
147
  - spec/lib/deterministic/monad_axioms.rb
146
148
  - spec/lib/deterministic/monad_spec.rb
147
149
  - spec/spec_helper.rb
@@ -178,6 +180,7 @@ test_files:
178
180
  - spec/lib/deterministic/either/match_spec.rb
179
181
  - spec/lib/deterministic/either/success_spec.rb
180
182
  - spec/lib/deterministic/either_spec.rb
183
+ - spec/lib/deterministic/maybe_spec.rb
181
184
  - spec/lib/deterministic/monad_axioms.rb
182
185
  - spec/lib/deterministic/monad_spec.rb
183
186
  - spec/spec_helper.rb