Ruby

From NeoWiki

Jump to: navigation, search
Neo4j.rb is a graph database framework for JRuby built on top of the neo4j Java library.

[edit] Downloads and documentation

[edit] Introduction

There is an example using IMDB data. It's possible to navigate and create relationships from both direction between actors and movies using the "acted_in" and "actors" methods that are defined by using the NodeMixin class method has_n.(see below in the imdb model), so:

willis.acted_in << die_hard
# is the same as
die_hard.actors << willis

Neo4j.rb also support indexing relationships, so a (stupid) query for finding all movies produced 2001 with actors of age between 20 and 30 would be

Movie.find("actors.age" => 20..30,  :year => 2001)

or for find the actors in those movies:

Actors.find("acted_in.year" => 2001,  :age => 20..30)

This feature is not shown in the imdb example, only in some RSpec test code.

The complete model for IMDB looks like this (def to_s is like toString method in Java)

class Role
  include Neo4j::RelationMixin
  properties :title, :character

  def to_s
    "Role title #{self.title} character #{self.character}"
  end
end

class Actor
  include Neo4j::NodeMixin
  properties :name
  has_n(:acted_in).to(Movie).relation(Role)
  index :name, :tokenized => true

  def to_s
    "Actor #{self.name}"
  end
end

class Movie
  include Neo4j::NodeMixin
  properties :title
  properties :year

  # defines a method for traversing incoming acted_in relationships from Actor
  has_n(:actors).from(Actor, :acted_in)

  def to_s
    "Movie #{self.title}"
  end
end

To run the example run the install.sh script to download the database then run the create and find ruby scripts.

For a closer look, head over to @github!

Personal tools