deterministic 0.3.0 → 0.4.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: 73c7677d88bfefa44b35f3001d00eec4ae4d57ba
4
- data.tar.gz: c7ea57c2f75c73203303428eb241401951b6e974
3
+ metadata.gz: 16ab904730d38a8b5b0e3b3a2e6a8e40f3bf68fa
4
+ data.tar.gz: 29b690c3723bfcce5c2cfe40fcc03db7059aee5f
5
5
  SHA512:
6
- metadata.gz: 54f36f7a7868f74c04335c3e4d989ee1f5df9263df5bd917f947fed073ae18dc2c218716e56b34eb565f2efcedbad1e4fdcb97b7a695bbc24590bab37b0c5a41
7
- data.tar.gz: 3f6065134f25fbe1e36e432466d8c7463557674169911aceeefdfc14860ec7903568f08896eb19a3cdeca61fb8eb09170904b54e6974880dc3e787c2c5a9ea53
6
+ metadata.gz: 8042eed9f812f6570fbfa18dfdb2f5245d53bbd2419dbc44fdefb7a760d5da575b19064ced2723bc82724fd4866767728951e73e89ccb3a157a1912db1d89690
7
+ data.tar.gz: 62e5ec295f1d1535b77cb271def694ecd8d22291e0ed39ad6f708539273cc8dca03ea81af277375f139690b1e912cef4da30f0a251d3063534bec27c9c7ac4cd
data/README.md CHANGED
@@ -193,16 +193,30 @@ end # => Success(2)
193
193
  The simplest NullObject wrapper there can be. It adds `#some?` and `#none?` to `Object` though.
194
194
 
195
195
  ```ruby
196
- maybe(nil).foo # => None
197
- maybe(nil).foo.bar # => None
196
+ require 'deterministic/maybe' # you need to do this explicitly
197
+ Maybe(nil).foo # => None
198
+ Maybe(nil).foo.bar # => None
199
+ Mmaybe({a: 1})[:a] # => 1
198
200
 
199
- maybe({a: 1})[:a] # => 1
201
+ Maybe(nil).none? # => true
202
+ Maybe({}).none? # => false
200
203
 
201
- maybe(nil).none? # => true
202
- maybe({}).none? # => false
204
+ Maybe(nil).some? # => false
205
+ Maybe({}).some? # => true
206
+ ```
207
+
208
+ ## Mimic
209
+
210
+ If you want a custom NullObject which mimicks another class
211
+
212
+ ```ruby
213
+ class Mimick
214
+ def test; end
215
+ end
203
216
 
204
- maybe(nil).some? # => false
205
- maybe({}).some? # => true
217
+ null = Maybe.mimick(Mimick)
218
+ null.test # => None
219
+ null.foo # => NoMethodError
206
220
  ```
207
221
 
208
222
  ## Inspirations
@@ -211,6 +225,7 @@ maybe({}).some? # => true
211
225
  * [Pithyless' rumblings](https://gist.github.com/pithyless/2216519)
212
226
  * [either by rsslldnphy](https://github.com/rsslldnphy/either)
213
227
  * [Functors, Applicatives, And Monads In Pictures](http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html)
228
+ * [Naught by avdi](https://github.com/avdi/naught/)
214
229
 
215
230
  ## Installation
216
231
 
@@ -1,13 +1,40 @@
1
1
  # The simplest NullObject there can be
2
2
  class None
3
- def self.instance
4
- @instance ||= None.new
3
+ class << self
4
+ def method_missing(m, *args)
5
+ if m == :new
6
+ super
7
+ else
8
+ None.instance.send(m, *args)
9
+ end
10
+ end
11
+
12
+ def instance
13
+ @instance ||= new([])
14
+ end
15
+
16
+ def mimic(klas)
17
+ new(klas.instance_methods(false))
18
+ end
19
+ end
20
+ private_class_method :new
21
+
22
+ def initialize(methods)
23
+ @methods = methods
5
24
  end
6
25
 
7
- # def respond_to_missing
26
+ # implicit conversions
27
+ def to_str
28
+ ''
29
+ end
30
+
31
+ def to_ary
32
+ []
33
+ end
8
34
 
9
- def method_missing(*args)
10
- self
35
+ def method_missing(m, *args)
36
+ return self if respond_to?(m)
37
+ super
11
38
  end
12
39
 
13
40
  def none?
@@ -19,7 +46,8 @@ class None
19
46
  end
20
47
 
21
48
  def respond_to?(m)
22
- true
49
+ return true if @methods.empty? || @methods.include?(m)
50
+ super
23
51
  end
24
52
  end
25
53
 
@@ -33,6 +61,6 @@ class Object
33
61
  end
34
62
  end
35
63
 
36
- def maybe(obj)
64
+ def Maybe(obj)
37
65
  obj.nil? ? None.instance : obj
38
66
  end
@@ -1,3 +1,3 @@
1
1
  module Deterministic
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -3,17 +3,45 @@ require 'deterministic/maybe'
3
3
 
4
4
  describe 'maybe' do
5
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
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
14
  end
15
15
 
16
- it 'Nothing#to_s' do
16
+ it "None is a Singleton" do
17
+ expect(None.instance).to be_a None
18
+ expect { None.new }.to raise_error(NoMethodError, "private method `new' called for None:Class")
19
+ end
20
+
21
+ it "implicit conversions" do
22
+ null = Maybe(nil)
23
+ expect(null.to_str).to eq ""
24
+ expect(null.to_ary).to eq []
25
+ expect("" + null).to eq ""
26
+
27
+ a, b, c = null
28
+ expect(a).to be_nil
29
+ expect(b).to be_nil
30
+ expect(c).to be_nil
31
+ end
32
+
33
+ it "explicit conversions" do
17
34
  expect(None.to_s).to eq 'None'
18
35
  end
36
+
37
+ it "mimic, only return None on specific methods of another class" do
38
+ class MimicTest
39
+ def test
40
+ end
41
+ end
42
+
43
+ mimic = None.mimic(MimicTest)
44
+ expect(mimic.test).to be_none
45
+ expect { mimic.foo }.to raise_error(NoMethodError)
46
+ end
19
47
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deterministic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Zolnierek