Class: Beachcomber::Result
- Inherits:
-
Object
- Object
- Beachcomber::Result
- Defined in:
- lib/beachcomber/result.rb
Overview
Wraps a daemon response.
A result can represent a cache hit (data present), a miss (ok but no data), or an error (ok: false — though errors are normally raised as ServerError).
Examples:
result = client.get('git.branch', path: '/repo')
if result.hit?
puts result.data # "main"
puts result.age_ms # 42
end
# hash data access
result = client.get('git', path: '/repo')
puts result['branch']
Instance Attribute Summary collapse
-
#age_ms ⇒ Integer
readonly
Age of the cached value in milliseconds.
-
#data ⇒ Object?
readonly
Decoded payload (String, Integer, Hash, Array, etc.).
-
#error ⇒ String?
readonly
Error message when ok is false.
-
#ok ⇒ Boolean
readonly
Whether the daemon reported success.
Instance Method Summary collapse
-
#[](key) ⇒ Object?
Delegates field access to the data hash.
-
#hit? ⇒ Boolean
True when the response carried data (cache hit).
-
#initialize(ok:, data:, age_ms:, stale:, error:) ⇒ Result
constructor
A new instance of Result.
- #inspect ⇒ Object
-
#miss? ⇒ Boolean
True when the response was successful but had no data.
- #ok? ⇒ Boolean
-
#stale? ⇒ Boolean
True when the cached value is stale.
Constructor Details
#initialize(ok:, data:, age_ms:, stale:, error:) ⇒ Result
Returns a new instance of Result.
31 32 33 34 35 36 37 |
# File 'lib/beachcomber/result.rb', line 31 def initialize(ok:, data:, age_ms:, stale:, error:) @ok = ok @data = data @age_ms = age_ms @stale = stale @error = error end |
Instance Attribute Details
#age_ms ⇒ Integer (readonly)
Returns age of the cached value in milliseconds.
26 27 28 |
# File 'lib/beachcomber/result.rb', line 26 def age_ms @age_ms end |
#data ⇒ Object? (readonly)
Returns decoded payload (String, Integer, Hash, Array, etc.).
23 24 25 |
# File 'lib/beachcomber/result.rb', line 23 def data @data end |
#error ⇒ String? (readonly)
Returns error message when ok is false.
29 30 31 |
# File 'lib/beachcomber/result.rb', line 29 def error @error end |
#ok ⇒ Boolean (readonly)
Returns whether the daemon reported success.
20 21 22 |
# File 'lib/beachcomber/result.rb', line 20 def ok @ok end |
Instance Method Details
#[](key) ⇒ Object?
Delegates field access to the data hash.
64 65 66 67 68 69 70 |
# File 'lib/beachcomber/result.rb', line 64 def [](key) unless @data.is_a?(Hash) raise TypeError, "Result#[] requires hash data, got #{@data.class}" end @data[key] end |
#hit? ⇒ Boolean
Returns true when the response carried data (cache hit).
45 46 47 |
# File 'lib/beachcomber/result.rb', line 45 def hit? @ok && !@data.nil? end |
#inspect ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/beachcomber/result.rb', line 72 def inspect parts = ["ok=#{@ok}"] parts << "data=#{@data.inspect}" unless @data.nil? parts << "age_ms=#{@age_ms}" if @age_ms > 0 parts << "stale=true" if @stale parts << "error=#{@error.inspect}" if @error "#<Beachcomber::Result #{parts.join(', ')}>" end |
#miss? ⇒ Boolean
Returns true when the response was successful but had no data.
50 51 52 |
# File 'lib/beachcomber/result.rb', line 50 def miss? @ok && @data.nil? end |
#ok? ⇒ Boolean
40 41 42 |
# File 'lib/beachcomber/result.rb', line 40 def ok? @ok end |
#stale? ⇒ Boolean
Returns true when the cached value is stale.
55 56 57 |
# File 'lib/beachcomber/result.rb', line 55 def stale? @stale end |