resonad 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/resonad.rb +44 -31
  3. data/lib/resonad/version.rb +2 -2
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 25157450da225c6390b1913ecee5c69b13025b32
4
- data.tar.gz: aa7cfe69d2b80c222da33ad2b6fab421f8bc79a0
3
+ metadata.gz: 350f8a5aeb12b9502c1a6c6e9bdc1b9741dde935
4
+ data.tar.gz: eb0c4b2ea23b9f589b46c4261ff85aae5ac2effd
5
5
  SHA512:
6
- metadata.gz: b2fe2862418ef2527b10cd97015807115d0a19168332ad3d9793b84f1591c5695f84d3331d8163d0265110023977d093d105c7495aeffed18a2cd0ebf881f3a3
7
- data.tar.gz: 1e8da46e15b5e841c46bea2a3a06590a74c85ea69493d65bc9f5b3178d9e0707a7fcaf97d7daab89a365c036979dbfcc5e0be45b188c1085410511f0478b7fd2
6
+ metadata.gz: 66aaebdf2b1ba74598c85e54d605f755d18672cf241f464a59fd640d2835feee7cd1f91ae7266d581391be0220b12a72ce6d19c5940af98823fab05b59fb3112
7
+ data.tar.gz: bfaf318864e49273aec6ca07f2200b42203cc78841f809511dc8afce51347ef19c23a6fda5cff13e43598c030be7c53ec5c437cf91a48398264f386bad620d61
data/lib/resonad.rb CHANGED
@@ -1,28 +1,19 @@
1
- module Resonad
1
+ class Resonad
2
2
  class NonExistentError < StandardError; end
3
3
  class NonExistentValue < StandardError; end
4
4
 
5
- def self.Success(*args)
6
- Success.new(*args)
7
- end
8
-
9
- def self.Failure(*args)
10
- Failure.new(*args)
11
- end
5
+ module Mixin
6
+ def Success(*args)
7
+ ::Resonad::Success.new(*args)
8
+ end
12
9
 
13
- def self.rescuing_from(*exception_classes)
14
- Success(yield)
15
- rescue Exception => e
16
- if exception_classes.empty?
17
- Failure(e) # rescue from all exceptions
18
- elsif exception_classes.any? { |klass| e.is_a?(klass) }
19
- Failure(e) # rescue from specified exception type
20
- else
21
- raise # reraise unhandled exception
10
+ def Failure(*args)
11
+ ::Resonad::Failure.new(*args)
22
12
  end
23
13
  end
14
+ extend Mixin
24
15
 
25
- class Success
16
+ class Success < Resonad
26
17
  attr_accessor :value
27
18
 
28
19
  def initialize(value)
@@ -34,10 +25,6 @@ module Resonad
34
25
  true
35
26
  end
36
27
 
37
- def failure?
38
- false
39
- end
40
-
41
28
  def on_success
42
29
  yield value
43
30
  self
@@ -67,15 +54,13 @@ module Resonad
67
54
  def flat_map
68
55
  yield value
69
56
  end
70
- alias_method :and_then, :flat_map
71
57
 
72
58
  def flat_map_error
73
59
  self
74
60
  end
75
- alias_method :or_else, :flat_map_error
76
61
  end
77
62
 
78
- class Failure
63
+ class Failure < Resonad
79
64
  attr_accessor :error
80
65
 
81
66
  def initialize(error)
@@ -87,10 +72,6 @@ module Resonad
87
72
  false
88
73
  end
89
74
 
90
- def failure?
91
- true
92
- end
93
-
94
75
  def on_success
95
76
  self
96
77
  end
@@ -120,12 +101,44 @@ module Resonad
120
101
  def flat_map
121
102
  self
122
103
  end
123
- alias_method :and_then, :flat_map
124
104
 
125
105
  def flat_map_error
126
106
  yield error
127
107
  end
128
- alias_method :or_else, :flat_map_error
129
108
  end
130
109
 
110
+ def self.rescuing_from(*exception_classes)
111
+ Success(yield)
112
+ rescue Exception => e
113
+ if exception_classes.empty?
114
+ Failure(e) # rescue from all exceptions
115
+ elsif exception_classes.any? { |klass| e.is_a?(klass) }
116
+ Failure(e) # rescue from specified exception type
117
+ else
118
+ raise # reraise unhandled exception
119
+ end
120
+ end
121
+
122
+ def initialize(*args)
123
+ raise NotImplementedError, "This is an abstract class. Use Resonad::Success or Resonad::Failure instead."
124
+ end
125
+
126
+ def success?
127
+ raise NotImplementedError, "should be implemented in subclass"
128
+ end
129
+
130
+ def failure?
131
+ not success?
132
+ end
133
+
134
+ def flat_map
135
+ raise NotImplementedError, "should be implemented in subclass"
136
+ end
137
+ alias_method :and_then, :flat_map
138
+
139
+ def flat_map_error
140
+ raise NotImplementedError, "should be implemented in subclass"
141
+ end
142
+ alias_method :or_else, :flat_map_error
143
+
131
144
  end
@@ -1,3 +1,3 @@
1
- module Resonad
2
- VERSION = '1.0.2'
1
+ class Resonad
2
+ VERSION = '1.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resonad
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Dalling
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-22 00:00:00.000000000 Z
11
+ date: 2017-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler