Class: Omnitrack::Result
- Inherits:
-
Object
- Object
- Omnitrack::Result
- Defined in:
- lib/omnitrack/result.rb
Overview
Immutable value object returned by every adapter call. Provides a consistent interface regardless of platform.
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
- .failure(error: nil, adapter: nil, metadata: {}) ⇒ Object
- .skipped(reason: nil, adapter: nil) ⇒ Object
-
.success(adapter: nil, data: nil, metadata: {}) ⇒ Object
——————————————————————- Factory methods ——————————————————————-.
Instance Method Summary collapse
- #failure? ⇒ Boolean
-
#initialize(status:, adapter: nil, data: nil, error: nil, metadata: {}) ⇒ Result
constructor
A new instance of Result.
- #inspect ⇒ Object
- #skipped? ⇒ Boolean
-
#success? ⇒ Boolean
——————————————————————- Predicate helpers ——————————————————————-.
-
#to_h ⇒ Object
——————————————————————- Serialisation ——————————————————————-.
- #to_json(*args) ⇒ Object
Constructor Details
#initialize(status:, adapter: nil, data: nil, error: nil, metadata: {}) ⇒ Result
Returns a new instance of Result.
9 10 11 12 13 14 15 16 |
# File 'lib/omnitrack/result.rb', line 9 def initialize(status:, adapter: nil, data: nil, error: nil, metadata: {}) @status = status.to_sym # :success | :failure | :skipped @adapter = adapter @data = data @error = error @metadata = || {} freeze end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
7 8 9 |
# File 'lib/omnitrack/result.rb', line 7 def adapter @adapter end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/omnitrack/result.rb', line 7 def data @data end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
7 8 9 |
# File 'lib/omnitrack/result.rb', line 7 def error @error end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
7 8 9 |
# File 'lib/omnitrack/result.rb', line 7 def @metadata end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
7 8 9 |
# File 'lib/omnitrack/result.rb', line 7 def status @status end |
Class Method Details
.failure(error: nil, adapter: nil, metadata: {}) ⇒ Object
26 27 28 |
# File 'lib/omnitrack/result.rb', line 26 def self.failure(error: nil, adapter: nil, metadata: {}) new(status: :failure, adapter: adapter, error: error, metadata: ) end |
.skipped(reason: nil, adapter: nil) ⇒ Object
30 31 32 33 |
# File 'lib/omnitrack/result.rb', line 30 def self.skipped(reason: nil, adapter: nil) new(status: :skipped, adapter: adapter, metadata: { reason: reason }) end |
.success(adapter: nil, data: nil, metadata: {}) ⇒ Object
Factory methods
22 23 24 |
# File 'lib/omnitrack/result.rb', line 22 def self.success(adapter: nil, data: nil, metadata: {}) new(status: :success, adapter: adapter, data: data, metadata: ) end |
Instance Method Details
#failure? ⇒ Boolean
43 44 45 |
# File 'lib/omnitrack/result.rb', line 43 def failure? @status == :failure end |
#inspect ⇒ Object
70 71 72 73 |
# File 'lib/omnitrack/result.rb', line 70 def inspect "#<Omnitrack::Result status=#{@status} adapter=#{@adapter} " \ "error=#{@error&..inspect}>" end |
#skipped? ⇒ Boolean
47 48 49 |
# File 'lib/omnitrack/result.rb', line 47 def skipped? @status == :skipped end |
#success? ⇒ Boolean
Predicate helpers
39 40 41 |
# File 'lib/omnitrack/result.rb', line 39 def success? @status == :success end |
#to_h ⇒ Object
Serialisation
55 56 57 58 59 60 61 62 63 |
# File 'lib/omnitrack/result.rb', line 55 def to_h { status: @status, adapter: @adapter, data: @data, error: @error&., metadata: @metadata } end |
#to_json(*args) ⇒ Object
65 66 67 68 |
# File 'lib/omnitrack/result.rb', line 65 def to_json(*args) require "json" JSON.generate(to_h, *args) end |