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
# 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']
# @return [Boolean] whether the daemon reported success
attr_reader :ok
# @return [Object, nil] decoded payload (String, Integer, Hash, Array, etc.)
attr_reader :data
# @return [Integer] age of the cached value in milliseconds
attr_reader :age_ms
# @return [String, nil] error message when ok is false
attr_reader :error
@ok = ok
@data = data
@age_ms = age_ms
@stale = stale
@error = error
end
# @return [Boolean]
@ok
end
# @return [Boolean] true when the response carried data (cache hit)
@ok && !@data.nil?
end
# @return [Boolean] true when the response was successful but had no data
@ok && @data.nil?
end
# @return [Boolean] true when the cached value is stale
@stale
end
# Delegates field access to the data hash.
#
# @param key [String] field name
# @return [Object, nil]
# @raise [TypeError] if data is not a Hash
unless @data.is_a?(Hash)
raise TypeError,
end
@data[key]
end
parts = []
parts << unless @data.nil?
parts << if @age_ms > 0
parts << if @stale
parts << if @error
end
end
end