Class: Interceptors::Result
- Inherits:
-
Object
- Object
- Interceptors::Result
- Defined in:
- lib/interceptors/result.rb
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
Instance Method Summary collapse
- #err? ⇒ Boolean
-
#initialize(value: nil, error: nil, meta: {}) ⇒ Result
constructor
A new instance of Result.
- #merge_meta(extra) ⇒ Object
- #ok? ⇒ Boolean
- #to_h ⇒ Object
Constructor Details
#initialize(value: nil, error: nil, meta: {}) ⇒ Result
Returns a new instance of Result.
7 8 9 10 11 |
# File 'lib/interceptors/result.rb', line 7 def initialize(value: nil, error: nil, meta: {}) @value = value @error = error @meta = || {} end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
5 6 7 |
# File 'lib/interceptors/result.rb', line 5 def error @error end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
5 6 7 |
# File 'lib/interceptors/result.rb', line 5 def @meta end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
5 6 7 |
# File 'lib/interceptors/result.rb', line 5 def value @value end |
Class Method Details
.err(error, meta: {}) ⇒ Object
25 26 27 28 29 |
# File 'lib/interceptors/result.rb', line 25 def self.err(error, meta: {}) raise ArgumentError, "error must be provided" if error.nil? new(error: error, meta: ) end |
.ok(value = nil, meta: {}) ⇒ Object
21 22 23 |
# File 'lib/interceptors/result.rb', line 21 def self.ok(value = nil, meta: {}) new(value: value, meta: ) end |
Instance Method Details
#err? ⇒ Boolean
17 18 19 |
# File 'lib/interceptors/result.rb', line 17 def err? !ok? end |
#merge_meta(extra) ⇒ Object
31 32 33 |
# File 'lib/interceptors/result.rb', line 31 def (extra) self.class.new(value: value, error: error, meta: .merge(extra)) end |
#ok? ⇒ Boolean
13 14 15 |
# File 'lib/interceptors/result.rb', line 13 def ok? error.nil? end |
#to_h ⇒ Object
35 36 37 |
# File 'lib/interceptors/result.rb', line 35 def to_h { value: value, error: error, meta: } end |