Skip to content
This repository has been archived by the owner on Sep 18, 2021. It is now read-only.

Commit

Permalink
move execute_at and position to an options hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Maxwell committed Dec 10, 2010
1 parent a8b3d69 commit 472d086
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
12 changes: 9 additions & 3 deletions lib/flock/client.rb
Expand Up @@ -70,10 +70,13 @@ def size(*query)

# edge manipulation

def update(method, source_id, graph, destination_id, priority = Flock::Priority::High, execute_at = nil)
def update(method, source_id, graph, destination_id, priority = Flock::Priority::High, options = {})
execute_at = options.delete(:execute_at)
position = options.delete(:position)

_cache_clear
ops = current_transaction || Flock::ExecuteOperations.new(@service, priority, execute_at)
ops.send(method, *_query_args([source_id, graph, destination_id])[0, 3])
ops.send(method, *(_query_args([source_id, graph, destination_id])[0, 3] << position))
ops.apply unless in_transaction?
end

Expand All @@ -84,7 +87,10 @@ def update(method, source_id, graph, destination_id, priority = Flock::Priority:

alias unarchive add

def transaction(priority = Flock::Priority::High, execute_at = nil, &block)
def transaction(priority = Flock::Priority::High, options = {}, &block)
execute_at = options.delete(:execute_at)
position = options.delete(:position)

new_transaction = !in_transaction?

ops =
Expand Down
5 changes: 3 additions & 2 deletions lib/flock/operations/execute_operation.rb
@@ -1,12 +1,13 @@
module Flock
class ExecuteOperation
def initialize(operation_type, query)
@operation_type, @query = operation_type, query
def initialize(operation_type, query, position = nil)
@operation_type, @query, @position = operation_type, query, position
end

def to_thrift
op = Edges::ExecuteOperation.new
op.operation_type = @operation_type
op.position = @position if @position
op.term = QueryTerm.new(@query).to_thrift
op
end
Expand Down
4 changes: 2 additions & 2 deletions lib/flock/operations/execute_operations.rb
@@ -1,12 +1,12 @@
module Flock
class ExecuteOperations
def initialize(service, priority, execute_at = nil)
@service, @operations, @priority, @execute_at = service, [], priority, execute_at
@service, @operations, @priority, @execute_at = service, [], priority, execute_at
end

Flock::Edges::ExecuteOperationType::VALUE_MAP.each do |op_id, op|
op = op.downcase
class_eval "def #{op}(s, g, d); @operations << ExecuteOperation.new(#{op_id}, [s, g, d]); self end", __FILE__, __LINE__
class_eval "def #{op}(s, g, d, p = nil); @operations << ExecuteOperation.new(#{op_id}, [s, g, d], p); self end", __FILE__, __LINE__
end

def apply
Expand Down
18 changes: 15 additions & 3 deletions spec/execute_operations_spec.rb
Expand Up @@ -3,20 +3,32 @@
describe Flock::ExecuteOperations do
describe "#negate" do
it "should create a negate opperation" do
mock(Flock::ExecuteOperation).new(Flock::Edges::ExecuteOperationType::Negate, [1,1,3])
mock(Flock::ExecuteOperation).new(Flock::Edges::ExecuteOperationType::Negate, [1,1,3], nil)
operation = Flock::ExecuteOperations.new(Flock::Client.new(Flock::MockService), nil)
operation.negate(1,1,3)
end
end

describe "adding with timestamp" do
it "should pass through" do
service = Flock::MockService
client = Flock::Client.new(service)
now = Time.now.to_i
thing = Flock::ExecuteOperations.new(service, Flock::Priority::High, now)
mock(Flock::ExecuteOperations).new(service, Flock::Priority::High, now) { thing }
client.add(1, 1, 3, Flock::Priority::High, now)
client.add(1, 1, 3, Flock::Priority::High, :execute_at => now)
end
end

describe "adding with position" do
it "should pass through" do
service = Flock::MockService
client = Flock::Client.new(service)
now = Time.now.to_i
thing = Flock::ExecuteOperation.new(1, [1, 1, 3], 12345)
mock(Flock::ExecuteOperation).new(1, [1, 1, 3], 12345) { thing}
client.add(1, 1, 3, Flock::Priority::High, :position => 12345)
end
end

end
2 changes: 1 addition & 1 deletion spec/simple_operation_spec.rb
Expand Up @@ -3,7 +3,7 @@
describe Flock::ExecuteOperations do
describe "#negate" do
it "should create a negate operation" do
mock(Flock::ExecuteOperation).new(Flock::Edges::ExecuteOperationType::Negate, [1,1,3])
mock(Flock::ExecuteOperation).new(Flock::Edges::ExecuteOperationType::Negate, [1,1,3], nil)
operation = Flock::ExecuteOperations.new(Flock::Client.new(Flock::MockService), nil)
operation.negate(1,1,3)
end
Expand Down

0 comments on commit 472d086

Please sign in to comment.