monad-maybe 0.9.2 → 0.9.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f4ff513fcc26d6a2a1034ca7628d787994f1f4c4
4
- data.tar.gz: a44413edb8240cea79f755c6374981424ca97d86
3
+ metadata.gz: 4b83febe6374c7006a9d08b6245aabdeb489b0ba
4
+ data.tar.gz: a1dcc208247490a63bccfd2664991ccee1616e1b
5
5
  SHA512:
6
- metadata.gz: c71be1ce7e5f7e6f487cbaefaac244128215218d83a5375954dab890e378e35836a38483dbd37629f624335d2ac803a13a02b1c75fa06d436ae9cffda302118d
7
- data.tar.gz: 6efd96a3d2581d1204c6dc0c0dd8268d305ff460515cf75228ad5c2c110c43b27128368fa93bf9e9481717d235e5cb3bb5729db2d172a2b3dd51c6cfab7f0487
6
+ metadata.gz: a589a26010e6e95de784bff1312d83e38c9b6a433135c36ef20a7cb04a44b2d03c862d1216b95b1e2b442738960e0f187611bd1cc70238802d72b46506e9cb74
7
+ data.tar.gz: 234a28c4ce91a6f91a2d7e47da0d3b4964c5c7a76d01ea60e3107359a20c8d282d326f31f69d97442b5a4b67e311b02dd3d1030a5eae64a67330e303c20ec0f7
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.9.2
1
+ 0.9.3
data/lib/monad/maybe.rb CHANGED
@@ -3,75 +3,70 @@ require_relative 'maybe/just'
3
3
  require_relative 'maybe/nothing'
4
4
  require_relative 'maybe/list'
5
5
 
6
- # Top-level module includes some monkey patching and
7
- # constructor methods. Monad::Maybe is included by default.
8
- module Monad
9
- module Maybe
10
- module Enumerable
11
- def to_maybe
12
- first.maybe
13
- end
6
+ #
7
+ # Some monkey patching and constructor methods.
8
+ #
9
+ module Enumerable
10
+ def to_maybe
11
+ first.maybe
12
+ end
14
13
 
15
- def maybe_map
16
- List.new(map{ |x| yield(x) })
17
- end
18
- end
14
+ def maybe_map
15
+ Monad::Maybe::List.new(map{ |x| yield(x) })
16
+ end
17
+ end
19
18
 
20
- class ::Array; include Enumerable end
21
- class ::Range; include Enumerable end
19
+ class Array; include Enumerable end
20
+ class Range; include Enumerable end
22
21
 
23
- class ::Object
24
- def maybe(obj=self, &blk)
25
- if obj && blk
26
- blk.call(obj).to_maybe
27
- else
28
- obj.to_maybe
29
- end
30
- end
22
+ class Object
23
+ def maybe(obj=self, &blk)
24
+ if obj && blk
25
+ blk.call(obj).to_maybe
26
+ else
27
+ obj.to_maybe
28
+ end
29
+ end
31
30
 
32
- def to_maybe
33
- Just.new(self)
34
- end
31
+ def to_maybe
32
+ Monad::Maybe::Just.new(self)
33
+ end
35
34
 
36
- def maybe?
37
- false
38
- end
35
+ def maybe?
36
+ false
37
+ end
39
38
 
40
- def just?
41
- false
42
- end
39
+ def just?
40
+ false
41
+ end
43
42
 
44
- def nothing?
45
- false
46
- end
47
-
48
- def something?(&blk)
49
- true
50
- end
51
- end
52
-
53
- class ::NilClass
54
- def to_maybe
55
- Nothing.instance.freeze
56
- end
43
+ def nothing?
44
+ false
45
+ end
57
46
 
58
- def maybe(&blk)
59
- to_maybe
60
- end
47
+ def something?(&blk)
48
+ true
49
+ end
50
+ end
61
51
 
62
- def something?
63
- false
64
- end
65
- end
52
+ class NilClass
53
+ def to_maybe
54
+ Monad::Maybe::Nothing.instance.freeze
55
+ end
66
56
 
67
- def just(o)
68
- Just.new(o)
69
- end
57
+ def maybe(&blk)
58
+ to_maybe
59
+ end
70
60
 
71
- def nothing
72
- Nothing.instance
73
- end
61
+ def something?
62
+ false
74
63
  end
75
64
  end
76
65
 
77
- include Monad::Maybe
66
+ def just(o)
67
+ Monad::Maybe::Just.new(o)
68
+ end
69
+
70
+ def nothing
71
+ Monad::Maybe::Nothing.instance
72
+ end
@@ -0,0 +1,26 @@
1
+ require 'json'
2
+
3
+ #
4
+ # Adds JSON conversions to Just, Nothing, and List
5
+ #
6
+ module Monad
7
+ module Maybe
8
+ class Just
9
+ def to_json(*args)
10
+ value.to_json(*args)
11
+ end
12
+ end
13
+
14
+ class Nothing
15
+ def to_json(*args)
16
+ 'null'
17
+ end
18
+ end
19
+
20
+ class List
21
+ def to_json(*args)
22
+ to_a.to_json(*args)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -1,10 +1,5 @@
1
1
  module Monad
2
2
  module Maybe
3
- #
4
- # Wraps a non-nil object allows us to treat these
5
- # objects as a Maybe while distinguishing them from
6
- # a Nothing
7
- #
8
3
  class Just < Base
9
4
  def initialize(value)
10
5
  @value = value
@@ -30,7 +25,7 @@ module Monad
30
25
  true
31
26
  end
32
27
 
33
- # NOTE: This being able to return Nothings maybe dangerous
28
+ # NOTE: being able to return Nothings maybe dangerous
34
29
  def maybe(&blk)
35
30
  if blk
36
31
  blk.call(self.value).to_maybe
@@ -60,8 +55,9 @@ module Monad
60
55
  end
61
56
 
62
57
  def to_s
63
- Just.new(value.to_s)
58
+ value.to_s
64
59
  end
60
+ alias to_str to_s
65
61
 
66
62
  def to_a
67
63
  [self]
@@ -2,9 +2,6 @@ require 'singleton'
2
2
 
3
3
  module Monad
4
4
  module Maybe
5
- #
6
- # A better nil
7
- #
8
5
  class Nothing < Base
9
6
  include ::Singleton
10
7
 
@@ -48,10 +45,6 @@ module Monad
48
45
  nil
49
46
  end
50
47
 
51
- def to_json(*args)
52
- 'null'
53
- end
54
-
55
48
  def to_s
56
49
  ''
57
50
  end
data/monad-maybe.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "monad-maybe"
8
- s.version = "0.9.2"
8
+ s.version = "0.9.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Delon Newman"]
@@ -23,6 +23,7 @@ Gem::Specification.new do |s|
23
23
  "VERSION",
24
24
  "lib/monad/maybe.rb",
25
25
  "lib/monad/maybe/base.rb",
26
+ "lib/monad/maybe/json.rb",
26
27
  "lib/monad/maybe/just.rb",
27
28
  "lib/monad/maybe/list.rb",
28
29
  "lib/monad/maybe/nothing.rb",
data/test/maybe.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'test/unit'
2
2
  require_relative '../lib/monad/maybe'
3
+ require_relative '../lib/monad/maybe/json'
3
4
 
4
5
  class MaybeTest < Test::Unit::TestCase
5
6
  def test_nothing
@@ -13,6 +14,15 @@ class MaybeTest < Test::Unit::TestCase
13
14
  assert 1.maybe.just?
14
15
  assert_equal 1, 1.maybe.value
15
16
  assert_equal 1, 1.maybe.unwrap('test')
17
+ assert_equal '1', just(1).to_s
18
+ end
19
+
20
+ def test_json
21
+ assert_equal 'null', nothing.to_json
22
+ assert_equal '1', just(1).to_json
23
+ assert_equal '{}', just({}).to_json
24
+ assert_equal '[]', just([]).to_json
25
+ assert_equal (0..10).to_a.to_json, (0..10).maybe_map { |n| n }.to_json
16
26
  end
17
27
 
18
28
  def test_to_a
@@ -29,8 +39,8 @@ class MaybeTest < Test::Unit::TestCase
29
39
  end
30
40
 
31
41
  def test_class
32
- assert_equal Nothing, nil.maybe.class
33
- assert_equal Just, 1.maybe.class
42
+ assert_equal Monad::Maybe::Nothing, nil.maybe.class
43
+ assert_equal Monad::Maybe::Just, 1.maybe.class
34
44
  end
35
45
 
36
46
  def test_list
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monad-maybe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Delon Newman
@@ -66,6 +66,7 @@ files:
66
66
  - VERSION
67
67
  - lib/monad/maybe.rb
68
68
  - lib/monad/maybe/base.rb
69
+ - lib/monad/maybe/json.rb
69
70
  - lib/monad/maybe/just.rb
70
71
  - lib/monad/maybe/list.rb
71
72
  - lib/monad/maybe/nothing.rb