Skip to content

Instantly share code, notes, and snippets.

@andreasronge
Created January 15, 2010 09:44
Show Gist options
  • Save andreasronge/277929 to your computer and use it in GitHub Desktop.
Save andreasronge/277929 to your computer and use it in GitHub Desktop.
require "rubygems"
require "neo4j"
class Person
include Neo4j::NodeMixin
# define Neo4j properties
property :name
# define an one way relationship to any other node
has_n :friends
end
Neo4j::Transaction.run do
neo = Person.new {|n| n.name = 'Neo' }
morpheus = Person.new {|n| n.name = 'Morpheus' }
trinity = Person.new {|n| n.name = 'Trinity' }
cypher = Person.new {|n| n.name = 'Cypher' }
cypher.friends << morpheus
smith = Person.new {|n| n.name = 'Agent Smith' }
architect = Person.new {|n| n.name = 'Architect' }
neo.friends << morpheus
morpheus.friends << trinity
cypher.friends << smith
trinity.rels.outgoing(:loves) << neo
architect.rels.outgoing(:has_coded) << smith
#find Neo's friends
neo.friends.each { |n| puts "Friend: #{n.name}" }
#Neo's friends of friends
neo.friends.depth(2).each { |n| puts "FOAF: #{n.name}" }
#who is in love?
architect.traverse.both(:friends, :has_coded, :loves).depth(:all).filter{ outgoing(:loves).to_a.size > 0 }.each do |n|
puts "In love: #{n.name}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment