Skip to content

Instantly share code, notes, and snippets.

@andreasronge
Created January 5, 2012 13:42
Show Gist options
  • Save andreasronge/1565312 to your computer and use it in GitHub Desktop.
Save andreasronge/1565312 to your computer and use it in GitHub Desktop.
Domain Example
# load 'nodes.rb'
require 'rubygems'
require 'neo4j'
alice = Neo4j::Transaction.run { Neo4j::Node.new }
bob = Neo4j::Transaction.run { Neo4j::Node.new }
Neo4j::Transaction.run do
alice[:name] = "alice"
alice[:score] = [42,4]
bob[:name] = "bob"
bob[:score] = 13.42
end
#load 'properties.rb'
Neo4j::Transaction.run do
Neo4j::Relationship.new(:friends, alice, bob, :since => 2006)
end
puts alice.outgoing(:friends).first[:name]
class Tweet < Neo4j::Rails::Model
end
class Person < Neo4j::Rails::Model
property :name
property :age, :type => Fixnum
has_n :friends
has_n(:tweets).to(Tweet)
end
p = Person.new
p.name = "andreas"
p.friends << Person.new
p.save!
require 'rubygems'
require 'neo4j'
puts "Using #{Neo4j::VERSION}"
class Tweet < Neo4j::Rails::Model
end
class Person < Neo4j::Rails::Model
property :name
property :age, :type => Fixnum
has_n :friends
has_n(:tweets).to(Tweet)
end
p = Person.new
p.name = "andreas"
p.friends << Person.new
p.save!
puts "Person #{p.name} has #{p.friends.count}"
require "rubygems"
require "neo4j-core"
alice = Neo4j::Transaction.run { Neo4j::Node.new }
bob = Neo4j::Transaction.run { Neo4j::Node.new }
Neo4j::Transaction.run do
alice[:name] = "alice"
alice[:score] = [42,4]
bob[:name] = "bob"
bob[:score] = 13.42
end
Neo4j::Transaction.run do
Neo4j::Relationship.new(:friends, alice, bob, :since => 2006)
end
require 'rubygems'
require 'neo4j'
class Enrollment < Neo4j::Rails::Relationship
property :mark_received
end
class Seminar < Neo4j::Rails::Model
property :name
property :number
has_one :waiting_list
# Creates a convenient method for navigating and creating incoming relationships
# to outgoing relationship from Student of type enrolled
has_n(:students).from("Student", :enrolled)
end
class Student < Neo4j::Rails::Model
property :name
has_n(:enrolled).to(Seminar).relationship(Enrollment)
end
class WaitingList < Neo4j::Rails::Model
include Enumerable
has_one :first_student
def last_student
first_student && first_student.outgoing(:next_student).include_start_node.depth(:all).filter{|path| !path.end_node.rel?(:outgoing, :next_student)}.first
end
def <<(student)
if first_student
last_student.outgoing(:next_student) << student
student.save!
else
self.first_student = student
save!
end
self
end
def each
curr = first_student
while(curr) do
yield curr
curr = curr.node(:outgoing, :next_student)
end
end
end
seminar = Seminar.create(:name => "introductory programming class", :number => 6001, :waiting_list => WaitingList.new)
seminar.students << Student.create(:name => 'bob')
seminar.save!
seminar.waiting_list << Student.create(:name => 'alice') << Student.create(:name => 'ted') << Student.create(:name => 'carol')
puts "Enrolled Students for #{seminar.name}"
seminar.students.each {|student| puts "Student #{student.name} enrolled on #{student.enrolled.count} seminars"}
puts "Waiting list"
seminar.waiting_list.each {|student| puts student.name}
class Seminar < Neo4j::Rails::Model
property :name, :index => :fulltext
property :number, :index => :exact
end
Seminar.create(:name => "introductory programming class", :number => 6001)
puts Seminar.find_by_number(6001).name
require 'rubygems'
require 'neo4j'
load 'seminar2.rb'
load 'seminar_query.rb'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment