Permalink
Browse files

+ unshift, << to index, category as realtime methods.

  • Loading branch information...
1 parent 8806a42 commit 2f895f8823f7e349ac331fc2cdd075a1807dc984 @floere committed Nov 29, 2011
@@ -195,7 +195,7 @@ def pid
# Use the host and pid (generated lazily in child processes) for the result.
#
def generate_intermediate_result_id
- :"#{host}:#{pid}:picky:result"
+ @intermediate_result_id ||= "#{host}:#{pid}:picky:result"
end
end
@@ -31,8 +31,7 @@ def remove id
# Returns a reference to the array where the id has been added.
#
def add id, str_or_sym, where = :unshift
- str_or_syms = @realtime[id]
- str_or_syms = (@realtime[id] = []) unless str_or_syms # TODO Nicefy.
+ str_or_syms = @realtime[id] || (@realtime[id] = [])
# Inverted.
#
@@ -69,12 +68,11 @@ def add id, str_or_sym, where = :unshift
def add_similarity str_or_sym, where = :unshift
if encoded = self.similarity_strategy.encoded(str_or_sym)
similarity = @similarity[encoded] || (@similarity[encoded] = []) # Ensures that we get an extended Array
- if similarity.include? str_or_sym
- similarity.delete str_or_sym # Not completely correct, as others will also be affected, but meh.
- similarity.send where, str_or_sym #
- else
- similarity.send where, str_or_sym
- end
+
+ # Not completely correct, as others will also be affected, but meh.
+ #
+ similarity.delete str_or_sym if similarity.include? str_or_sym
+ similarity.send where, str_or_sym
end
end
@@ -27,6 +27,18 @@ def replace object, where = :unshift
add object, where
end
+ # Add at the end.
+ #
+ def << thing
+ add thing, __method__
+ end
+
+ # Add at the beginning.
+ #
+ def unshift thing
+ add thing, __method__
+ end
+
# For the given id, adds the list of
# strings to the index for the given id.
#
@@ -4,13 +4,25 @@ module Picky
#
class Index
- delegate :remove, # aka delete.
- :add, # aka insert.
- :replace, # aka insert or update.
+ delegate :remove, # aka "delete".
+ :add, # aka "insert".
+ :replace, # aka "insert or update".
:clear_realtime,
:build_realtime_mapping,
:to => :categories
+ # Add at the end.
+ #
+ def << thing
+ add thing, __method__
+ end
+
+ # Add at the beginning (calls add).
+ #
+ def unshift thing
+ add thing, __method__
+ end
+
end
end
@@ -0,0 +1,33 @@
+# encoding: utf-8
+#
+require 'spec_helper'
+
+describe Picky::Category, "Realtime API" do
+
+ Thing = Struct.new :id, :text
+
+ let(:category) do
+ index = Picky::Index.new :some_index_name
+ category = described_class.new :text, index
+ end
+
+ it 'offers an add method' do
+ category.add Thing.new(1, 'text')
+ end
+ it 'offers a remove method' do
+ category.remove 1
+ end
+ it 'offers a replace method' do
+ category.replace Thing.new(1, 'text')
+ end
+ it 'offers a << method' do
+ category << Thing.new(1, 'text')
+ end
+ # it 'offers a >> method' do
+ # Thing.new(1, 'text') >> category # I mean, as long as we're dreaming.
+ # end
+ it 'offers an unshift method' do
+ category.unshift Thing.new(1, 'text')
+ end
+
+end
@@ -1,38 +1,38 @@
require 'spec_helper'
describe Picky::Backends::Redis::Float do
-
+
let(:client) { stub :client }
- let(:backend) { described_class.new client, :some_namespace }
-
+ let(:backend) { described_class.new client, 'some_namespace' }
+
describe 'dump' do
it 'dumps correctly' do
- client.should_receive(:del).once.ordered.with :some_namespace
- client.should_receive(:hset).once.ordered.with :some_namespace, :a, 1
- client.should_receive(:hset).once.ordered.with :some_namespace, :b, 2
- client.should_receive(:hset).once.ordered.with :some_namespace, :c, 3
-
- backend.dump a: 1, b: 2, c: 3
+ client.should_receive(:del).once.ordered.with 'some_namespace'
+ client.should_receive(:hset).once.ordered.with 'some_namespace', 'a', 1
+ client.should_receive(:hset).once.ordered.with 'some_namespace', 'b', 2
+ client.should_receive(:hset).once.ordered.with 'some_namespace', 'c', 3
+
+ backend.dump 'a' => 1, 'b' => 2, 'c' => 3
end
end
-
+
describe 'member' do
it 'delegates to the backend' do
- client.should_receive(:hget).once.with :some_namespace, :some_symbol
-
- backend[:some_symbol]
+ client.should_receive(:hget).once.with 'some_namespace', 'some'
+
+ backend['some']
end
it 'returns whatever it gets from the backend' do
client.should_receive(:hget).any_number_of_times.and_return '1.23'
-
- backend[:anything].should == 1.23
+
+ backend['anything'].should == 1.23
end
end
-
+
describe 'to_s' do
it 'returns the cache path with the default file extension' do
backend.to_s.should == 'Picky::Backends::Redis::Float(some_namespace:*)'
end
end
-
+
end
@@ -0,0 +1,34 @@
+# encoding: utf-8
+#
+require 'spec_helper'
+
+describe Picky::Index, "Realtime API" do
+
+ Thing = Struct.new :id, :text
+
+ let(:index) do
+ described_class.new :some_index_name do
+ category :text
+ end
+ end
+
+ it 'offers an add method' do
+ index.add Thing.new(1, 'text')
+ end
+ it 'offers a remove method' do
+ index.remove 1
+ end
+ it 'offers a replace method' do
+ index.replace Thing.new(1, 'text')
+ end
+ it 'offers a << method' do
+ index << Thing.new(1, 'text')
+ end
+ # it 'offers a >> method' do
+ # Thing.new(1, 'text') >> index # I mean, as long as we're dreaming.
+ # end
+ it 'offers an unshift method' do
+ index.unshift Thing.new(1, 'text')
+ end
+
+end
Oops, something went wrong. Retry.

0 comments on commit 2f895f8

Please sign in to comment.