import_everything_rails 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/app/controllers/dataloads_controller.rb +9 -0
- data/app/models/dataload.rb +106 -4
- data/lib/app/models/dataload.rb +86 -0
- data/lib/import_everything_rails.rb +1 -1
- metadata +20 -9
data/app/models/dataload.rb
CHANGED
|
@@ -1,18 +1,110 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
class MyTempfile < Tempfile
|
|
2
|
+
attr_accessor :original_filename
|
|
3
|
+
end
|
|
4
|
+
|
|
5
|
+
class UrlFile
|
|
6
|
+
attr_accessor :url
|
|
7
|
+
include FromHash
|
|
8
|
+
def basename
|
|
9
|
+
url.split("/").last
|
|
10
|
+
end
|
|
11
|
+
def tempfile
|
|
12
|
+
require 'open-uri'
|
|
13
|
+
mylog "tempfile", :url => url, :last => basename
|
|
14
|
+
res = MyTempfile.new(basename)
|
|
15
|
+
res.original_filename = basename
|
|
16
|
+
res << (open(url) { |f| f.read })
|
|
17
|
+
res
|
|
18
|
+
end
|
|
19
|
+
def self.file(url)
|
|
20
|
+
new(:url => url).tempfile
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
class ReadProxy
|
|
25
|
+
attr_accessor :file
|
|
26
|
+
include FromHash
|
|
27
|
+
def method_missing(sym,*args,&b)
|
|
28
|
+
file.send(sym,*args,&b)
|
|
29
|
+
end
|
|
30
|
+
fattr(:read) { file.read }
|
|
31
|
+
def respond_to?(x)
|
|
32
|
+
file.respond_to?(x)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
require 'mongo_mapper'
|
|
37
|
+
require 'joint'
|
|
5
38
|
|
|
6
39
|
class Dataload
|
|
7
40
|
include MongoMapper::Document
|
|
8
41
|
plugin Joint
|
|
9
42
|
attachment :file
|
|
10
43
|
key :coll_name
|
|
44
|
+
key :url
|
|
45
|
+
key :data
|
|
46
|
+
key :ignore_duplicates, Boolean
|
|
11
47
|
belongs_to :workspace
|
|
48
|
+
before_create 'set_file_from_url!'
|
|
49
|
+
def text
|
|
50
|
+
file.name.present? ? wrapped_file.read : nil
|
|
51
|
+
end
|
|
52
|
+
def set_file_from_url!
|
|
53
|
+
if url.present?
|
|
54
|
+
self.file = UrlFile.file(url)
|
|
55
|
+
elsif data.present?
|
|
56
|
+
self.file = data_tempfile
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
def data_tempfile
|
|
60
|
+
f = Tempfile.new('gdfgfhfghfg.sql')
|
|
61
|
+
f << data
|
|
62
|
+
f
|
|
63
|
+
end
|
|
64
|
+
def myfilename
|
|
65
|
+
file.name
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
class Dataload
|
|
70
|
+
key :rows_added
|
|
71
|
+
fattr(:wrapped_file) { ReadProxy.new(:file => file) }
|
|
72
|
+
def duplicate?(c,row)
|
|
73
|
+
res = c.find_one(row)
|
|
74
|
+
mylog "duplicate", :res => res
|
|
75
|
+
!!res
|
|
76
|
+
end
|
|
77
|
+
def should_ignore?(c,row)
|
|
78
|
+
ignore_duplicates && duplicate?(c,row)
|
|
79
|
+
end
|
|
80
|
+
def run!
|
|
81
|
+
self.rows_added ||= 0
|
|
82
|
+
ImportEverything.each_table_and_rows(:file => wrapped_file) do |table,rows|
|
|
83
|
+
c = workspace.get_or_create_coll(table.to_s)
|
|
84
|
+
rows.each do |row|
|
|
85
|
+
unless should_ignore?(c,row)
|
|
86
|
+
c.save(row)
|
|
87
|
+
self.rows_added += 1
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
c.invalidate_cache!
|
|
91
|
+
end
|
|
92
|
+
save!
|
|
93
|
+
end
|
|
94
|
+
fattr(:preview) { ImportEverything.preview(import_ops) }
|
|
95
|
+
def import_ops
|
|
96
|
+
{:file => wrapped_file}.merge(addl_ops)
|
|
97
|
+
end
|
|
12
98
|
end
|
|
13
99
|
|
|
14
100
|
class Dataload
|
|
15
101
|
key :addl_ops, Hash
|
|
102
|
+
def addl_ops_hash
|
|
103
|
+
res = addl_ops.clone
|
|
104
|
+
preview.addl_required_fields.each { |x| res[x.to_s] ||= '' }
|
|
105
|
+
mylog "dataload", :addl_ops => addl_ops, :res => res
|
|
106
|
+
res
|
|
107
|
+
end
|
|
16
108
|
def method_missing(sym,*args,&b)
|
|
17
109
|
if sym.to_s[-1..-1] == '='
|
|
18
110
|
self.addl_ops[sym.to_s[0..-2]] = args.first
|
|
@@ -22,6 +114,16 @@ class Dataload
|
|
|
22
114
|
end
|
|
23
115
|
end
|
|
24
116
|
|
|
117
|
+
class Dataload
|
|
118
|
+
def self.find(*args)
|
|
119
|
+
if args.first == :all
|
|
120
|
+
all(*args[1..-1])
|
|
121
|
+
else
|
|
122
|
+
super
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
25
127
|
# class Dataload
|
|
26
128
|
# attr_accessor :coll, :data, :url, :file, :workspace, :actual
|
|
27
129
|
# include FromHash
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
raise 'foo'
|
|
2
|
+
# class MockFile
|
|
3
|
+
# attr_accessor :filename, :read
|
|
4
|
+
# include FromHash
|
|
5
|
+
# end
|
|
6
|
+
|
|
7
|
+
# class Dataload
|
|
8
|
+
# include MongoMapper::Document
|
|
9
|
+
# plugin Joint
|
|
10
|
+
# attachment :file
|
|
11
|
+
# key :coll_name
|
|
12
|
+
# belongs_to :workspace
|
|
13
|
+
# end
|
|
14
|
+
#
|
|
15
|
+
# class Dataload
|
|
16
|
+
# key :addl_ops, Hash
|
|
17
|
+
# def method_missing(sym,*args,&b)
|
|
18
|
+
# if sym.to_s[-1..-1] == '='
|
|
19
|
+
# self.addl_ops[sym.to_s[0..-2]] = args.first
|
|
20
|
+
# else
|
|
21
|
+
# super
|
|
22
|
+
# end
|
|
23
|
+
# end
|
|
24
|
+
# end
|
|
25
|
+
|
|
26
|
+
# class Dataload
|
|
27
|
+
# attr_accessor :coll, :data, :url, :file, :workspace, :actual
|
|
28
|
+
# include FromHash
|
|
29
|
+
# fattr(:addl_ops) { {} }
|
|
30
|
+
# def import_ops
|
|
31
|
+
# {:file => get_file}.merge(addl_ops)
|
|
32
|
+
# end
|
|
33
|
+
# def save!
|
|
34
|
+
# ImportEverything.each_table_and_rows(:file => get_file) do |table,rows|
|
|
35
|
+
# c = workspace.get_or_create_coll(table.to_s)
|
|
36
|
+
# rows.each { |row| c.save(row) }
|
|
37
|
+
# c.invalidate_cache!
|
|
38
|
+
# end
|
|
39
|
+
# end
|
|
40
|
+
# fattr(:get_file) do
|
|
41
|
+
# if url.present?
|
|
42
|
+
# require 'open-uri'
|
|
43
|
+
# MockFile.new(:filename => url, :read => open(url, 'rb') { |f| f.read })
|
|
44
|
+
# elsif file.present?
|
|
45
|
+
# file
|
|
46
|
+
# else
|
|
47
|
+
# data
|
|
48
|
+
# end
|
|
49
|
+
# end
|
|
50
|
+
# fattr(:preview) { ImportEverything.preview(import_ops) }
|
|
51
|
+
# def to_json
|
|
52
|
+
# #save! if preview.ready?
|
|
53
|
+
# mylog "dataload", :ready => preview.ready?, :actual => actual
|
|
54
|
+
# if preview.ready?
|
|
55
|
+
# if actual
|
|
56
|
+
# save!
|
|
57
|
+
# {:ran => true}
|
|
58
|
+
# else
|
|
59
|
+
# mylog 'dataload', :stuff => 'ready not actual'
|
|
60
|
+
# res = {:addl_required_fields => preview.addl_required_fields, :result => true, :tables => preview.tables.map { |x| x.to_hash }}
|
|
61
|
+
# mylog 'dataload', :stuff => 'ready not actual 2'
|
|
62
|
+
# res
|
|
63
|
+
# end
|
|
64
|
+
# else
|
|
65
|
+
# {:addl_required_fields => preview.addl_required_fields, :result => false}
|
|
66
|
+
# end
|
|
67
|
+
# end
|
|
68
|
+
# def method_missing(sym,*args,&b)
|
|
69
|
+
# if sym.to_s[-1..-1] == '='
|
|
70
|
+
# self.addl_ops[sym.to_s[0..-2]] = args.first
|
|
71
|
+
# else
|
|
72
|
+
# super
|
|
73
|
+
# end
|
|
74
|
+
# end
|
|
75
|
+
# end
|
|
76
|
+
#
|
|
77
|
+
#
|
|
78
|
+
# class Dataload
|
|
79
|
+
# extend ActiveModel::Naming
|
|
80
|
+
# def to_key
|
|
81
|
+
# nil
|
|
82
|
+
# end
|
|
83
|
+
# def persisted?
|
|
84
|
+
# false
|
|
85
|
+
# end
|
|
86
|
+
# end
|
metadata
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: import_everything_rails
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
|
|
4
|
+
prerelease: false
|
|
5
|
+
segments:
|
|
6
|
+
- 0
|
|
7
|
+
- 0
|
|
8
|
+
- 2
|
|
9
|
+
version: 0.0.2
|
|
5
10
|
platform: ruby
|
|
6
11
|
authors:
|
|
7
12
|
- Mike Harris
|
|
@@ -9,19 +14,21 @@ autorequire:
|
|
|
9
14
|
bindir: bin
|
|
10
15
|
cert_chain: []
|
|
11
16
|
|
|
12
|
-
date: 2010-05-
|
|
17
|
+
date: 2010-05-18 00:00:00 -04:00
|
|
13
18
|
default_executable:
|
|
14
19
|
dependencies:
|
|
15
20
|
- !ruby/object:Gem::Dependency
|
|
16
21
|
name: rspec
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
prerelease: false
|
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
|
20
24
|
requirements:
|
|
21
25
|
- - ">="
|
|
22
26
|
- !ruby/object:Gem::Version
|
|
27
|
+
segments:
|
|
28
|
+
- 0
|
|
23
29
|
version: "0"
|
|
24
|
-
|
|
30
|
+
type: :development
|
|
31
|
+
version_requirements: *id001
|
|
25
32
|
description: import_everything_rails
|
|
26
33
|
email: mharris717@gmail.com
|
|
27
34
|
executables: []
|
|
@@ -32,7 +39,9 @@ extra_rdoc_files:
|
|
|
32
39
|
- LICENSE
|
|
33
40
|
- README.rdoc
|
|
34
41
|
files:
|
|
42
|
+
- app/controllers/dataloads_controller.rb
|
|
35
43
|
- app/models/dataload.rb
|
|
44
|
+
- lib/app/models/dataload.rb
|
|
36
45
|
- lib/import_everything_rails.rb
|
|
37
46
|
- lib/import_everything_rails/engine.rb
|
|
38
47
|
- LICENSE
|
|
@@ -50,18 +59,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
50
59
|
requirements:
|
|
51
60
|
- - ">="
|
|
52
61
|
- !ruby/object:Gem::Version
|
|
62
|
+
segments:
|
|
63
|
+
- 0
|
|
53
64
|
version: "0"
|
|
54
|
-
version:
|
|
55
65
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
56
66
|
requirements:
|
|
57
67
|
- - ">="
|
|
58
68
|
- !ruby/object:Gem::Version
|
|
69
|
+
segments:
|
|
70
|
+
- 0
|
|
59
71
|
version: "0"
|
|
60
|
-
version:
|
|
61
72
|
requirements: []
|
|
62
73
|
|
|
63
74
|
rubyforge_project:
|
|
64
|
-
rubygems_version: 1.3.
|
|
75
|
+
rubygems_version: 1.3.6
|
|
65
76
|
signing_key:
|
|
66
77
|
specification_version: 3
|
|
67
78
|
summary: import_everything_rails
|