/
result.rb
82 lines (74 loc) · 1.51 KB
/
result.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
module ROM
module Commands
# Abstract result class for success and error results
#
# @public
class Result
# Return command execution result
#
# @api public
attr_reader :value
# Return potential command execution result error
#
# @api public
attr_reader :error
# Coerce result to an array
#
# @abstract
#
# @api public
def to_ary
raise NotImplementedError
end
alias_method :to_a, :to_ary
# Success result has a value and no error
#
# @public
class Success < Result
# @api private
def initialize(value)
@value = value
end
# Call next command on continuation
#
# @api public
def >(other)
other.call(value)
end
# Return the value
#
# @return [Array]
#
# @api public
def to_ary
value.to_ary
end
end
# Failure result has an error and no value
#
# @public
class Failure < Result
# @api private
def initialize(error)
@error = error
end
# Do not call next command on continuation
#
# @return [self]
#
# @api public
def >(_other)
self
end
# Return the error
#
# @return [Array<CommandError>]
#
# @api public
def to_ary
error
end
end
end
end
end