-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
504 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
*.gem | ||
.bundle | ||
Gemfile.lock | ||
pkg/* | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--color |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source "http://rubygems.org" | ||
|
||
# Specify your gem's dependencies in neoid.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2012 Elad Ossadon | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require "bundler/gem_tasks" | ||
require 'rspec/core/rake_task' | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
|
||
task default: :spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require "neoid/version" | ||
require "neoid/model_config" | ||
require "neoid/model_additions" | ||
require "neoid/node" | ||
require "neoid/relationship" | ||
require "neoid/railtie" if defined? Rails | ||
|
||
module Neoid | ||
class << self | ||
attr_accessor :db | ||
attr_accessor :ref_node | ||
|
||
def db | ||
raise "Neoid.db wasn't supplied" unless @db | ||
@db | ||
end | ||
|
||
def ref_node | ||
@ref_node ||= Neography::Node.load(Neoid.db.get_root['self']) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module Neoid | ||
module ModelAdditions | ||
module ClassMethods | ||
def neoidable(options) | ||
@config = Neoid::ModelConfig.new | ||
yield(@config) if block_given? | ||
@neoidable_options = options | ||
end | ||
|
||
def neoidable_options | ||
@neoidable_options | ||
end | ||
end | ||
|
||
module InstanceMethods | ||
def to_neo | ||
{} | ||
end | ||
|
||
def neo_index_name | ||
@index_name ||= "#{self.class.name.tableize}_index" | ||
end | ||
|
||
protected | ||
def neo_properties_to_hash(*property_list) | ||
property_list.flatten.inject({}) { |all, property| | ||
all[property] = self.send(property) | ||
all | ||
} | ||
end | ||
|
||
private | ||
def _neo_representation | ||
@_neo_representation ||= begin | ||
results = neo_find_by_id | ||
if results | ||
neo_load(results.first['self']) | ||
else | ||
node = neo_create | ||
node | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Neoid | ||
class ModelConfig | ||
@properties = [] | ||
|
||
attr_accessor :properties | ||
|
||
def property(name) | ||
@properties << name | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
module Neoid | ||
module Node | ||
module ClassMethods | ||
def neo_subref_rel_type | ||
@_neo_subref_rel_type ||= "#{self.name.tableize}_subref" | ||
end | ||
def neo_subref_node_rel_type | ||
@_neo_subref_node_rel_type ||= self.name.tableize | ||
end | ||
|
||
def neo_subref_node | ||
@_neo_subref_node ||= begin | ||
subref_node_query = Neoid.ref_node.outgoing(neo_subref_rel_type) | ||
|
||
if subref_node_query.to_a.blank? | ||
node = Neography::Node.create(type: self.name, name: neo_subref_rel_type) | ||
Neography::Relationship.create( | ||
neo_subref_rel_type, | ||
Neoid.ref_node, | ||
node | ||
) | ||
else | ||
node = subref_node_query.first | ||
end | ||
|
||
node | ||
end | ||
end | ||
end | ||
|
||
module InstanceMethods | ||
def neo_find_by_id | ||
Neoid.db.get_node_index(neo_index_name, :ar_id, self.id) | ||
end | ||
|
||
def neo_create | ||
data = self.to_neo.merge(ar_type: self.class.name, ar_id: self.id) | ||
data.reject! { |k, v| v.nil? } | ||
|
||
node = Neography::Node.create(data) | ||
|
||
begin | ||
Neography::Relationship.create( | ||
self.class.neo_subref_node_rel_type, | ||
self.class.neo_subref_node, | ||
node | ||
) | ||
rescue Exception => e | ||
puts [$!.message] + $!.backtrace | ||
raise e | ||
end | ||
|
||
Neoid.db.add_node_to_index(neo_index_name, :ar_id, self.id, node) | ||
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(neo_index_name, neo_node) | ||
neo_node.del | ||
end | ||
end | ||
|
||
def self.included(receiver) | ||
Neoid.db.create_node_index(receiver.name.tableize) | ||
|
||
receiver.extend Neoid::ModelAdditions::ClassMethods | ||
receiver.send :include, Neoid::ModelAdditions::InstanceMethods | ||
receiver.extend ClassMethods | ||
receiver.send :include, InstanceMethods | ||
|
||
receiver.neo_subref_node # ensure | ||
|
||
receiver.after_destroy :neo_destroy | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
module Neoid | ||
module Relationship | ||
module InstanceMethods | ||
def neo_find_by_id | ||
Neoid.db.get_relationship_index(neo_index_name, :ar_id, self.id) | ||
end | ||
|
||
def neo_create | ||
options = self.class.neoidable_options | ||
|
||
start_node = self.send(options[:start_node]) | ||
end_node = self.send(options[:end_node]) | ||
|
||
return unless start_node && end_node | ||
|
||
relationship = Neography::Relationship.create( | ||
options[:type], | ||
start_node.neo_node, | ||
end_node.neo_node | ||
) | ||
|
||
Neoid.db.add_relationship_to_index(neo_index_name, :ar_id, self.id, relationship) | ||
|
||
relationship | ||
end | ||
|
||
def neo_load(relationship) | ||
Neography::Relationship.load(relationship) | ||
end | ||
|
||
def neo_destroy | ||
return unless neo_relationship | ||
Neoid.db.remove_relationship_from_index(neo_index_name, neo_relationship) | ||
puts neo_relationship.del | ||
end | ||
|
||
def neo_relationship | ||
_neo_representation | ||
end | ||
end | ||
|
||
def self.included(receiver) | ||
Neoid.db.create_relationship_index(receiver.name.tableize) | ||
|
||
receiver.extend Neoid::ModelAdditions::ClassMethods | ||
receiver.send :include, Neoid::ModelAdditions::InstanceMethods | ||
receiver.send :include, InstanceMethods | ||
|
||
receiver.after_create :neo_create | ||
receiver.after_destroy :neo_destroy | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module Neoid | ||
VERSION = "0.0.1.alpha" | ||
end |
Oops, something went wrong.