Class: FreshObjects::Lookup
- Inherits:
-
Object
- Object
- FreshObjects::Lookup
- Defined in:
- lib/fresh_objects/lookup.rb
Overview
A data structure which holds a timestamp per ID. It can then be used as a performant lookup to see if an incoming timestamp is stale or new. It is essentially backed by a hash where the key is a string and the value is a Time object. You can also pass in a hash into the constructor for configuration ease.
Instance Attribute Summary collapse
-
#timestamps_by_id ⇒ Object
readonly
Returns the value of attribute timestamps_by_id.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #fresh?(id, timestamp) ⇒ Boolean
- #fresh_set?(id, timestamp) ⇒ Boolean
- #get(id) ⇒ Object
-
#initialize(timestamps_by_id = {}) ⇒ Lookup
constructor
A new instance of Lookup.
- #set(id, timestamp) ⇒ Object
- #stale?(id, timestamp) ⇒ Boolean
Constructor Details
#initialize(timestamps_by_id = {}) ⇒ Lookup
Returns a new instance of Lookup.
28 29 30 31 32 33 34 |
# File 'lib/fresh_objects/lookup.rb', line 28 def initialize( = {}) = .map do |id, | [id.to_s, parse_time()] end.to_h freeze end |
Instance Attribute Details
#timestamps_by_id ⇒ Object (readonly)
Returns the value of attribute timestamps_by_id.
26 27 28 |
# File 'lib/fresh_objects/lookup.rb', line 26 def end |
Class Method Details
.make(timestamps_by_id = {}) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/fresh_objects/lookup.rb', line 17 def make( = {}) if .is_a?(self) else new() end end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
62 63 64 |
# File 'lib/fresh_objects/lookup.rb', line 62 def ==(other) other.is_a?(self.class) && == other. end |
#fresh?(id, timestamp) ⇒ Boolean
42 43 44 |
# File 'lib/fresh_objects/lookup.rb', line 42 def fresh?(id, ) !stale?(id, ) end |
#fresh_set?(id, timestamp) ⇒ Boolean
36 37 38 39 40 |
# File 'lib/fresh_objects/lookup.rb', line 36 def fresh_set?(id, ) fresh?(id, ).tap do |fresh| set(id, ) if fresh end end |
#get(id) ⇒ Object
54 55 56 |
# File 'lib/fresh_objects/lookup.rb', line 54 def get(id) [id.to_s] end |
#set(id, timestamp) ⇒ Object
58 59 60 |
# File 'lib/fresh_objects/lookup.rb', line 58 def set(id, ) tap { [id.to_s] = parse_time() } end |
#stale?(id, timestamp) ⇒ Boolean
46 47 48 49 50 51 52 |
# File 'lib/fresh_objects/lookup.rb', line 46 def stale?(id, ) id = id.to_s current = get(id) = parse_time() current && <= current end |