Skip to content

Commit

Permalink
prettify code
Browse files Browse the repository at this point in the history
  • Loading branch information
dpisarewski committed Jan 6, 2015
1 parent a24194f commit 9a69115
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 200 deletions.
38 changes: 17 additions & 21 deletions lib/neo4j-server/cypher_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,26 @@ def map_row_value(value, session)
def hash_value_as_object(value, session)
return value unless value['labels'] || value['type'] || transaction_response?

is_node, data = if transaction_response?
add_transaction_entity_id
[!mapped_rest_data['start'], mapped_rest_data]
elsif value['labels'] || value['type']
add_entity_id(value)
[value['labels'], value]
end
is_node, data = if transaction_response?
add_transaction_entity_id
[!mapped_rest_data['start'], mapped_rest_data]
elsif value['labels'] || value['type']
add_entity_id(value)
[value['labels'], value]
end
(is_node ? CypherNode : CypherRelationship).new(session, data).wrapper
end

attr_reader :struct

def initialize(response, uncommited = false)
@response = response
@response = response
@uncommited = uncommited
set_data_from_request if response
end

def entity_data(id=nil)
if uncommited?
def entity_data(id = nil)
if @uncommited
data = @data.first['row'].first
data.is_a?(Hash) ? {'data' => data, 'id' => id} : data
else
Expand All @@ -104,7 +104,7 @@ def entity_data(id=nil)
end

def first_data(id = nil)
if uncommited?
if @uncommited
data = @data.first['row'].first
# data.is_a?(Hash) ? {'data' => data, 'id' => id} : data
else
Expand Down Expand Up @@ -156,10 +156,6 @@ def error?
errors.any?
end

def uncommited?
@uncommited
end

def data?
!response.body['data'].nil?
end
Expand All @@ -182,7 +178,7 @@ def set_data(data, columns)
@struct = columns.empty? ? Object.new : Struct.new(*columns.map(&:to_sym))
self
end

def set_data_from_request
return if error?
if transaction_response? && response.body['results']
Expand All @@ -191,17 +187,17 @@ def set_data_from_request
set_data(response.body['data'], response.body['columns'])
end
end

def raise_error
fail 'Tried to raise error without an error' unless error?
fail error
end

def raise_cypher_error
fail 'Tried to raise error without an error' unless error?
fail Neo4j::Session::CypherError.new(error.message, error.code, error.status)
end

def self.create_with_no_tx(response)
CypherResponse.new(response)
end
Expand All @@ -213,11 +209,11 @@ def self.create_with_tx(response)
def transaction_response?
response.respond_to?('body') && !response.body['commit'].nil?
end

def transaction_failed?
errors.any? { |e| e.code =~ /Neo\.DatabaseError/ }
end

def transaction_not_found?
errors.any? { |e| e.code == 'Neo.ClientError.Transaction.UnknownId' }
end
Expand Down
23 changes: 12 additions & 11 deletions lib/neo4j-server/cypher_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def initialize(url, session_connection)
end

ROW_REST = %w(row REST)

def _query(cypher_query, params = nil)
statement = {statement: cypher_query, parameters: params, resultDataContents: ROW_REST}
body = {statements: [statement]}
Expand All @@ -38,10 +39,10 @@ def _create_cypher_response(response)
# Replaces current transaction with invalid transaction indicating it was rolled back or expired on the server side. http://neo4j.com/docs/stable/status-codes.html#_classifications
def handle_transaction_errors(response)
tx_class = if response.transaction_not_found?
ExpiredCypherTransaction
elsif response.transaction_failed?
InvalidCypherTransaction
end
ExpiredCypherTransaction
elsif response.transaction_failed?
InvalidCypherTransaction
end

register_invalid_transaction(tx_class) if tx_class
end
Expand All @@ -58,7 +59,7 @@ def _delete_tx

def _commit_tx
_tx_query(:post, commit_url, nil)
end
end

private

Expand Down Expand Up @@ -86,11 +87,11 @@ def empty_response
def valid?
!invalid?
end

def expired?
is_a? ExpiredCypherTransaction
end

def invalid?
is_a? InvalidCypherTransaction
end
Expand All @@ -108,15 +109,15 @@ def close
Neo4j::Transaction.unregister(self)
end

def _query(cypher_query, params=nil)
def _query(cypher_query, params = nil)
fail 'Transaction invalid, unable to perform query'
end
end

class ExpiredCypherTransaction < InvalidCypherTransaction
def _query(cypher_query, params=nil)
def _query(cypher_query, params = nil)
fail 'Transaction expired, unable to perform query'
end
end
end
end
end
end
168 changes: 0 additions & 168 deletions spec/neo4j-server/unit/cypher_transaction_spec.rb

This file was deleted.

0 comments on commit 9a69115

Please sign in to comment.