Skip to content

Commit

Permalink
First commit. Copied from neo4apis-twitter
Browse files Browse the repository at this point in the history
  • Loading branch information
cheerfulstoic committed Jan 6, 2015
0 parents commit 6ebc429
Show file tree
Hide file tree
Showing 15 changed files with 476 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
/pkg
2 changes: 2 additions & 0 deletions .rvmrc
@@ -0,0 +1,2 @@
rvm use 2.1.5@neo4apis-activerecord

17 changes: 17 additions & 0 deletions Gemfile
@@ -0,0 +1,17 @@
source 'https://rubygems.org'

gemspec

group :development do
gem 'pry'
gem 'neo4apis', path: '../neo4apis'
end

group :test do
gem 'rspec'
gem 'sqlite3'

gem 'guard'
gem 'guard-rspec'
end

126 changes: 126 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,126 @@
PATH
remote: .
specs:
neo4apis-activerecord (0.7.0)
activerecord (~> 4.0)
neo4apis (~> 0.4.0)

PATH
remote: ../neo4apis
specs:
neo4apis (0.4.2)
colorize (~> 0.7.3)
faraday (~> 0.9.0)
neo4j-core (~> 3.0)
thor (~> 0.19.1)

GEM
remote: https://rubygems.org/
specs:
activemodel (4.2.0)
activesupport (= 4.2.0)
builder (~> 3.1)
activerecord (4.2.0)
activemodel (= 4.2.0)
activesupport (= 4.2.0)
arel (~> 6.0)
activesupport (4.2.0)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
arel (6.0.0)
builder (3.2.2)
celluloid (0.16.0)
timers (~> 4.0.0)
coderay (1.1.0)
colorize (0.7.5)
diff-lcs (1.2.5)
faraday (0.9.0)
multipart-post (>= 1.2, < 3)
faraday_middleware (0.9.1)
faraday (>= 0.7.4, < 0.10)
ffi (1.9.6)
formatador (0.2.5)
guard (2.10.5)
formatador (>= 0.2.4)
listen (~> 2.7)
lumberjack (~> 1.0)
nenv (~> 0.1)
pry (>= 0.9.12)
thor (>= 0.18.1)
guard-compat (1.2.0)
guard-rspec (4.5.0)
guard (~> 2.1)
guard-compat (~> 1.1)
rspec (>= 2.99.0, < 4.0)
hitimes (1.2.2)
httparty (0.13.3)
json (~> 1.8)
multi_xml (>= 0.5.2)
httpclient (2.6.0.1)
i18n (0.7.0)
json (1.8.1)
listen (2.8.4)
celluloid (>= 0.15.2)
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
lumberjack (1.0.9)
method_source (0.8.2)
minitest (5.5.0)
multi_xml (0.5.5)
multipart-post (2.0.0)
nenv (0.1.1)
neo4j-core (3.1.1)
activesupport
faraday (~> 0.9.0)
faraday_middleware (~> 0.9.1)
httparty
httpclient
json
net-http-persistent
os
zip
net-http-persistent (2.9.4)
os (0.9.6)
pry (0.10.1)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rb-fsevent (0.9.4)
rb-inotify (0.9.5)
ffi (>= 0.5.0)
rspec (3.1.0)
rspec-core (~> 3.1.0)
rspec-expectations (~> 3.1.0)
rspec-mocks (~> 3.1.0)
rspec-core (3.1.7)
rspec-support (~> 3.1.0)
rspec-expectations (3.1.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.1.0)
rspec-mocks (3.1.3)
rspec-support (~> 3.1.0)
rspec-support (3.1.2)
slop (3.6.0)
sqlite3 (1.3.10)
thor (0.19.1)
thread_safe (0.3.4)
timers (4.0.1)
hitimes
tzinfo (1.2.2)
thread_safe (~> 0.1)
zip (2.0.2)

PLATFORMS
ruby

DEPENDENCIES
guard
guard-rspec
neo4apis!
neo4apis-activerecord!
pry
rspec
sqlite3
6 changes: 6 additions & 0 deletions Guardfile
@@ -0,0 +1,6 @@
guard :rspec, cmd: 'rspec' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
end

21 changes: 21 additions & 0 deletions LICENCE.txt
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) <year> <copyright holders>

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.
Empty file added README.md
Empty file.
2 changes: 2 additions & 0 deletions Rakefile
@@ -0,0 +1,2 @@
require 'rake'
require "bundler/gem_tasks"
5 changes: 5 additions & 0 deletions lib/neo4apis-activerecord.rb
@@ -0,0 +1,5 @@
require 'neo4apis'

require 'neo4apis/activerecord'
require 'neo4apis/cli/activerecord'

44 changes: 44 additions & 0 deletions lib/neo4apis/activerecord.rb
@@ -0,0 +1,44 @@
require 'neo4apis'
require 'ostruct'

module Neo4Apis
class ActiveRecord < Base
prefix nil

batch_size 1000

def self.model_importer(model_class)
self.uuid model_class.name.to_sym, model_class.primary_key

self.importer model_class.name.to_sym do |object|
node = add_node model_class.name.to_sym, object, model_class.column_names

model_class.reflect_on_all_associations.each do |association_reflection|
case association_reflection.macro
when :belongs_to, :has_one
if options[:"import_#{association_reflection.macro}"]
referenced_object = object.send(association_reflection.name)
referenced_class = referenced_object.class
referenced_node = add_node referenced_class.name.to_sym, referenced_object, referenced_class.column_names

add_relationship association_reflection.name, node, referenced_node
end
when :has_many
if options[:import_has_many]
object.send(association_reflection.name).each do |referenced_object|
referenced_class = referenced_object.class
referenced_node = add_node referenced_class.name.to_sym, referenced_object, referenced_class.column_names

add_relationship association_reflection.name, node, referenced_node
end
end
end
end
end

end

end

end

54 changes: 54 additions & 0 deletions lib/neo4apis/cli/activerecord.rb
@@ -0,0 +1,54 @@
require 'active_record'
require 'active_support/inflector'
require 'thor'
require 'colorize'

module Neo4Apis
module CLI
class ActiveRecord < Thor
class_option :config_path, type: :string, default: 'config/database.yml'

class_option :import_belongs_to, type: :boolean, default: false
class_option :import_has_one, type: :boolean, default: false
class_option :import_has_many, type: :boolean, default: false


desc "table TABLE_NAME_OR_MODEL", "Import SQL table from ActiveRecord model"
def table(table_name_or_model)
setup

model_class = table_name_or_model.classify.constantize

::Neo4Apis::ActiveRecord.model_importer(model_class)

neo4apis_client.batch do
model_class.find_each do |object|
neo4apis_client.import model_class.name.to_sym, object
end
end
end

private

def setup
require './config/environment'
end

NEO4APIS_CLIENT_CLASS = ::Neo4Apis::ActiveRecord

def neo4apis_client
@neo4apis_client ||= NEO4APIS_CLIENT_CLASS.new(Neo4j::Session.open(:server_db, parent_options[:neo4j_url]),
import_belongs_to: options[:import_belongs_to],
import_has_one: options[:import_has_one],
import_has_many: options[:import_has_many])
end

end

class Base < Thor
desc "activerecord SUBCOMMAND ...ARGS", "methods of importing data automagically from Twitter"
subcommand "activerecord", CLI::ActiveRecord
end
end
end

24 changes: 24 additions & 0 deletions neo4apis-activerecord.gemspec
@@ -0,0 +1,24 @@
lib = File.expand_path('../lib/', __FILE__)
$:.unshift lib unless $:.include?(lib)

Gem::Specification.new do |s|
s.name = "neo4apis-activerecord"
s.version = '0.7.0'
s.required_ruby_version = ">= 1.9.1"

s.authors = "Brian Underwood"
s.email = 'public@brian-underwood.codes'
s.homepage = "https://github.com/neo4jrb/neo4apis-activerecord/"
s.summary = "An ruby gem to import SQL data to neo4j using activerecord"
s.license = 'MIT'
s.description = <<-EOF
A ruby gem using neo4apis to make importing SQL data to neo4j easy
EOF

s.require_path = 'lib'
s.files = Dir.glob("{bin,lib,config}/**/*") + %w(README.md Gemfile neo4apis-activerecord.gemspec)

s.add_dependency('neo4apis', '~> 0.4.0')
s.add_dependency('activerecord', '~> 4.0')

end

0 comments on commit 6ebc429

Please sign in to comment.