Class: Railsmith::Result
- Inherits:
-
Object
- Object
- Railsmith::Result
- Defined in:
- lib/railsmith/result.rb
Overview
An immutable success/failure wrapper with a stable serialization contract.
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
- .failure(code: nil, message: nil, details: nil, error: nil, meta: nil) ⇒ Object
- .success(value: nil, meta: nil) ⇒ Object
Instance Method Summary collapse
- #and_then ⇒ Object
- #as_json ⇒ Object
- #code ⇒ Object
- #failure? ⇒ Boolean
-
#initialize(success:, value:, error:, meta:) ⇒ Result
constructor
A new instance of Result.
- #on_failure {|@error| ... } ⇒ Object
- #on_success {|@value| ... } ⇒ Object
- #or_else ⇒ Object
- #success? ⇒ Boolean
- #to_h ⇒ Object
Constructor Details
#initialize(success:, value:, error:, meta:) ⇒ Result
Returns a new instance of Result.
24 25 26 27 28 29 30 |
# File 'lib/railsmith/result.rb', line 24 def initialize(success:, value:, error:, meta:) @success = success ? true : false @value = value @error = error @meta = || {} freeze end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
40 41 42 |
# File 'lib/railsmith/result.rb', line 40 def error @error end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
40 41 42 |
# File 'lib/railsmith/result.rb', line 40 def @meta end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
40 41 42 |
# File 'lib/railsmith/result.rb', line 40 def value @value end |
Class Method Details
.failure(code: nil, message: nil, details: nil, error: nil, meta: nil) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/railsmith/result.rb', line 10 def self.failure(code: nil, message: nil, details: nil, error: nil, meta: nil) normalized_error = error || Errors::ErrorPayload.new( code: code || :unexpected, message: || "Unexpected error", details: ) new(success: false, value: nil, error: normalized_error, meta: || {}) end |
.success(value: nil, meta: nil) ⇒ Object
6 7 8 |
# File 'lib/railsmith/result.rb', line 6 def self.success(value: nil, meta: nil) new(success: true, value:, error: nil, meta: || {}) end |
Instance Method Details
#and_then ⇒ Object
48 49 50 51 52 53 |
# File 'lib/railsmith/result.rb', line 48 def and_then return self if failure? chained = yield(@value) (chained) end |
#as_json ⇒ Object
80 81 82 |
# File 'lib/railsmith/result.rb', line 80 def as_json(*) to_h end |
#code ⇒ Object
42 43 44 45 46 |
# File 'lib/railsmith/result.rb', line 42 def code return nil if error.nil? error.code end |
#failure? ⇒ Boolean
36 37 38 |
# File 'lib/railsmith/result.rb', line 36 def failure? !success? end |
#on_failure {|@error| ... } ⇒ Object
67 68 69 70 |
# File 'lib/railsmith/result.rb', line 67 def on_failure yield(@error) if failure? self end |
#on_success {|@value| ... } ⇒ Object
62 63 64 65 |
# File 'lib/railsmith/result.rb', line 62 def on_success yield(@value) if success? self end |
#or_else ⇒ Object
55 56 57 58 59 60 |
# File 'lib/railsmith/result.rb', line 55 def or_else return self if success? chained = yield(@error) (chained) end |
#success? ⇒ Boolean
32 33 34 |
# File 'lib/railsmith/result.rb', line 32 def success? @success end |
#to_h ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/railsmith/result.rb', line 72 def to_h if success? { success: true, value:, meta: } else { success: false, error: error.to_h, meta: } end end |