Class: Phoenix::Inbox
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#ttl ⇒ Object
readonly
Returns the value of attribute ttl.
Instance Method Summary collapse
- #current_timestamp ⇒ Object
-
#initialize(ttl:) ⇒ Inbox
constructor
A new instance of Inbox.
- #key?(key) ⇒ Boolean
- #pop(key) ⇒ Object (also: #delete)
- #push(key, val) ⇒ Object (also: #[]=)
Constructor Details
#initialize(ttl:) ⇒ Inbox
8 9 10 11 12 13 |
# File 'lib/yatapp/inbox.rb', line 8 def initialize(ttl:) @ttl = ttl @bucket = Time.now.to_i / ttl @data = Hash.new { |h, k| h[k] = {} } super() end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/yatapp/inbox.rb', line 7 def data @data end |
#ttl ⇒ Object (readonly)
Returns the value of attribute ttl.
7 8 9 |
# File 'lib/yatapp/inbox.rb', line 7 def ttl @ttl end |
Instance Method Details
#current_timestamp ⇒ Object
41 42 43 |
# File 'lib/yatapp/inbox.rb', line 41 def Time.now.to_i / ttl end |
#key?(key) ⇒ Boolean
37 38 39 |
# File 'lib/yatapp/inbox.rb', line 37 def key?(key) data.values.any? { |v| v.key?(key) } end |
#pop(key) ⇒ Object Also known as: delete
28 29 30 31 32 33 |
# File 'lib/yatapp/inbox.rb', line 28 def pop(key) synchronize do ts = data[ts - 1].delete(key) { data[ts].delete(key) { yield }} end end |
#push(key, val) ⇒ Object Also known as: []=
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/yatapp/inbox.rb', line 15 def push(key, val) synchronize do ts = (data[ts][key] = val).tap do if data.keys.size >= 3 data.delete_if { |key, _| key < (ts - 1) } end end end end |