-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.rb
86 lines (72 loc) · 2.37 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
83
84
85
86
# frozen_string_literal: true
module Nanites
# Command result
class Result
# Defines result status constants
module States
UNKNOWN = Option.maybe
SUCCESS = Option.some true
ERROR = Option.none
class << self
# Is given code a valid status code?
# @return [Boolean]
def valid_status?(code)
constants(false).each do |constant|
return true if States.const_get(constant).eql?(code)
end
false
end
end
end
attr_reader :messages, :option, :status
# @param [Object] option
# @param [States] status
def initialize(option, status = States::UNKNOWN, *messages)
raise ArgumentError, 'Payload must be an "Option"' unless option.is_a?(Nanites::Option)
raise ArgumentError, "Unknown status code '#{status}'" unless States.valid_status?(status)
@option = option
@messages = messages
@status = status
end
# Does the result indicate some error?
def error?
@status.eql? States::ERROR
end
# Does the result indicate successful command execution?
def success?
@status.eql? States::SUCCESS
end
# Does the result indicate unknown command execution?
def unknown?
@status.eql?(States::UNKNOWN) || (!error? && !success?)
end
class << self
# Create result object indicating an erroneous command execution
# @param [Object] value optional payload
# @param [Array] messages optional informational messages
# @return [Nanites::Result]
def error(value, *messages)
Result.new option_for_payload(value), States::ERROR, *messages
end
# Create successful result object
# @param [Object] value optional payload
# @param [Array] messages optional informational messages
# @return [Nanites::Result]
def success(value, *messages)
Result.new option_for_payload(value), States::SUCCESS, *messages
end
# Create unknown result
# @return [Nanites::Result]
def unknown
Result.new Option.none, States::UNKNOWN, 'Undefined result'
end
private
# Helper returning an [Option] containing the actual payload
# @param [Object] value The value to be wrapped
# @return [Option]
def option_for_payload(value)
value ? Option.some(value) : Option.none
end
end
end
end