Skip to content

Commit

Permalink
Refactoring Transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Mar 27, 2014
1 parent f59df45 commit 385282d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 63 deletions.
28 changes: 2 additions & 26 deletions lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Rest
include NodeLabels
include SchemaIndexes
include Constraints
include Transactions
extend Forwardable

attr_reader :connection
Expand Down Expand Up @@ -71,35 +72,10 @@ def initialize(options = ENV['NEO4J_URL'] || {})
@extensions ||= Extensions.new(@connection)
@batch ||= Batch.new(@connection)
@clean ||= Clean.new(@connection)
@transactions ||= Transactions.new(@connection)
@spatial ||= Spatial.new(@connection)
end

# transactions

def begin_transaction(statements=nil)
@transactions.begin(statements)
end

def in_transaction(tx, statements)
@transactions.add(tx, statements)
end

def keep_transaction(tx)
@transactions.add(tx)
end

def commit_transaction(tx, statements=[])
if (tx.is_a?(Hash) || tx.is_a?(Integer))
@transactions.commit(tx, statements)
else
@transactions.begin(tx, "/commit")
end
end

def rollback_transaction(tx)
@transactions.rollback(tx)
end


# nodes

Expand Down
51 changes: 25 additions & 26 deletions lib/neography/rest/transactions.rb
Original file line number Diff line number Diff line change
@@ -1,54 +1,53 @@
module Neography
class Rest
class Transactions
extend Neography::Rest::Paths
module Transactions
include Neography::Rest::Helpers

add_path :base, "/transaction"
add_path :tx, "/transaction/:id"
add_path :commit, "/transaction/:id/commit"

def initialize(connection)
@connection ||= connection
end

def begin(statements = [], commit = "")

def begin_transaction(statements = [], commit = "")
options = {
:body => (
convert_cypher(statements)
).to_json,
:headers => json_content_type
}
@connection.post(base_path + commit, options)
@connection.post("/transaction" + commit, options)
end

def add(tx, statements = [])
def in_transaction(tx, statements = [])
options = {
:body => (
convert_cypher(statements)
).to_json,
:headers => json_content_type
}
@connection.post(tx_path(:id => get_id(tx)), options)
@connection.post("/transaction/%{id}" % {:id => get_tx_id(tx)}, options)
end

def keep_transaction(tx)
in_transaction(tx)
end

def commit(tx, statements = [])
options = {
:body => (
convert_cypher(statements)
).to_json,
:headers => json_content_type
}
@connection.post(commit_path(:id => get_id(tx)), options)
def commit_transaction(tx, statements = [])
if (tx.is_a?(Hash) || tx.is_a?(Integer))
options = {
:body => (
convert_cypher(statements)
).to_json,
:headers => json_content_type
}
@connection.post("/transaction/%{id}/commit" % {:id => get_tx_id(tx)}, options)
else
begin_transaction(tx, "/commit")
end
end

def rollback(tx)
@connection.delete(tx_path(:id => get_id(tx)))
def rollback_transaction(tx)
@connection.delete("/transaction/%{id}" % {:id => get_tx_id(tx)})
end

private

def get_id(tx)
def get_tx_id(tx)
return tx if tx.is_a?(Integer)
return tx.split("/")[-2] if tx.is_a?(String)
return tx["commit"].split("/")[-2] if tx["commit"]
Expand Down
21 changes: 10 additions & 11 deletions spec/unit/rest/transactions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,38 @@ module Neography
class Rest
describe Transactions do

let(:connection) { double(:configuration => "http://configuration") }
subject { Transactions.new(connection) }

subject { Neography::Rest.new }

it "can create new transactions" do
options = {
:body => '{"statements":[]}',
:headers => json_content_type
}
connection.should_receive(:post).with("/transaction", options)
subject.begin
subject.connection.should_receive(:post).with("/transaction", options)
subject.begin_transaction
end

it "can add to transactions" do
options = {
:body => '{"statements":[]}',
:headers => json_content_type
}
connection.should_receive(:post).with("/transaction/1", options)
subject.add(1, [])
subject.connection.should_receive(:post).with("/transaction/1", options)
subject.in_transaction(1, [])
end

it "can commit transactions" do
options = {
:body => '{"statements":[]}',
:headers => json_content_type
}
connection.should_receive(:post).with("/transaction/1/commit", options)
subject.commit(1, [])
subject.connection.should_receive(:post).with("/transaction/1/commit", options)
subject.commit_transaction(1, [])
end

it "can rollback transactions" do
connection.should_receive(:delete).with("/transaction/1")
subject.rollback(1)
subject.connection.should_receive(:delete).with("/transaction/1")
subject.rollback_transaction(1)
end

end
Expand Down

0 comments on commit 385282d

Please sign in to comment.