neo4apis-activerecord 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e872c76387db29333b1d6b009ea65045888f2fa2
4
+ data.tar.gz: 938e81267ab658e6857050cb20ee204127ae87a4
5
+ SHA512:
6
+ metadata.gz: dca90c53f4e3cb387333e67ffbd2c0b2ebbf2a7ab088e04e3508507909d0a104340e991c381dda82c4311117f43d34d403d3cba1d65511da6d6337bd342b9c4b
7
+ data.tar.gz: 8c6f51d5a96207ab8e69f8679cd7c5e29a044ba860896f5de19fb564769f3309ce494971bbe6aed7fd815b8e8515e79149346bd7b3e5ddc300f4ff2ae586daa7
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem 'pry'
7
+ gem 'neo4apis', path: '../neo4apis'
8
+ gem 'rubocop'
9
+ end
10
+
11
+ group :test do
12
+ gem 'rspec'
13
+ gem 'sqlite3'
14
+
15
+ gem 'guard'
16
+ gem 'guard-rspec'
17
+ end
File without changes
@@ -0,0 +1,4 @@
1
+ require 'neo4apis'
2
+
3
+ require 'neo4apis/activerecord'
4
+ require 'neo4apis/cli/activerecord'
@@ -0,0 +1,56 @@
1
+ require 'neo4apis'
2
+ require 'ostruct'
3
+
4
+ module Neo4Apis
5
+ class ActiveRecord < Base
6
+ prefix nil
7
+
8
+ batch_size 1000
9
+
10
+ def self.model_importer(model_class)
11
+ uuid model_class.name.to_sym, model_class.primary_key
12
+
13
+ importer model_class.name.to_sym do |object|
14
+ node = add_model_node model_class, object
15
+
16
+ model_class.reflect_on_all_associations.each do |association_reflection|
17
+ case association_reflection.macro
18
+ when :belongs_to, :has_one
19
+ if options[:"import_#{association_reflection.macro}"]
20
+ referenced_object = object.send(association_reflection.name)
21
+ add_model_relationship association_reflection.name, node, referenced_object
22
+ end
23
+ when :has_many
24
+ if options[:import_has_many]
25
+ object.send(association_reflection.name).each do |referenced_object|
26
+ add_model_relationship association_reflection.name, node, referenced_object
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ def add_model_relationship(relationship_name, node, referenced_object)
35
+ referenced_class = referenced_object.class
36
+ referenced_node = add_model_node referenced_class, referenced_object
37
+
38
+ add_relationship relationship_name, node, referenced_node
39
+ end
40
+
41
+ def add_model_node(model_class, object)
42
+ object_data = OpenStruct.new
43
+
44
+ object.attributes.each do |column, value|
45
+ v = if coder = model_class.serialized_attributes[column]
46
+ coder.dump(value)
47
+ else
48
+ value
49
+ end
50
+ object_data.send("#{column}=", v)
51
+ end
52
+
53
+ add_node model_class.name.to_sym, object_data, model_class.column_names
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,100 @@
1
+ require 'active_record'
2
+ require 'active_support/inflector'
3
+ require 'thor'
4
+ require 'colorize'
5
+
6
+ module Neo4Apis
7
+ module CLI
8
+ class ActiveRecord < Thor
9
+ class_option :config_path, type: :string, default: 'config/database.yml'
10
+
11
+ class_option :import_associations, type: :boolean, default: false
12
+ class_option :import_belongs_to, type: :boolean, default: nil
13
+ class_option :import_has_one, type: :boolean, default: nil
14
+ class_option :import_has_many, type: :boolean, default: nil
15
+
16
+ class_option :guess_associations, type: :boolean, default: false
17
+
18
+ class_option :active_record_config_path, type: :string, default: './config/database.yml'
19
+ class_option :active_record_environment, type: :string, default: 'development'
20
+
21
+ desc 'models MODELS_OR_TABLE_NAMES', 'Import SQL tables via ActiveRecord models'
22
+ def models(*models_or_table_names)
23
+ setup
24
+
25
+ puts 'models_or_table_names', models_or_table_names.inspect
26
+ model_classes = models_or_table_names.map(&method(:get_model))
27
+
28
+ model_classes.each do |model_class|
29
+ ::Neo4Apis::ActiveRecord.model_importer(model_class)
30
+ end
31
+
32
+ neo4apis_client.batch do
33
+ model_classes.each do |model_class|
34
+ model_class.find_each do |object|
35
+ neo4apis_client.import model_class.name.to_sym, object
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def setup
44
+ if File.exist?('config/environment.rb')
45
+ # Rails
46
+ require './config/environment'
47
+ else
48
+ puts 'active_record_config', active_record_config.inspect
49
+ ::ActiveRecord::Base.establish_connection(active_record_config)
50
+ end
51
+ end
52
+
53
+ NEO4APIS_CLIENT_CLASS = ::Neo4Apis::ActiveRecord
54
+
55
+ def neo4apis_client
56
+ @neo4apis_client ||= NEO4APIS_CLIENT_CLASS.new(Neo4j::Session.open(:server_db, parent_options[:neo4j_url]),
57
+ import_belongs_to: import_association?(:belongs_to),
58
+ import_has_one: import_association?(:has_one),
59
+ import_has_many: import_association?(:has_many))
60
+ end
61
+
62
+ def import_association?(type)
63
+ options[:"import_#{type}"].nil? ? options[:import_associations] : options[:"import_#{type}"]
64
+ end
65
+
66
+ def get_model(model_or_table_name)
67
+ model_class = model_or_table_name
68
+ model_class = model_or_table_name.classify unless model_or_table_name.match(/^[A-Z]/)
69
+ model_class.constantize
70
+ rescue NameError
71
+ Object.const_set(model_class, Class.new(::ActiveRecord::Base)).tap do |model_class|
72
+ apply_guessed_model_associations!(model_class) if options[:guess_associations]
73
+ end
74
+ end
75
+
76
+ def apply_guessed_model_associations!(model_class)
77
+ model_class.columns.each do |column|
78
+ next if not column.name.match(/_id$/)
79
+
80
+ begin
81
+ base = column.name.humanize.tableize.split(' ').join('_')
82
+
83
+ model_class.belongs_to base.singularize.to_sym
84
+ rescue NameError
85
+ end
86
+ end
87
+ end
88
+
89
+ def active_record_config
90
+ require 'yaml'
91
+ YAML.load(File.read(options[:active_record_config_path]))[options[:active_record_environment]]
92
+ end
93
+ end
94
+
95
+ class Base < Thor
96
+ desc 'activerecord SUBCOMMAND ...ARGS', 'methods of importing data automagically from Twitter'
97
+ subcommand 'activerecord', CLI::ActiveRecord
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,23 @@
1
+ lib = File.expand_path('../lib/', __FILE__)
2
+ $LOAD_PATH.unshift lib unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'neo4apis-activerecord'
6
+ s.version = '0.1.0'
7
+ s.required_ruby_version = '>= 1.9.1'
8
+
9
+ s.authors = 'Brian Underwood'
10
+ s.email = 'public@brian-underwood.codes'
11
+ s.homepage = 'https://github.com/neo4jrb/neo4apis-activerecord/'
12
+ s.summary = 'An ruby gem to import SQL data to neo4j using activerecord'
13
+ s.license = 'MIT'
14
+ s.description = <<-EOF
15
+ A ruby gem using neo4apis to make importing SQL data to neo4j easy
16
+ EOF
17
+
18
+ s.require_path = 'lib'
19
+ s.files = Dir.glob('{bin,lib,config}/**/*') + %w(README.md Gemfile neo4apis-activerecord.gemspec)
20
+
21
+ s.add_dependency('neo4apis', '~> 0.4.0')
22
+ s.add_dependency('activerecord', '~> 4.0')
23
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: neo4apis-activerecord
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Underwood
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: neo4apis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.4.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.4.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ description: |
42
+ A ruby gem using neo4apis to make importing SQL data to neo4j easy
43
+ email: public@brian-underwood.codes
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - Gemfile
49
+ - README.md
50
+ - lib/neo4apis-activerecord.rb
51
+ - lib/neo4apis/activerecord.rb
52
+ - lib/neo4apis/cli/activerecord.rb
53
+ - neo4apis-activerecord.gemspec
54
+ homepage: https://github.com/neo4jrb/neo4apis-activerecord/
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 1.9.1
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.4.3
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: An ruby gem to import SQL data to neo4j using activerecord
78
+ test_files: []