Skip to content
Browse files

Ruby 1.8.7 support

  • Loading branch information...
1 parent adc123a commit b7edf39fa67c291587979fd88e6c73e8d0e9710f @bpaul committed Apr 4, 2012
View
20 lib/neoid/model_config.rb
@@ -3,43 +3,43 @@ class ModelConfig
attr_reader :properties
attr_reader :search_options
attr_reader :relationship_options
-
+
def initialize(klass)
@klass = klass
end
-
+
def stored_fields
@stored_fields ||= {}
end
-
+
def field(name, &block)
self.stored_fields[name] = block
end
-
+
def relationship(options)
@relationship_options = options
end
-
+
def search(&block)
raise "search needs a block" unless block_given?
@search_options = SearchConfig.new
- block.(@search_options)
+ block.call(@search_options)
end
-
+
def inspect
"#<Neoid::ModelConfig @properties=#{properties.inspect} @search_options=#{@search_options.inspect}>"
end
end
-
+
class SearchConfig
def index_fields
@index_fields ||= {}
end
-
+
def index(field, options = {})
index_fields[field] = options
end
-
+
def inspect
"#<Neoid::SearchConfig @index_fields=#{index_fields.inspect}>"
end
View
54 lib/neoid/node.rb
@@ -7,17 +7,17 @@ def neo_subref_rel_type
def neo_subref_node_rel_type
@_neo_subref_node_rel_type ||= self.name.tableize
end
-
+
def neo_subref_node
@_neo_subref_node ||= begin
Neoid::logger.info "Node#neo_subref_node #{neo_subref_rel_type}"
-
+
subref_node_query = Neoid.ref_node.outgoing(neo_subref_rel_type)
if subref_node_query.to_a.blank?
Neoid::logger.info "Node#neo_subref_node not existing, creating #{neo_subref_rel_type}"
-
- node = Neography::Node.create(type: self.name, name: neo_subref_rel_type)
+
+ node = Neography::Node.create(:type=> self.name, :name=> neo_subref_rel_type)
Neography::Relationship.create(
neo_subref_rel_type,
Neoid.ref_node,
@@ -28,7 +28,7 @@ def neo_subref_node
node = subref_node_query.first
Neoid::logger.info "Node#neo_subref_node existing, fetched #{neo_subref_rel_type}"
end
-
+
node
end
end
@@ -43,48 +43,48 @@ def search(*args)
}.join(" OR ")
when Hash
end
-
+
results = Neoid.db.find_node_index(self.neo_search_index_name, query)
-
+
SearchSession.new(results, self)
end
end
-
+
module InstanceMethods
def neo_find_by_id
Neoid::logger.info "Node#neo_find_by_id #{self.class.neo_index_name} #{self.id}"
- Neoid.db.get_node_index(self.class.neo_index_name, :ar_id, self.id)
+ Neoid.db.get_node_index(self.class.neo_index_name,:ar_id, self.id)
end
-
+
def neo_create
return unless Neoid.enabled?
-
- data = self.to_neo.merge(ar_type: self.class.name, ar_id: self.id)
-
+
+ data = self.to_neo.merge(:ar_type=> self.class.name, :ar_id=> self.id)
+
node = Neography::Node.create(data)
-
+
Neography::Relationship.create(
self.class.neo_subref_node_rel_type,
self.class.neo_subref_node,
node
)
-
+
Neoid.db.add_node_to_index(self.class.neo_index_name, :ar_id, self.id, node)
Neoid::logger.info "Node#neo_create #{self.class.name} #{self.id}, index = #{self.class.neo_index_name}"
-
+
neo_search_index
node
end
-
+
def neo_update
Neoid.db.set_node_properties(neo_node, self.to_neo)
neo_search_index
-
+
neo_node
end
-
+
def neo_search_index
return if self.class.neoid_config.search_options.blank? || self.class.neoid_config.search_options.index_fields.blank?
@@ -94,35 +94,35 @@ def neo_search_index
value = self.send(field) rescue (raise "No field #{field} for #{self.class.name}")
Neoid.db.add_node_to_index(self.class.neo_search_index_name, field, value, neo_node.neo_id)
}
-
+
neo_node
end
-
+
def neo_load(node)
Neography::Node.load(node)
end
-
+
def neo_node
_neo_representation
end
-
+
def neo_destroy
return unless neo_node
Neoid.db.remove_node_from_index(self.class.neo_index_name, neo_node)
neo_node.del
end
end
-
+
def self.included(receiver)
receiver.send :include, Neoid::ModelAdditions
receiver.extend ClassMethods
receiver.send :include, InstanceMethods
-
+
receiver.neo_subref_node # ensure
-
+
receiver.after_create :neo_create
receiver.after_update :neo_update
receiver.after_destroy :neo_destroy
end
end
-end
+end
View
10 lib/neoid/search_session.rb
@@ -4,17 +4,17 @@ def initialize(response, model)
@response = response || []
@model = model
end
-
+
def hits
@response.map { |x|
Neography::Node.new(x)
}
end
-
+
def results
ids = @response.collect { |x| x['data']['ar_id'] }
-
- @model.where(id: ids)
+
+ @model.where(id=> ids)
end
end
-end
+end
View
46 spec/neoid/model_additions_spec.rb
@@ -7,67 +7,67 @@
it "should call neo_create on a neo_node for user" do
User.any_instance.should_receive(:neo_create)
- User.create(name: "Elad Ossadon")
+ User.create(:name=> "Elad Ossadon")
end
-
+
it "should create a neo_node for user" do
- user = User.create(name: "Elad Ossadon", slug: "elado")
-
+ user = User.create(:name=> "Elad Ossadon", :slug=> "elado")
+
user.neo_node.should_not be_nil
-
+
user.neo_node.ar_id.should == user.id
user.neo_node.name.should == user.name
user.neo_node.slug.should == user.slug
end
it "should create a neo_node for movie" do
- movie = Movie.create(name: "Memento", slug: "memento-1999", year: 1999)
-
+ movie = Movie.create(:name=> "Memento", :slug=> "memento-1999", :year=> 1999)
+
movie.neo_node.should_not be_nil
-
+
movie.neo_node.ar_id.should == movie.id
movie.neo_node.name.should == movie.name
movie.neo_node.year.should == movie.year
end
end
-
+
context "find by id" do
it "should find a neo_node for user" do
- user = User.create(name: "Elad Ossadon", slug: "elado")
-
+ user = User.create(:name=> "Elad Ossadon", :slug=> "elado")
+
user.neo_node.should_not be_nil
user.neo_find_by_id.should_not be_nil
end
end
end
-
+
context "relationships" do
- let(:user) { User.create(name: "Elad Ossadon", slug: "elado") }
- let(:movie) { Movie.create(name: "Memento", slug: "memento-1999", year: 1999) }
-
+ let(:user) { User.create(:name=> "Elad Ossadon", :slug=> "elado") }
+ let(:movie) { Movie.create(:name=> "Memento", :slug=> "memento-1999", :year=> 1999) }
+
it "should create a relationship on neo4j" do
user.like! movie
like = user.likes.first
-
+
like.neo_find_by_id.should_not be_nil
-
+
like.neo_relationship.should_not be_nil
-
+
like.neo_relationship.start_node.should == user.neo_node
like.neo_relationship.end_node.should == movie.neo_node
like.neo_relationship.rel_type.should == 'likes'
end
-
+
it "should delete a relationship on deleting a record" do
user.like! movie
like = user.likes.first
-
+
relationship_neo_id = like.neo_relationship.neo_id
-
+
Neography::Relationship.load(relationship_neo_id).should_not be_nil
-
+
user.unlike! movie
-
+
Neography::Relationship.load(relationship_neo_id).should be_nil
end
end
View
4 spec/neoid/model_config_spec.rb
@@ -16,8 +16,8 @@
end
it "should store stored fields based on blocks" do
- article = Article.create! title: "Hello", year: 2012
-
+ article = Article.create! :title => "Hello", :year => 2012
+
article.neo_node.title_length.should == article.title.length
end
end
View
30 spec/neoid/search_spec.rb
@@ -4,18 +4,18 @@
describe Neoid::ModelAdditions do
context "search" do
let(:index_name) { "articles_search_index_#{Time.now.to_f.to_s.gsub('.', '')}" }
-
+
before(:each) do
Article.stub!(:neo_search_index_name).and_return(index_name)
end
-
+
it "should index and find node in fulltext" do
Neoid.db.create_node_index(index_name, "fulltext", "lucene")
-
- n = Neography::Node.create(name: "test hello world", year: 2012)
+
+ n = Neography::Node.create(:name => "test hello world", :year => 2012)
Neoid.db.add_node_to_index(index_name, "name", n.name, n)
Neoid.db.add_node_to_index(index_name, "year", n.year, n)
-
+
[
"name:test",
"year:2012",
@@ -26,10 +26,10 @@
Neoid.db.send(:get_id, results).should == n.neo_id
}
end
-
+
it "should index item on save" do
- article = Article.create!(title: "Hello world", body: "Lorem ipsum dolor sit amet", year: 2012)
-
+ article = Article.create!(:title => "Hello world", :body => "Lorem ipsum dolor sit amet", :year => 2012)
+
[
"title:Hello",
"year:2012",
@@ -41,22 +41,22 @@
Neoid.db.send(:get_id, results).should == article.neo_node.neo_id
}
end
-
+
context "search session" do
it "should return a search session" do
Article.search("hello").should be_a(Neoid::SearchSession)
end
-
+
it "should find hits" do
- article = Article.create!(title: "Hello world", body: "Lorem ipsum dolor sit amet", year: 2012)
-
+ article = Article.create!(:title => "Hello world", :body => "Lorem ipsum dolor sit amet", :year => 2012)
+
Article.search("hello").hits.should == [ article.neo_node ]
end
-
+
it "should find results" do
- article = Article.create!(title: "Hello world", body: "Lorem ipsum dolor sit amet", year: 2012)
+ article = Article.create!(:title => "Hello world", :body => "Lorem ipsum dolor sit amet", :year => 2012)
- Article.search("hello").results.should == [ article ]
+ Article.search("Hello").results.should == [ article ]
end
end
end
View
2 spec/support/connection.rb
@@ -1 +1 @@
-ActiveRecord::Base.establish_connection adapter: "sqlite3", database: File.join(File.dirname(__FILE__), "db/test.sqlite3")
+ActiveRecord::Base.establish_connection :adapter=> "sqlite3", :database=> File.join(File.dirname(__FILE__), "db/test.sqlite3")
View
34 spec/support/models.rb
@@ -1,23 +1,23 @@
class User < ActiveRecord::Base
include ActiveModel::Validations::Callbacks
-
+
has_many :likes
- has_many :movies, through: :likes
-
+ has_many :movies, :through => :likes
+
def likes?(movie)
- likes.where(movie_id: movie.id).exists?
+ likes.where(:movie_id=> movie.id).exists?
end
-
+
def like!(movie)
movies << movie unless likes?(movie)
end
-
+
def unlike!(movie)
- likes.where(movie_id: movie.id, user_id: self.id).destroy_all
+ likes.where(:movie_id=> movie.id, :user_id=> self.id).destroy_all
end
-
+
include Neoid::Node
-
+
neoidable do |c|
c.field :name
c.field :slug
@@ -26,12 +26,12 @@ def unlike!(movie)
class Movie < ActiveRecord::Base
include ActiveModel::Validations::Callbacks
-
+
has_many :likes
- has_many :users, through: :likes
-
+ has_many :users, :through=> :likes
+
include Neoid::Node
-
+
neoidable do |c|
c.field :name
c.field :slug
@@ -41,14 +41,14 @@ class Movie < ActiveRecord::Base
class Like < ActiveRecord::Base
include ActiveModel::Validations::Callbacks
-
+
belongs_to :user
belongs_to :movie
-
+
include Neoid::Relationship
-
+
neoidable do |c|
- c.relationship start_node: :user, end_node: :movie, type: :likes
+ c.relationship :start_node=> :user, :end_node=> :movie, :type => :likes
c.field :rate
end
end

0 comments on commit b7edf39

Please sign in to comment.
Something went wrong with that request. Please try again.