Class: Neo4j::Rails::Model

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Translation, OrmAdapter::ToAdapter
Includes:
ActiveModel::Dirty, ActiveModel::MassAssignmentSecurity, ActiveModel::Observing, NodeMixin, AcceptId, Attributes, Callbacks, Compositions, Finders, HasN, Identity, NestedAttributes, NodePersistence, Persistence, Relationships, Serialization, Timestamps, Validations
Defined in:
lib/neo4j/rails/model.rb,
lib/orm_adapter/adapters/neo4j.rb

Overview

Makes Neo4j nodes and relationships behave like active record objects. That means for example that you don't (normally) have to care about transactions since they will be automatically be created when needed. Validation, Callbacks etc. are also supported.

Class Method Modules

Examples:

Create a node (learn more - see Persistence)

class Company < Neo4j::Rails::Model
end
Company.new.save
Company.save
Company.save(:name => 'Foo Ab')

Declare properties (learn more - see Attributes)


class Company < Neo4j::Rails::Model
  property :data
  property :revenue, :type => :Float
end

c = Company.new(:data => false, :type => '2123123.23')
c.data = "changed type"
c.revenue = 123124 # will always be converted

Creating and Navigating Relationships (learn more - see Relationships)

class Person < Neo4j::Rails::Model
end

person = Person.new
person.outgoing(:foo) << Person.new
person.save!
person.outgoing(:foo).depth(:all)...
person.outgoing(:friends).map{|f| f.outgoing(:knows).to_a}.flatten
person.rels(:outgoing, :foo).first.end_node #=> the other node

Declared Relationships (learn more - see HasN::ClassMethods)


class Person < Neo4j::Rails::Model
end

class Person
   has_n(:friends).to(Person)
   has_n(:employed_by)
end

Person.new.foo << other_node
Person.friends.build(:name => 'kalle').save

Searching with Lucene Index (learn more - see Finders::ClassMethods)


class Person < Neo4j::Rails::Model
  property :name
  property :age, :type => Fixnum, :index => :exact
end

Person.create(:name => 'kalle', :age => 42, :undeclared_prop => 3.14)
Person.find_by_age(42)

Searching with Cypher (learn more - Neo4j-core)


Monster.all.query(:strength => 17).first #=> a node/Neo4j::Rails::Model
Monster.all.query(:strength => 17).to_s  #=> "START n0=node(42) MATCH ..."
Neo4j.query{Neo4j.rb cypher DSL}
dungeon.monsters.query(:name => 'Ghost', :strength => 10) # all monsters with those properties
dungeon.monsters(:name => 'Ghost', :strength => 10) # same as above
dungeon.monsters { |m| m[:name] == 'Ghost'] & m[:strength] == 16} # same as above

Rules and Cypher (learn more Neoj::Wrapper::Rule::ClassMethods )

class Dungeon < Neo4j::Rails::Model
  has_n(:monsters).to(Monster)
end

class Monster < Neo4j::Rails::Model
  rule(:dangerous) { |m| m[:strength] > 15 }
end

class Room < Neo4j::Rails::Model
  has_n(:monsters).to(Monster)
end

@dungeon.monsters.dangerous { |m| rooms = m.incoming(Room.monsters); rooms }  # returns rooms we should avoid
@dungeon.monsters{|m| ret(m).asc(m[:strength])} # return the monsters nodes sorted by strength

Callbacks (learn more - see #Callbacks)


class Person < Neo4j::Rails::Model
  before_save :do_something
  def do_something
  end
end

Defined Under Namespace

Classes: OrmAdapter

Constant Summary

Constant Summary

Constants included from Timestamps

Timestamps::TIMESTAMP_PROPERTIES

Constants included from Callbacks

Callbacks::CALLBACKS

Class Method Summary (collapse)

Methods included from Compositions

#clear_composition_cache, #composition_cache

Methods included from Relationships

#_incoming, #_outgoing, #incoming, #node, #nodes, #outgoing, #rel, #rel?, #rels

Methods included from Timestamps

#create_timestamp, #new_or_changed?, #update_timestamp, #write_date_or_timestamp

Methods included from Callbacks

#destroy_with_callbacks, #valid_with_callbacks?

Methods included from Validations

#read_attribute_for_validation, #save, #valid?

Methods included from NestedAttributes

#update_nested_attributes

Methods included from TxMethods

#tx_methods

Methods included from Attributes

#_classname, #attribute?, #attribute_defaults, #attribute_names, #attributes, #attributes=, #hash, #property?, #property_changed?, #property_names, #props, #read_attribute, #read_attribute_with_type_conversion, #to_key, #to_model, #to_param, #update_attribute, #update_attributes, #update_attributes!, #write_attribute, #write_attribute_with_type_conversion

Methods included from NodePersistence

#create, #freeze_if_deleted, #initialize, #reload, #reload_from_database, #update

Methods included from Persistence

#create_or_updating?, #delete, #destroy, #destroyed?, #freeze, #frozen?, #new_record?, #persisted?, #save, #save!

Methods included from Identity

#==, #getId, #id, #neo_id

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Neo4j::Rails::Attributes

Class Method Details

+ (Object) default_timezone

Determines whether to use Time.local (using :local) or Time.utc (using :utc) when pulling dates and times from the database. This is set to :local by default.



133
134
135
# File 'lib/neo4j/rails/model.rb', line 133

def default_timezone
  @default_timezone || :local
end

+ (Object) default_timezone=(zone)



138
139
140
# File 'lib/neo4j/rails/model.rb', line 138

def default_timezone=(zone)
  @default_timezone = zone
end

+ (Symbol) i18n_scope

Set the i18n scope to overwrite ActiveModel.

Returns:

  • (Symbol)

    :neo4j



146
147
148
# File 'lib/neo4j/rails/model.rb', line 146

def i18n_scope
  :neo4j
end