Skip to content

Commit

Permalink
All Rails specs now works
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasronge committed Apr 5, 2011
1 parent 85c34a9 commit 661fc79
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 184 deletions.
3 changes: 2 additions & 1 deletion lib/neo4j/rails/rel_persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,10 @@ def _load(id)
end

def init_on_create(*)
self["_classname"] = self.class.to_s
#self["_classname"] = self.class.to_s
write_default_attributes
write_changed_attributes
@_java_rel[:_classname] = self.class.to_s
end

def reset_attributes
Expand Down
16 changes: 5 additions & 11 deletions lib/neo4j/rails/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,23 @@ class Relationship
# Initialize a Node with a set of properties (or empty if nothing is passed)
def initialize(*args)
@type, @start_node, @end_node, attributes = args
puts "CREATE REL #{@type}, start: #@start_node end: #@end_node"
reset_attributes
self.attributes = attributes if attributes.is_a?(Hash)
end

def other_node(node) # TODO - compare neo_id instead ?
if @start_node == node
@end_node
if persisted?
_java_rel.getOtherNode(node._java_node)
else
@start_node
@start_node == (node._java_node || node) ? @end_node : @start_node
end
end


alias_method :get_other_node, :other_node # so it looks like the java version

def to_s
if start_node.nil?
"START NODE NIL"
elsif end_node.nil?
"END NODE NIL"
else
"id: #{self.object_id} @start_node: #{start_node.object_id}/#{start_node.persisted?} @end_node: #{end_node.object_id}/#{end_node.persisted?} type:#{@type}"
end
"id: #{self.object_id} start_node: #{start_node} end_node: #{end_node} type:#{@type}"
end

def id
Expand Down
20 changes: 13 additions & 7 deletions lib/neo4j/rails/relationships/relationships.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def _create_or_get_storage(rel_type, relationship_class = nil) #:nodoc:
@relationships[rel_type.to_sym] ||= Storage.new(self, rel_type, relationship_class)
end

# If the node is persisted it returns a Neo4j::NodeTraverser
# otherwise create a new object which will handle creating new relationships in memory.
# If not persisted the traversal method like prune, expand, filter etc. will not be available
# If the node is persisted and it does not have any unsaved relationship it returns a Neo4j::NodeTraverser.
# Otherwise it will return a NodesDSL which behaves like the Neo4j::NodeTraverser except that it does not
# allow to traverse both persisted and not persisted (not saved yet) relationship more then depth one.
#
# See, Neo4j::NodeRelationship#outgoing (when node is persisted) which returns a Neo4j::NodeTraverser
#
Expand All @@ -45,11 +45,14 @@ def outgoing(rel_type)
end


def create_relationship_to(other_node, rel_type)
def create_relationship_to(other_node, rel_type) #:nodoc:
storage = _create_or_get_storage(rel_type.name)
storage.create_relationship_to(other_node, :outgoing)
end

# Traverse or update an incoming relationship
# See #outgoing
# See, Neo4j::NodeRelationship#outgoing (when node is persisted) which returns a Neo4j::NodeTraverser
def incoming(rel_type)
storage = _create_or_get_storage(rel_type)
if persisted? && !storage.modified?
Expand All @@ -59,6 +62,9 @@ def incoming(rel_type)
end
end

# See Neo4j::Rels#rels.
# Will also allow to access unsaved relationships - like the #outgoing and #incoming method.
#
def rels(*rel_types)
storage = _create_or_get_storage(rel_types.first)

Expand All @@ -69,15 +75,15 @@ def rels(*rel_types)
end
end

def add_outgoing_rel(rel_type, rel)
def add_outgoing_rel(rel_type, rel) #:nodoc:
_create_or_get_storage(rel_type).add_outgoing_rel(rel)
end

def add_incoming_rel(rel_type, rel)
def add_incoming_rel(rel_type, rel) #:nodoc:
_create_or_get_storage(rel_type).add_incoming_rel(rel)
end

def rm_incoming_rel(rel_type, rel)
def rm_incoming_rel(rel_type, rel) #:nodoc:
_create_or_get_storage(rel_type).rm_incoming_rel(rel)
end
end
Expand Down
23 changes: 9 additions & 14 deletions lib/neo4j/rails/relationships/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ def each_node(dir, &block)
def single_relationship(dir)
rel = relationships(dir).first
if rel.nil? && @node.persisted?
rel = @node._java_node.getSingleRelationship(java_rel_type, dir_to_java(dir))
# TODO wrapper
java_rel = @node._java_node.getSingleRelationship(java_rel_type, dir_to_java(dir))
java_rel && java_rel.wrapper
else
rel
end

rel
end

def all_relationships(dir)
Expand All @@ -90,8 +90,9 @@ def all_relationships(dir)

def single_node(dir)
rel = single_relationship(dir)
puts "REL = #{rel}"
rel && rel.get_other_node(@node).wrapper # TODO wrapper
return nil if rel.nil?
other = rel.other_node(@node)
other && other.wrapper
end

def del_rel(rel)
Expand All @@ -107,9 +108,11 @@ def del_rel(rel)
def create_relationship_to(to, dir)
if dir == :outgoing
rel = @rel_class.new(@rel_type, @node, to, self)
# puts "create outgoing rel_class #{@rel_class} rel = #{rel} from #{@node} to #{to} of type #{@rel_type}"
to.class != Neo4j::Node && to.add_incoming_rel(@rel_type, rel)
add_outgoing_rel(rel)
else
# puts "create incoming rel #{@rel_class} from #{to} to #{@node} of type #{@rel_type}"
rel = @rel_class.new(@rel_type, to, @node, self)
@node.class != Neo4j::Node && to.add_outgoing_rel(@rel_type, rel)
add_incoming_rel(rel)
Expand All @@ -121,7 +124,6 @@ def add_incoming_rel(rel)
end

def add_outgoing_rel(rel)
puts "ADD OUTGOING for #{@node} #{rel}"
@outgoing_rels << rel
end

Expand Down Expand Up @@ -153,32 +155,25 @@ def valid?(context, validated_nodes)
end

def persist
puts "PERSIST #{@node} outgoing: #{@outgoing_rels.inspect}, @incoming_rels=#{@incoming_rels.inspect}"
success = true
@outgoing_rels.each do |rel|
success = rel.save
puts " ERROR #{rel.errors.inspect}" unless success
break unless success
end

puts " success = #{success}"

if success
@outgoing_rels.each do |rel|
rel.end_node.rm_incoming_rel(@rel_type.to_sym, rel)
end
@outgoing_rels.clear

@incoming_rels.each do |rel|
puts " persist incoming #{rel}"
success = rel.start_node.persisted? || rel.start_node.save
puts " ERROR #{rel.errors.inspect}" unless success
break unless success
end
success
end

puts " ret #{success}"
success
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/neo4j/traversal/traverser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def to_ary
def new(other_node)
case @dir
when org.neo4j.graphdb.Direction::OUTGOING
puts "FROM #{@from}/#{@from.class}, caller #{caller.inspect}"
@from.create_relationship_to(other_node, @type)
when org.neo4j.graphdb.Direction::INCOMING
other_node.create_relationship_to(@from, @type)
Expand Down
Loading

0 comments on commit 661fc79

Please sign in to comment.