Skip to content

Commit

Permalink
Store document IDs for retrieval later
Browse files Browse the repository at this point in the history
  • Loading branch information
James Conroy-Finn committed Feb 25, 2011
1 parent 69c512f commit 1eecedf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
24 changes: 20 additions & 4 deletions lib/visitors/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'redis-namespace'

class Visitors::Store
ID_STORE_NAMESPACE = 'document_ids'
TTL = 24 * 60 * 60 # 24 hours

attr_reader :namespace, :redis_config
Expand All @@ -19,16 +20,31 @@ def store
@store ||= Redis::Namespace.new(@namespace, :redis => redis)
end

def each
documents.each do |document_id|
yield document_id, store.hgetall(document_id)
end
end

def documents
store.smembers(ID_STORE_NAMESPACE)
end

def find(document_id)
store.hgetall document_id
store.hgetall(document_id)
end

def increment(document_id, field)
Visitors.assert_valid_field!(field.to_sym)
count = store.hincrby document_id, field.to_sym, 1
store.expire document_id, TTL
count
push(document_id, field)
end

alias :incr :increment

private

def push(document_id, field)
store.sadd(ID_STORE_NAMESPACE, document_id)
store.hincrby(document_id, field, 1)
end
end
14 changes: 10 additions & 4 deletions spec/visitors/store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,16 @@ def store(options = {})

describe '#increment' do
context 'with a valid field' do
it 'increments the field' do
Visitors.stub!(:fields => %w[field])
store.store.should_receive(:hincrby).with('abc', 'field', 1).and_return(true)
store.increment('abc', 'field')
before(:each) do
Visitors.stub!(:assert_valid_field! => true)
end

context 'with a business id that is not in the log' do
it 'stores the id of the business and increments the field' do
store.store.should_receive(:sadd).with('document_ids', '123').and_return(true)
store.store.should_receive(:hincrby).with('123', 'show', 1).and_return(true)
store.increment('123', 'show')
end
end
end
end
Expand Down

0 comments on commit 1eecedf

Please sign in to comment.