Skip to content

Commit

Permalink
Changed back to Neo4j::Rails::Model and Neo4j::Rails::Relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasronge committed Apr 14, 2012
1 parent 0d7ccb8 commit dfd59b8
Show file tree
Hide file tree
Showing 53 changed files with 472 additions and 433 deletions.
27 changes: 27 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,33 @@ Here are some of the major benefits of Neo4j.rb
* ACID Transaction with rollbacks support.


=== Public API

The neo4j gem depends on the neo4j-wrapper and neo4j-core gems.

==== neo4j gem

{Neo4j::Rails::Model}
{Neo4j::Rails::Relationship}
{Neo4j::Railtie}

==== neo4j-wrapper gem

{Neo4j::NodeMixin}
{Neo4j::RelationshipMixin}

==== neo4j-core gem

{Neo4j::Node} The Java Neo4j Node

{Neo4j::Relationship} The Java Relationship

{Neo4j} The Database

{Neo4j::Cypher} Cypher Query Generator, see RSpec spec/neo4j/cypher_spec

{Neo4j::Algo} Included algorithms, like shortest path

=== Some Examples

==== Neo4j::Node
Expand Down
12 changes: 6 additions & 6 deletions example/social/model.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class City < Neo4j::RailsNode
class City < Neo4j::Rails::Model

end

class State < Neo4j::RailsNode
class State < Neo4j::Rails::Model
property :name
index :name

Expand All @@ -21,11 +21,11 @@ def to_s

end

class Person < Neo4j::RailsNode
class Person < Neo4j::Rails::Model

end

class City < Neo4j::RailsNode
class City < Neo4j::Rails::Model
property :name
index :name

Expand All @@ -47,7 +47,7 @@ def to_s



class Person < Neo4j::RailsNode
class Person < Neo4j::Rails::Model
property :name
index :name
has_n :likes
Expand All @@ -67,7 +67,7 @@ def to_s

end

class Film < Neo4j::RailsNode
class Film < Neo4j::Rails::Model
property :title
index :title

Expand Down
2 changes: 0 additions & 2 deletions lib/neo4j.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

require 'neo4j/rails/rails'

require 'neo4j/rails_node'
require 'neo4j/rails_relationship'

require 'orm_adapter/adapters/neo4j'
Dir["#{File.dirname(__FILE__)}/tasks/**/*.rake"].each { |ext| load ext } if defined?(Rake) && respond_to?(:namespace)
6 changes: 3 additions & 3 deletions lib/neo4j/rails/accept_id.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Neo4j::Rails
# Allows accepting id for association objects.
# @example
# class Book < Neo4j::RailsNode
# class Book < Neo4j::Rails::Model
# has_one(:author).to(Author)
# accepts_id_for :author
# end
Expand All @@ -18,7 +18,7 @@ module ClassMethods
# Adds association_id getter and setter for one or more has_one associations
#
# @example
# class Book < Neo4j::RailsNode
# class Book < Neo4j::Rails::Model
# has_one(:author).to(Author)
# has_one(:publisher).to(Publisher)
# accepts_id_for :author, :publisher
Expand Down Expand Up @@ -57,7 +57,7 @@ def define_association_id_setter(association_name)
class_eval %Q{
def #{association_name}_id=(id)
relation_target_class = self.class._decl_rels[:#{association_name}].target_class
association_class = relation_target_class <= self.class ? Neo4j::RailsNode : relation_target_class
association_class = relation_target_class <= self.class ? Neo4j::Rails::Model : relation_target_class
self.#{association_name} = id.present? ? association_class.find(id) : nil
end
}, __FILE__, __LINE__
Expand Down
6 changes: 3 additions & 3 deletions lib/neo4j/rails/compositions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def composition_cache
# existing object) and how it can be turned back into attributes (when the entity is saved to
# the database).
#
# class Customer < Neo4j::RailsNode
# class Customer < Neo4j::Rails::Model
# composed_of :balance, :class_name => "Money", :mapping => %w(balance amount)
# composed_of :address, :mapping => [ %w(address_street street), %w(address_city city) ]
# end
Expand Down Expand Up @@ -103,7 +103,7 @@ def composition_cache
# unlike entity objects where equality is determined by identity. An entity class such as Customer can
# easily have two different objects that both have an address on Hyancintvej. Entity identity is
# determined by object or relational unique identifiers (such as primary keys). Normal
# Neo4j::RailsNode classes are entity objects.
# Neo4j::Rails::Model classes are entity objects.
#
# It's also important to treat the value objects as immutable. Don't allow the Money object to have
# its amount changed after creation. Create a new Money object with the new value instead. This
Expand Down Expand Up @@ -135,7 +135,7 @@ def composition_cache
# or an array. The <tt>:constructor</tt> and <tt>:converter</tt> options can be used to meet
# these requirements:
#
# class NetworkResource < Neo4j::RailsNode
# class NetworkResource < Neo4j::Rails::Model
# composed_of :cidr,
# :class_name => 'NetAddr::CIDR',
# :mapping => [ %w(network_address network), %w(cidr_range bits) ],
Expand Down
2 changes: 1 addition & 1 deletion lib/neo4j/rails/finders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def count
all.size
end

# Call this method if you are using Neo4j::RailsNode outside rails
# Call this method if you are using Neo4j::Rails::Model outside rails
# This method is automatically called by rails to close all lucene connections.
def close_lucene_connections
Thread.current[:neo4j_lucene_connection].each {|hits| hits.close} if Thread.current[:neo4j_lucene_connection]
Expand Down
114 changes: 114 additions & 0 deletions lib/neo4j/rails/model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
module Neo4j
module Rails
# Includes the Neo4j::NodeMixin and adds ActiveRecord/Model like behaviour.
# That means for example that you don't have to care about transactions since they will be
# automatically be created when needed.
#
# ==== Included Mixins
#
# * Neo4j::Rails::Persistence :: handles how to save, create and update the model
# * Neo4j::Rails::Attributes :: handles how to save and retrieve attributes
# * Neo4j::Rails::Mapping::Property :: allows some additional options on the #property class method
# * Neo4j::Rails::Serialization :: enable to_xml and to_json
# * Neo4j::Rails::Timestamps :: handle created_at, updated_at timestamp properties
# * Neo4j::Rails::Validations :: enable validations
# * Neo4j::Rails::Callbacks :: enable callbacks
# * Neo4j::Rails::Finders :: ActiveRecord style find
# * Neo4j::Rails::Relationships :: handles persisted and none persisted relationships.
# * Neo4j::Rails::Compositions :: see Neo4j::Rails::Compositions::ClassMethods, similar to http://api.rubyonrails.org/classes/ActiveRecord/Aggregations/ClassMethods.html
# * ActiveModel::Observing # enable observers, see Rails documentation.
# * ActiveModel::Translation - class mixin
#
# ==== Traversals
# This class only expose a limited set of traversals.
# If you want to access the raw java node to do traversals use the _java_node.
#
# class Person < Neo4j::Rails::Model
# end
#
# person = Person.find(...)
# person._java_node.outgoing(:foo).depth(:all)...
#
# ==== has_n and has_one
#
# The has_n and has_one relationship accessors returns objects of type Neo4j::Rails::Relationships::RelsDSL
# and Neo4j::Rails::Relationships::NodesDSL which behaves more like the Active Record relationships.
# Notice that unlike Neo4j::NodeMixin new relationships are kept in memory until @save@ is called.
#
# ==== Callbacks
#
# The following callbacks are supported :initialize, :validation, :create, :destroy, :save, :update.
# It works with before, after and around callbacks, see the Rails documentation.
# Notice you can also do callbacks using the Neo4j::Rails::Callbacks module (check the Rails documentation)
#
# ==== Examples
#
# person.outgoing(:friends) << other_person
# person.save!
#
# person.outgoing(:friends).map{|f| f.outgoing(:knows).to_a}.flatten
#
# ==== Examples
#
# Neo4j::Transaction.run do
# person._java_node.outgoing(:friends) << other_person
# end
#
# person._java_node.outgoing(:friends).outgoing(:knows).depth(4)
#
# Notice you can also declare outgoing relationships with the #has_n and #has_one class method.
#
# See Neo4j::Rails::Relationships#outgoing
# See Neo4j::Traversal#outgoing (when using it from the _java_node)
class Model
extend ActiveModel::Translation

include Neo4j::NodeMixin
include ActiveModel::Dirty # track changes to attributes
include ActiveModel::MassAssignmentSecurity # handle attribute hash assignment
include ActiveModel::Observing # enable observers
include Neo4j::Rails::Identity
include Neo4j::Rails::Persistence # handles how to save, create and update the model
include Neo4j::Rails::NodePersistence # handles how to save, create and update the model
include Neo4j::Rails::Attributes # handles how to save and retrieve attributes and override the property class method
include Neo4j::Rails::NestedAttributes
include Neo4j::Rails::HasN # allows some additional options on the #property class method
include Neo4j::Rails::Serialization # enable to_xml and to_json
include Neo4j::Rails::Validations # enable validations
include Neo4j::Rails::Callbacks # enable callbacks
include Neo4j::Rails::Timestamps # handle created_at, updated_at timestamp properties
include Neo4j::Rails::Finders # ActiveRecord style find
include Neo4j::Rails::Relationships # for none persisted relationships
include Neo4j::Rails::Compositions
include Neo4j::Rails::AcceptId
include Neo4j::Rails::Relationships

# --------------------------------------
# Public Class Methods
# --------------------------------------
class << self

##
# 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.
# @api public
def default_timezone
@default_timezone || :local
end

# @api public
def default_timezone=(zone)
@default_timezone = zone
end

# Set the i18n scope to overwrite ActiveModel.
#
# @return [ Symbol ] :neo4j
# @api public
def i18n_scope
:neo4j
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/neo4j/rails/nested_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def update_nested_attributes(rel_type, attr, options)
begin
# Check if we want to destroy not found nodes (e.g. {..., :_destroy => '1' } ?
destroy = attr.delete(:_destroy)
found = _find_node(rel_type, attr[:id]) || Neo4j::RailsNode.find(attr[:id])
found = _find_node(rel_type, attr[:id]) || Neo4j::Rails::Model.find(attr[:id])
if allow_destroy && destroy && destroy != '0'
found.destroy if found
else
Expand Down
2 changes: 1 addition & 1 deletion lib/neo4j/rails/rack_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def call(env)
status, headers, body = @app.call(env)
[status, headers, Body.new(body, enabled)]
ensure
Neo4j::RailsNode.close_lucene_connections
Neo4j::Rails::Model.close_lucene_connections
Neo4j.threadlocal_ref_node = Neo4j.default_ref_node
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/neo4j/rails/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@
require 'neo4j/rails/relationships/relationships'
require 'neo4j/rails/has_n'
require 'neo4j/rails/rack_middleware'
require 'neo4j/rails/versioning/versioning'
require 'neo4j/rails/versioning/versioning'
require 'neo4j/rails/model'
require 'neo4j/rails/relationship'
6 changes: 3 additions & 3 deletions lib/neo4j/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ class Railtie < ::Rails::Railtie
# instantiate them after being reloaded in the development environment
initializer "instantiate.observers" do
config.after_initialize do
::Neo4j::RailsNode.observers = config.neo4j.observers || []
::Neo4j::RailsNode.instantiate_observers
::Neo4j::Rails::Model.observers = config.neo4j.observers || []
::Neo4j::Rails::Model.instantiate_observers

ActionDispatch::Callbacks.to_prepare do
::Neo4j::RailsNode.instantiate_observers
::Neo4j::Rails::Model.instantiate_observers
end
end
end
Expand Down
50 changes: 50 additions & 0 deletions lib/neo4j/rails/relationship.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module Neo4j
module Rails
# Includes the Neo4j::RelationshipMixin and adds ActiveRecord/Model like behaviour.
# That means for example that you don't have to care about transactions since they will be
# automatically be created when needed.
#
# By default all relationships created by hte Neo4j::Rails::Model will be of this type unless it is specified by
# an has_n(...).relationship(relationship_class),
#
# Notice that this class works like any ActiveModel compliant object with callbacks and validations.
# It also implement timestamps (like active record), just add a updated_at or created_at attribute.
#
class Relationship
extend ActiveModel::Translation

include Neo4j::RelationshipMixin
include ActiveModel::Dirty # track changes to attributes
include ActiveModel::MassAssignmentSecurity # handle attribute hash assignment
include ActiveModel::Observing # enable observers
include Neo4j::Rails::Identity
include Neo4j::Rails::Persistence # handles how to save, create and update the model
include Neo4j::Rails::RelationshipPersistence # handles how to save, create and update the model
include Neo4j::Rails::Attributes # handles how to save and retrieve attributes
include Neo4j::Rails::Serialization # enable to_xml and to_json
include Neo4j::Rails::Validations # enable validations
include Neo4j::Rails::Callbacks # enable callbacks
include Neo4j::Rails::Timestamps # handle created_at, updated_at timestamp properties
include Neo4j::Rails::Finders # ActiveRecord style find
include Neo4j::Rails::Compositions

index :_classname # since there are no rule we have to use lucene to find all instance of a class


def to_s
"id: #{self.object_id} start_node: #{start_node.id} end_node: #{end_node.id} type:#{@type}"
end

# --------------------------------------
# Public Class Methods
# --------------------------------------
class << self
def _all
_indexer.find(:_classname => self)
end
end

end

end
end
Loading

0 comments on commit dfd59b8

Please sign in to comment.