Class: WaveBox::Box

Inherits:
Object
  • Object
show all
Defined in:
lib/wave_box/box.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Box

Returns a new instance of Box.



5
6
7
8
9
10
11
12
# File 'lib/wave_box/box.rb', line 5

def initialize(options)
  @options = options
  @expire = options[:expire]
  @max_size = options[:max_size]
  @redis = options[:redis]
  @key = options[:key]
  @encode = options[:encode]
end

Instance Attribute Details

#expireObject (readonly)

Returns the value of attribute expire.



3
4
5
# File 'lib/wave_box/box.rb', line 3

def expire
  @expire
end

#max_sizeObject (readonly)

Returns the value of attribute max_size.



3
4
5
# File 'lib/wave_box/box.rb', line 3

def max_size
  @max_size
end

Instance Method Details

#after(time) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/wave_box/box.rb', line 29

def after(time)
  items = @redis.zrangebyscore( @key, "#{time.to_i}", "+inf")

  if @encode == false
    items
  else
    items.map { |x| decode(x) }
  end
end

#clear!Object



39
40
41
# File 'lib/wave_box/box.rb', line 39

def clear!
  @redis.del @key
end

#push(value, time = Time.now) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/wave_box/box.rb', line 14

def push(value, time = Time.now)
  if @encode == false
    v = value
  else
    v = encode(value)
  end
  @redis.zadd(@key, time.to_i, v)

  truncate!
end

#sizeObject



25
26
27
# File 'lib/wave_box/box.rb', line 25

def size
  @redis.zcard @key
end