datashift 0.2.1 → 0.2.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/.document +5 -5
- data/LICENSE.txt +26 -26
- data/README.markdown +326 -305
- data/README.rdoc +19 -19
- data/Rakefile +86 -93
- data/VERSION +1 -1
- data/datashift.gemspec +163 -152
- data/lib/applications/jruby/jexcel_file.rb +410 -408
- data/lib/applications/jruby/word.rb +79 -79
- data/lib/datashift.rb +183 -152
- data/lib/datashift/exceptions.rb +11 -11
- data/lib/datashift/file_definitions.rb +353 -353
- data/lib/datashift/mapping_file_definitions.rb +87 -87
- data/lib/datashift/method_detail.rb +293 -275
- data/lib/datashift/method_dictionary.rb +208 -209
- data/lib/datashift/method_mapper.rb +90 -90
- data/lib/datashift/model_mapper.rb +27 -0
- data/lib/exporters/csv_exporter.rb +36 -0
- data/lib/exporters/excel_exporter.rb +116 -0
- data/lib/exporters/exporter_base.rb +15 -0
- data/lib/generators/csv_generator.rb +36 -36
- data/lib/generators/excel_generator.rb +106 -122
- data/lib/generators/generator_base.rb +13 -13
- data/lib/helpers/core_ext/to_b.rb +24 -24
- data/lib/helpers/rake_utils.rb +42 -0
- data/lib/helpers/spree_helper.rb +194 -153
- data/lib/java/poi-3.7/LICENSE +507 -507
- data/lib/java/poi-3.7/NOTICE +21 -21
- data/lib/java/poi-3.7/RELEASE_NOTES.txt +115 -115
- data/lib/loaders/csv_loader.rb +98 -98
- data/lib/loaders/excel_loader.rb +155 -155
- data/lib/loaders/loader_base.rb +420 -420
- data/lib/loaders/spreadsheet_loader.rb +136 -136
- data/lib/loaders/spree/image_loader.rb +67 -63
- data/lib/loaders/spree/product_loader.rb +289 -248
- data/lib/thor/generate_excel.thor +54 -0
- data/sandbox/app/controllers/application_controller.rb +3 -0
- data/sandbox/config/application.rb +43 -0
- data/sandbox/config/database.yml +34 -0
- data/sandbox/config/environment.rb +7 -0
- data/sandbox/config/environments/development.rb +30 -0
- data/spec/csv_loader_spec.rb +30 -30
- data/spec/datashift_spec.rb +26 -26
- data/spec/db/migrate/20110803201325_create_test_bed.rb +85 -85
- data/spec/excel_exporter_spec.rb +78 -78
- data/spec/excel_generator_spec.rb +78 -78
- data/spec/excel_loader_spec.rb +223 -223
- data/spec/file_definitions.rb +141 -141
- data/spec/fixtures/ProjectsDefaults.yml +29 -29
- data/spec/fixtures/config/database.yml +27 -27
- data/spec/fixtures/datashift_Spree_db.sqlite +0 -0
- data/spec/fixtures/datashift_test_models_db.sqlite +0 -0
- data/spec/fixtures/negative/SpreeProdMiss1Mandatory.csv +4 -4
- data/spec/fixtures/negative/SpreeProdMissManyMandatory.csv +4 -4
- data/spec/fixtures/spree/SpreeProducts.csv +4 -4
- data/spec/fixtures/spree/SpreeProducts.xls +0 -0
- data/spec/fixtures/spree/SpreeProductsMultiColumn.csv +4 -4
- data/spec/fixtures/spree/SpreeProductsMultiColumn.xls +0 -0
- data/spec/fixtures/spree/SpreeProductsSimple.csv +4 -4
- data/spec/fixtures/spree/SpreeProductsWithImages.csv +4 -4
- data/spec/fixtures/spree/SpreeZoneExample.csv +5 -5
- data/spec/fixtures/test_model_defs.rb +57 -57
- data/spec/loader_spec.rb +120 -120
- data/spec/method_dictionary_spec.rb +242 -242
- data/spec/method_mapper_spec.rb +41 -41
- data/spec/spec_helper.rb +154 -116
- data/spec/spree_exporter_spec.rb +67 -0
- data/spec/spree_generator_spec.rb +77 -64
- data/spec/spree_loader_spec.rb +363 -324
- data/spec/spree_method_mapping_spec.rb +218 -214
- data/tasks/config/seed_fu_product_template.erb +15 -15
- data/tasks/config/tidy_config.txt +12 -12
- data/tasks/{excel_generator.rake → export/excel_generator.rake} +101 -78
- data/tasks/file_tasks.rake +36 -36
- data/tasks/import/csv.rake +50 -49
- data/tasks/import/excel.rake +74 -71
- data/tasks/spree/image_load.rake +108 -108
- data/tasks/spree/product_loader.rake +43 -43
- data/tasks/word_to_seedfu.rake +166 -166
- data/test/helper.rb +18 -18
- data/test/test_interact.rb +7 -7
- metadata +16 -8
- data/datashift-0.1.0.gem +0 -0
- data/tasks/db_tasks.rake +0 -66
@@ -1,13 +1,13 @@
|
|
1
|
-
# Copyright:: (c) Autotelik Media Ltd 2011
|
2
|
-
# Author :: Tom Statter
|
3
|
-
# Date :: Aug 2010
|
4
|
-
#
|
5
|
-
# License:: Free, OpenSource... MIT ?
|
6
|
-
|
7
|
-
# This is the best effort I've found so far to reduce the amount of MS cruft
|
8
|
-
# to absolute minimum
|
9
|
-
# ... but unfortunately these tags will NOT BE REMOVED completely - manual cleanup still required
|
10
|
-
|
11
|
-
# TODO - add another ruby parse layer to strip these out completely
|
12
|
-
|
1
|
+
# Copyright:: (c) Autotelik Media Ltd 2011
|
2
|
+
# Author :: Tom Statter
|
3
|
+
# Date :: Aug 2010
|
4
|
+
#
|
5
|
+
# License:: Free, OpenSource... MIT ?
|
6
|
+
|
7
|
+
# This is the best effort I've found so far to reduce the amount of MS cruft
|
8
|
+
# to absolute minimum
|
9
|
+
# ... but unfortunately these tags will NOT BE REMOVED completely - manual cleanup still required
|
10
|
+
|
11
|
+
# TODO - add another ruby parse layer to strip these out completely
|
12
|
+
|
13
13
|
new-empty-tags: o:smarttagtype, st1:placename, st1:place, st1:placetype
|
@@ -1,79 +1,102 @@
|
|
1
|
-
# Copyright:: (c) Autotelik Media Ltd 2011
|
2
|
-
# Author :: Tom Statter
|
3
|
-
# Date :: Aug 2011
|
4
|
-
# License:: MIT.
|
5
|
-
#
|
6
|
-
# REQUIRES: JRuby
|
7
|
-
#
|
8
|
-
# Usage::
|
9
|
-
#
|
10
|
-
# In Rakefile:
|
11
|
-
#
|
12
|
-
# require 'datashift'
|
13
|
-
#
|
14
|
-
# DataShift::load_tasks
|
15
|
-
#
|
16
|
-
# Cmd Line:
|
17
|
-
#
|
18
|
-
# => jruby -S rake datashift:generate:excel model=<active record class> result=<output_template.xls>
|
19
|
-
#
|
20
|
-
namespace :datashift do
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
1
|
+
# Copyright:: (c) Autotelik Media Ltd 2011
|
2
|
+
# Author :: Tom Statter
|
3
|
+
# Date :: Aug 2011
|
4
|
+
# License:: MIT.
|
5
|
+
#
|
6
|
+
# REQUIRES: JRuby
|
7
|
+
#
|
8
|
+
# Usage::
|
9
|
+
#
|
10
|
+
# In Rakefile:
|
11
|
+
#
|
12
|
+
# require 'datashift'
|
13
|
+
#
|
14
|
+
# DataShift::load_tasks
|
15
|
+
#
|
16
|
+
# Cmd Line:
|
17
|
+
#
|
18
|
+
# => jruby -S rake datashift:generate:excel model=<active record class> result=<output_template.xls>
|
19
|
+
#
|
20
|
+
namespace :datashift do
|
21
|
+
|
22
|
+
namespace :generate do
|
23
|
+
|
24
|
+
include RakeUtils
|
25
|
+
|
26
|
+
desc "Generate a template .xls (Excel) file for a model"
|
27
|
+
|
28
|
+
task :excel, [:model, :result] => :environment do |t, args|
|
29
|
+
|
30
|
+
|
31
|
+
# in familiar ruby style args seems to have been become empty using this new style for rake 0.9.2
|
32
|
+
# whatever format i try, on both Win and OSX .. so had to revert back to ENV
|
33
|
+
model = args.model || ENV['model']
|
34
|
+
result = args.result || ENV['result']
|
35
|
+
|
36
|
+
RakeUtils::check_args(args, [:model, :result]) do
|
37
|
+
x =<<-EOS
|
38
|
+
USAGE::
|
39
|
+
|
40
|
+
rake datashift:generate:excel model=<Class> result=<file.xls>
|
41
|
+
|
42
|
+
Generate a template Excel file representing an Active Record mode.
|
43
|
+
Once populated with data the template can be used to import the data,
|
44
|
+
via the partner import tasks
|
45
|
+
Parameters :
|
46
|
+
[:model] - Mandatory - The database model to export.
|
47
|
+
[:results] - Mandatory - Name of the output template file..
|
48
|
+
EOS
|
49
|
+
puts x
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
require 'excel_generator'
|
54
|
+
|
55
|
+
raise "USAGE: jruby -S rake datashift:generate:excel model=<Class> result=<file.xls>" unless(result)
|
56
|
+
|
57
|
+
|
58
|
+
begin
|
59
|
+
# support modules e.g "Spree::Property")
|
60
|
+
klass = ModelMapper::class_from_string(model) #Kernel.const_get(model)
|
61
|
+
rescue NameError
|
62
|
+
raise "ERROR: No such AR Model found - check valid model supplied via model=<Class>"
|
63
|
+
end
|
64
|
+
|
65
|
+
gen = DataShift::ExcelGenerator.new(result)
|
66
|
+
|
67
|
+
gen.generate(klass)
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
namespace :export do
|
73
|
+
|
74
|
+
desc "Export active record data to .xls (Excel) file"
|
75
|
+
|
76
|
+
task :excel, [:model, :result] => [:environment] do |t, args|
|
77
|
+
|
78
|
+
require 'excel_generator'
|
79
|
+
|
80
|
+
# in familiar ruby style args seems to have been become empty using this new style for rake 0.9.2
|
81
|
+
# whatever format i try, on both Win and OSX .. so had to revert back to ENV
|
82
|
+
model = ENV['model']
|
83
|
+
result = ENV['result']
|
84
|
+
|
85
|
+
raise "USAGE: jruby -S rake datashift:gen:excel model=<Class> result=<file.xls>" unless(result)
|
86
|
+
raise "ERROR: Cannot process without AR Model - please supply model=<Class>" unless(model)
|
87
|
+
|
88
|
+
begin
|
89
|
+
# support modules e.g "Spree::Property")
|
90
|
+
klass = ModelMapper::class_from_string(model) #Kernel.const_get(model)
|
91
|
+
rescue NameError
|
92
|
+
raise "ERROR: No such AR Model found - check valid model supplied via model=<Class>"
|
93
|
+
end
|
94
|
+
|
95
|
+
gen = DataShift::ExcelGenerator.new(result)
|
96
|
+
|
97
|
+
gen.export(klass.all)
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
|
79
102
|
end
|
data/tasks/file_tasks.rake
CHANGED
@@ -1,37 +1,37 @@
|
|
1
|
-
# Copyright:: (c) Autotelik Media Ltd 2011
|
2
|
-
# Author :: Tom Statter
|
3
|
-
# Date :: Feb 2011
|
4
|
-
# License:: MIT
|
5
|
-
#
|
6
|
-
# Usage:: rake datashift:file_rename input=/blah image_load input=path_to_images
|
7
|
-
#
|
8
|
-
namespace :datashift do
|
9
|
-
|
10
|
-
desc "copy or mv a folder of files, consistently renaming in the process"
|
11
|
-
task :file_rename, :input, :offset, :prefix, :width, :commit, :mv do |t, args|
|
12
|
-
raise "USAGE: rake file_rename input='C:\blah' [offset=n prefix='str' width=n]" unless args[:input] && File.exists?(args[:input])
|
13
|
-
width = args[:width] || 2
|
14
|
-
|
15
|
-
action = args[:mv] ? 'mv' : 'cp'
|
16
|
-
|
17
|
-
cache = args[:input]
|
18
|
-
|
19
|
-
if(File.exists?(cache) )
|
20
|
-
puts "Renaming files from #{cache}"
|
21
|
-
Dir.glob(File.join(cache, "*")) do |name|
|
22
|
-
path, base_name = File.split(name)
|
23
|
-
id = base_name.slice!(/\w+/)
|
24
|
-
|
25
|
-
id = id.to_i + args[:offset].to_i if(args[:offset])
|
26
|
-
id = "%0#{width}d" % id.to_i if(args[:width])
|
27
|
-
id = args[:prefix] + id.to_s if(args[:prefix])
|
28
|
-
|
29
|
-
destination = File.join(path, "#{id}#{base_name}")
|
30
|
-
puts "ACTION: #{action} #{name} #{destination}"
|
31
|
-
|
32
|
-
File.send( action, name, destination) if args[:commit]
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
1
|
+
# Copyright:: (c) Autotelik Media Ltd 2011
|
2
|
+
# Author :: Tom Statter
|
3
|
+
# Date :: Feb 2011
|
4
|
+
# License:: MIT
|
5
|
+
#
|
6
|
+
# Usage:: rake datashift:file_rename input=/blah image_load input=path_to_images
|
7
|
+
#
|
8
|
+
namespace :datashift do
|
9
|
+
|
10
|
+
desc "copy or mv a folder of files, consistently renaming in the process"
|
11
|
+
task :file_rename, :input, :offset, :prefix, :width, :commit, :mv do |t, args|
|
12
|
+
raise "USAGE: rake file_rename input='C:\blah' [offset=n prefix='str' width=n]" unless args[:input] && File.exists?(args[:input])
|
13
|
+
width = args[:width] || 2
|
14
|
+
|
15
|
+
action = args[:mv] ? 'mv' : 'cp'
|
16
|
+
|
17
|
+
cache = args[:input]
|
18
|
+
|
19
|
+
if(File.exists?(cache) )
|
20
|
+
puts "Renaming files from #{cache}"
|
21
|
+
Dir.glob(File.join(cache, "*")) do |name|
|
22
|
+
path, base_name = File.split(name)
|
23
|
+
id = base_name.slice!(/\w+/)
|
24
|
+
|
25
|
+
id = id.to_i + args[:offset].to_i if(args[:offset])
|
26
|
+
id = "%0#{width}d" % id.to_i if(args[:width])
|
27
|
+
id = args[:prefix] + id.to_s if(args[:prefix])
|
28
|
+
|
29
|
+
destination = File.join(path, "#{id}#{base_name}")
|
30
|
+
puts "ACTION: #{action} #{name} #{destination}"
|
31
|
+
|
32
|
+
File.send( action, name, destination) if args[:commit]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
37
|
end
|
data/tasks/import/csv.rake
CHANGED
@@ -1,50 +1,51 @@
|
|
1
|
-
# Copyright:: (c) Autotelik Media Ltd 2011
|
2
|
-
# Author :: Tom Statter
|
3
|
-
# Date :: Aug 2011
|
4
|
-
# License:: MIT
|
5
|
-
#
|
6
|
-
# Usage::
|
7
|
-
#
|
8
|
-
# In Rakefile:
|
9
|
-
#
|
10
|
-
# require 'datashift'
|
11
|
-
#
|
12
|
-
# DataShift::load_tasks
|
13
|
-
#
|
14
|
-
# Cmd Line:
|
15
|
-
#
|
16
|
-
# => jruby -S rake datashift:import:csv model=<active record class> input=<file.csv>
|
17
|
-
#
|
18
|
-
namespace :datashift do
|
19
|
-
|
20
|
-
namespace :import do
|
21
|
-
|
22
|
-
desc "Populate a model's table in db with data from CSV file"
|
23
|
-
task :csv, [:model, :loader, :input, :verbose] => [:environment] do |t, args|
|
24
|
-
|
25
|
-
# in familiar ruby style args seems to have been become empty with rake 0.9.2 whatever i try
|
26
|
-
# so had to revert back to ENV
|
27
|
-
model = ENV['model']
|
28
|
-
input = ENV['input']
|
29
|
-
|
30
|
-
raise "USAGE: rake datashift:import:csv input=file.csv model=<Class>" unless(input)
|
31
|
-
raise "ERROR: Cannot process without AR Model - please supply model=<Class>" unless(model)
|
32
|
-
raise "ERROR: Could not find csv file #{args[:input]}" unless File.exists?(input)
|
33
|
-
|
34
|
-
require 'csv_loader'
|
35
|
-
|
36
|
-
begin
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
1
|
+
# Copyright:: (c) Autotelik Media Ltd 2011
|
2
|
+
# Author :: Tom Statter
|
3
|
+
# Date :: Aug 2011
|
4
|
+
# License:: MIT
|
5
|
+
#
|
6
|
+
# Usage::
|
7
|
+
#
|
8
|
+
# In Rakefile:
|
9
|
+
#
|
10
|
+
# require 'datashift'
|
11
|
+
#
|
12
|
+
# DataShift::load_tasks
|
13
|
+
#
|
14
|
+
# Cmd Line:
|
15
|
+
#
|
16
|
+
# => jruby -S rake datashift:import:csv model=<active record class> input=<file.csv>
|
17
|
+
#
|
18
|
+
namespace :datashift do
|
19
|
+
|
20
|
+
namespace :import do
|
21
|
+
|
22
|
+
desc "Populate a model's table in db with data from CSV file"
|
23
|
+
task :csv, [:model, :loader, :input, :verbose] => [:environment] do |t, args|
|
24
|
+
|
25
|
+
# in familiar ruby style args seems to have been become empty with rake 0.9.2 whatever i try
|
26
|
+
# so had to revert back to ENV
|
27
|
+
model = ENV['model']
|
28
|
+
input = ENV['input']
|
29
|
+
|
30
|
+
raise "USAGE: rake datashift:import:csv input=file.csv model=<Class>" unless(input)
|
31
|
+
raise "ERROR: Cannot process without AR Model - please supply model=<Class>" unless(model)
|
32
|
+
raise "ERROR: Could not find csv file #{args[:input]}" unless File.exists?(input)
|
33
|
+
|
34
|
+
require 'csv_loader'
|
35
|
+
|
36
|
+
begin
|
37
|
+
# support modules e.g "Spree::Property")
|
38
|
+
klass = ModelMapper::class_from_string(model) #Kernel.const_get(model)
|
39
|
+
rescue NameError
|
40
|
+
raise "ERROR: No such AR Model found - check valid model supplied via model=<Class>"
|
41
|
+
end
|
42
|
+
|
43
|
+
puts "INFO: Using CSV loader"
|
44
|
+
|
45
|
+
loader = DataShift::CsvLoader.new(klass)
|
46
|
+
|
47
|
+
loader.perform_load(input)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
50
51
|
end
|
data/tasks/import/excel.rake
CHANGED
@@ -1,72 +1,75 @@
|
|
1
|
-
# Copyright:: (c) Autotelik Media Ltd 2011
|
2
|
-
# Author :: Tom Statter
|
3
|
-
# Date :: Feb 2011
|
4
|
-
# License:: TBD. Free, Open Source. MIT ?
|
5
|
-
#
|
6
|
-
# REQUIRES: JRuby
|
7
|
-
#
|
8
|
-
# Usage::
|
9
|
-
#
|
10
|
-
# In Rakefile:
|
11
|
-
#
|
12
|
-
# require 'datashift'
|
13
|
-
#
|
14
|
-
# DataShift::load_tasks
|
15
|
-
#
|
16
|
-
# Cmd Line:
|
17
|
-
#
|
18
|
-
# => jruby -S rake datashift:import:excel model=<active record class> input=<file.xls>
|
19
|
-
# => jruby -S rake datashift:import:excel model=<active record class> input=C:\MyProducts.xlsverbose=true
|
20
|
-
#
|
21
|
-
require 'datashift'
|
22
|
-
require 'excel_loader'
|
23
|
-
|
24
|
-
namespace :datashift do
|
25
|
-
|
26
|
-
namespace :import do
|
27
|
-
|
28
|
-
desc "Populate model's table with data from .xls (Excel) file"
|
29
|
-
|
30
|
-
task :excel, [:model, :loader, :input, :config, :verbose] => [:environment] do |t, args|
|
31
|
-
|
32
|
-
# in familiar ruby style args seems to have been become empty using this new style for rake 0.9.2
|
33
|
-
# whatever format i try, on both Win and OSX .. so had to revert back to ENV
|
34
|
-
model = ENV['model']
|
35
|
-
input = ENV['input']
|
36
|
-
|
37
|
-
raise "USAGE: jruby -S rake datashift:import:excel input=excel_file.xls model=<Class>" unless(input)
|
38
|
-
raise "ERROR: Cannot process without AR Model - please supply model=<Class>" unless(model)
|
39
|
-
raise "ERROR: Could not find file #{args[:input]}" unless File.exists?(input)
|
40
|
-
|
41
|
-
begin
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
loader
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
1
|
+
# Copyright:: (c) Autotelik Media Ltd 2011
|
2
|
+
# Author :: Tom Statter
|
3
|
+
# Date :: Feb 2011
|
4
|
+
# License:: TBD. Free, Open Source. MIT ?
|
5
|
+
#
|
6
|
+
# REQUIRES: JRuby
|
7
|
+
#
|
8
|
+
# Usage::
|
9
|
+
#
|
10
|
+
# In Rakefile:
|
11
|
+
#
|
12
|
+
# require 'datashift'
|
13
|
+
#
|
14
|
+
# DataShift::load_tasks
|
15
|
+
#
|
16
|
+
# Cmd Line:
|
17
|
+
#
|
18
|
+
# => jruby -S rake datashift:import:excel model=<active record class> input=<file.xls>
|
19
|
+
# => jruby -S rake datashift:import:excel model=<active record class> input=C:\MyProducts.xlsverbose=true
|
20
|
+
#
|
21
|
+
require 'datashift'
|
22
|
+
require 'excel_loader'
|
23
|
+
|
24
|
+
namespace :datashift do
|
25
|
+
|
26
|
+
namespace :import do
|
27
|
+
|
28
|
+
desc "Populate model's table with data from .xls (Excel) file"
|
29
|
+
|
30
|
+
task :excel, [:model, :loader, :input, :config, :verbose] => [:environment] do |t, args|
|
31
|
+
|
32
|
+
# in familiar ruby style args seems to have been become empty using this new style for rake 0.9.2
|
33
|
+
# whatever format i try, on both Win and OSX .. so had to revert back to ENV
|
34
|
+
model = ENV['model']
|
35
|
+
input = ENV['input']
|
36
|
+
|
37
|
+
raise "USAGE: jruby -S rake datashift:import:excel input=excel_file.xls model=<Class>" unless(input)
|
38
|
+
raise "ERROR: Cannot process without AR Model - please supply model=<Class>" unless(model)
|
39
|
+
raise "ERROR: Could not find file #{args[:input]}" unless File.exists?(input)
|
40
|
+
|
41
|
+
begin
|
42
|
+
# support modules e.g "Spree::Property")
|
43
|
+
klass = ModelMapper::class_from_string(model) #Kernel.const_get(model)
|
44
|
+
rescue NameError
|
45
|
+
raise "ERROR: No such AR Model found - check valid model supplied via model=<Class>"
|
46
|
+
end
|
47
|
+
|
48
|
+
if(ENV['loader'])
|
49
|
+
begin
|
50
|
+
#loader_klass = Kernel.const_get(ENV['loader'])
|
51
|
+
# support modules e.g "Spree::Property")
|
52
|
+
loader_klass = ModelMapper::class_from_string(ENV['loader']) #Kernel.const_get(model)
|
53
|
+
|
54
|
+
loader = loader_klass.new(klass)
|
55
|
+
|
56
|
+
puts "INFO: Using loader : #{loader.class}"
|
57
|
+
rescue
|
58
|
+
puts "INFO: No specific #{model}Loader found - using generic ExcelLoader"
|
59
|
+
loader = DataShift::ExcelLoader.new(klass)
|
60
|
+
end
|
61
|
+
else
|
62
|
+
puts "INFO: No Loader specified - using generic ExcelLoader"
|
63
|
+
loader = DataShift::ExcelLoader.new(klass)
|
64
|
+
end
|
65
|
+
|
66
|
+
puts "ARGS #{args.inspect} #{args[:verbose]} [#{args.verbose}]"
|
67
|
+
loader.logger.verbose if(ENV['verbose'])
|
68
|
+
|
69
|
+
loader.configure_from( ENV['config'] ) if(ENV['config'])
|
70
|
+
|
71
|
+
loader.perform_load(input)
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
72
75
|
end
|