datashift 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -5
- data/Gemfile +28 -25
- data/LICENSE.txt +26 -26
- data/README.markdown +302 -285
- data/README.rdoc +19 -19
- data/Rakefile +93 -95
- data/VERSION +5 -5
- data/datashift.gemspec +162 -178
- data/lib/applications/jruby/jexcel_file.rb +396 -396
- data/lib/applications/jruby/word.rb +79 -79
- data/lib/datashift.rb +152 -113
- 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 +236 -236
- data/lib/datashift/method_mapper.rb +256 -256
- data/lib/generators/csv_generator.rb +36 -36
- data/lib/generators/excel_generator.rb +121 -121
- data/lib/generators/generator_base.rb +13 -13
- data/lib/helpers/core_ext/to_b.rb +24 -24
- data/lib/helpers/spree_helper.rb +131 -131
- 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 +154 -149
- data/lib/loaders/loader_base.rb +403 -331
- data/lib/loaders/spreadsheet_loader.rb +136 -136
- data/lib/loaders/spree/image_loader.rb +45 -45
- data/lib/loaders/spree/product_loader.rb +224 -224
- 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_generator_spec.rb +78 -78
- data/spec/excel_loader_spec.rb +204 -176
- data/spec/file_definitions.rb +141 -141
- data/spec/fixtures/.~lock.ProjectsSingleCategories.xls# +1 -0
- data/spec/fixtures/ProjectsDefaults.yml +29 -0
- data/spec/fixtures/config/database.yml +24 -24
- data/spec/fixtures/interact_models_db.sqlite +0 -0
- data/spec/fixtures/interact_spree_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/SpreeProductsMultiColumn.csv +4 -4
- data/spec/fixtures/spree/SpreeProductsSimple.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_mapper_spec.rb +237 -237
- data/spec/spec_helper.rb +115 -115
- data/spec/spree_generator_spec.rb +64 -64
- data/spec/spree_loader_spec.rb +310 -310
- data/spec/spree_method_mapping_spec.rb +214 -214
- data/tasks/config/seed_fu_product_template.erb +15 -15
- data/tasks/config/tidy_config.txt +12 -12
- data/tasks/db_tasks.rake +65 -64
- data/tasks/excel_generator.rake +78 -78
- data/tasks/file_tasks.rake +36 -36
- data/tasks/import/csv.rake +49 -49
- data/tasks/import/excel.rake +71 -66
- 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 +7 -38
- data/Gemfile.lock +0 -211
- data/bin/autospec +0 -16
- data/bin/convert_to_should_syntax +0 -16
- data/bin/erubis +0 -16
- data/bin/htmldiff +0 -16
- data/bin/jeweler +0 -16
- data/bin/ldiff +0 -16
- data/bin/nokogiri +0 -16
- data/bin/rackup +0 -16
- data/bin/rails +0 -16
- data/bin/rake +0 -16
- data/bin/rake2thor +0 -16
- data/bin/ri +0 -16
- data/bin/rspec +0 -16
- data/bin/spree +0 -16
- data/bin/thor +0 -16
- data/bin/tilt +0 -16
- data/bin/tt +0 -16
@@ -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
|
data/tasks/db_tasks.rake
CHANGED
@@ -1,65 +1,66 @@
|
|
1
|
-
# Author :: Tom Statter
|
2
|
-
# Date :: Mar 2011
|
3
|
-
#
|
4
|
-
# License:: The MIT License (Free and OpenSource)
|
5
|
-
#
|
6
|
-
# About:: Additional Rake tasks useful when testing seeding DB via DataShift
|
7
|
-
#
|
8
|
-
namespace :autotelik do
|
9
|
-
|
10
|
-
namespace :db do
|
11
|
-
|
12
|
-
SYSTEM_TABLE_EXCLUSION_LIST = ['schema_migrations']
|
13
|
-
|
14
|
-
desc "Purge the current database"
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
ActiveRecord::Base.
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
`osql -E -S #{config["host"]} -d #{config["database"]} -i db\\#{
|
38
|
-
|
39
|
-
|
40
|
-
ActiveRecord::Base.
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
ActiveRecord::Base.
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
1
|
+
# Author :: Tom Statter
|
2
|
+
# Date :: Mar 2011
|
3
|
+
#
|
4
|
+
# License:: The MIT License (Free and OpenSource)
|
5
|
+
#
|
6
|
+
# About:: Additional Rake tasks useful when testing seeding DB via DataShift
|
7
|
+
#
|
8
|
+
namespace :autotelik do
|
9
|
+
|
10
|
+
namespace :db do
|
11
|
+
|
12
|
+
SYSTEM_TABLE_EXCLUSION_LIST = ['schema_migrations']
|
13
|
+
|
14
|
+
desc "Purge the current database"
|
15
|
+
|
16
|
+
task :purge, [:exclude_system_tables] => [:environment] do |t, args|
|
17
|
+
require 'highline/import'
|
18
|
+
|
19
|
+
if(RAILS_ENV == 'production')
|
20
|
+
agree("WARNING: In Production database, REALLY PURGE ? [y]:")
|
21
|
+
end
|
22
|
+
|
23
|
+
config = ActiveRecord::Base.configurations[RAILS_ENV || 'development']
|
24
|
+
case config['adapter']
|
25
|
+
when "mysql", "jdbcmysql"
|
26
|
+
ActiveRecord::Base.establish_connection(config)
|
27
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
28
|
+
next if(args[:exclude_system_tables] && SYSTEM_TABLE_EXCLUSION_LIST.include?(table) )
|
29
|
+
puts "purging table: #{table}"
|
30
|
+
ActiveRecord::Base.connection.execute("TRUNCATE #{table}")
|
31
|
+
end
|
32
|
+
when "sqlite","sqlite3"
|
33
|
+
dbfile = config["database"] || config["dbfile"]
|
34
|
+
File.delete(dbfile) if File.exist?(dbfile)
|
35
|
+
when "sqlserver"
|
36
|
+
dropfkscript = "#{config["host"]}.#{config["database"]}.DP1".gsub(/\\/,'-')
|
37
|
+
`osql -E -S #{config["host"]} -d #{config["database"]} -i db\\#{dropfkscript}`
|
38
|
+
`osql -E -S #{config["host"]} -d #{config["database"]} -i db\\#{RAILS_ENV}_structure.sql`
|
39
|
+
when "oci", "oracle"
|
40
|
+
ActiveRecord::Base.establish_connection(config)
|
41
|
+
ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|
|
42
|
+
ActiveRecord::Base.connection.execute(ddl)
|
43
|
+
end
|
44
|
+
when "firebird"
|
45
|
+
ActiveRecord::Base.establish_connection(config)
|
46
|
+
ActiveRecord::Base.connection.recreate_database!
|
47
|
+
else
|
48
|
+
raise "Task not supported by '#{config["adapter"]}'"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
desc "Clear database and optional directories such as assets, then run db:seed"
|
53
|
+
task :seed_again, [:assets] => [:environment] do |t, args|
|
54
|
+
|
55
|
+
Rake::Task['autotelik:db:purge'].invoke( true ) # i.e ENV['exclude_system_tables'] = true
|
56
|
+
|
57
|
+
if(args[:assets])
|
58
|
+
assets = "#{Rails.root}/public/assets"
|
59
|
+
FileUtils::rm_rf(assets) if(File.exists?(assets))
|
60
|
+
end
|
61
|
+
|
62
|
+
Rake::Task['db:seed'].invoke
|
63
|
+
end
|
64
|
+
|
65
|
+
end # db
|
65
66
|
end # autotelik
|
data/tasks/excel_generator.rake
CHANGED
@@ -1,79 +1,79 @@
|
|
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
|
-
namespace :generate do
|
22
|
-
|
23
|
-
desc "Generate a template .xls (Excel) file for a model"
|
24
|
-
|
25
|
-
task :excel, [:model, :result] => [:environment] do |t, args|
|
26
|
-
|
27
|
-
require 'excel_generator'
|
28
|
-
|
29
|
-
# in familiar ruby style args seems to have been become empty using this new style for rake 0.9.2
|
30
|
-
# whatever format i try, on both Win and OSX .. so had to revert back to ENV
|
31
|
-
model = args[:model] || ENV['model']
|
32
|
-
result = args[:result] || ENV['result']
|
33
|
-
|
34
|
-
raise "USAGE: jruby -S rake datashift:generate:excel model=<Class> result=<file.xls>" unless(result)
|
35
|
-
raise "ERROR: Cannot process without AR Model - please supply model=<Class>" unless(model)
|
36
|
-
|
37
|
-
begin
|
38
|
-
klass = 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
|
-
gen = DataShift::ExcelGenerator.new(result)
|
44
|
-
|
45
|
-
gen.generate(klass)
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
namespace :export do
|
51
|
-
|
52
|
-
desc "Export active record data to .xls (Excel) file"
|
53
|
-
|
54
|
-
task :excel, [:model, :result] => [:environment] do |t, args|
|
55
|
-
|
56
|
-
require 'excel_generator'
|
57
|
-
|
58
|
-
# in familiar ruby style args seems to have been become empty using this new style for rake 0.9.2
|
59
|
-
# whatever format i try, on both Win and OSX .. so had to revert back to ENV
|
60
|
-
model = ENV['model']
|
61
|
-
result = ENV['result']
|
62
|
-
|
63
|
-
raise "USAGE: jruby -S rake datashift:gen:excel model=<Class> result=<file.xls>" unless(result)
|
64
|
-
raise "ERROR: Cannot process without AR Model - please supply model=<Class>" unless(model)
|
65
|
-
|
66
|
-
begin
|
67
|
-
klass = Kernel.const_get(model)
|
68
|
-
rescue NameError
|
69
|
-
raise "ERROR: No such AR Model found - check valid model supplied via model=<Class>"
|
70
|
-
end
|
71
|
-
|
72
|
-
gen = DataShift::ExcelGenerator.new(result)
|
73
|
-
|
74
|
-
gen.export(klass.all)
|
75
|
-
end
|
76
|
-
|
77
|
-
end
|
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
|
+
namespace :generate do
|
22
|
+
|
23
|
+
desc "Generate a template .xls (Excel) file for a model"
|
24
|
+
|
25
|
+
task :excel, [:model, :result] => [:environment] do |t, args|
|
26
|
+
|
27
|
+
require 'excel_generator'
|
28
|
+
|
29
|
+
# in familiar ruby style args seems to have been become empty using this new style for rake 0.9.2
|
30
|
+
# whatever format i try, on both Win and OSX .. so had to revert back to ENV
|
31
|
+
model = args[:model] || ENV['model']
|
32
|
+
result = args[:result] || ENV['result']
|
33
|
+
|
34
|
+
raise "USAGE: jruby -S rake datashift:generate:excel model=<Class> result=<file.xls>" unless(result)
|
35
|
+
raise "ERROR: Cannot process without AR Model - please supply model=<Class>" unless(model)
|
36
|
+
|
37
|
+
begin
|
38
|
+
klass = 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
|
+
gen = DataShift::ExcelGenerator.new(result)
|
44
|
+
|
45
|
+
gen.generate(klass)
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
namespace :export do
|
51
|
+
|
52
|
+
desc "Export active record data to .xls (Excel) file"
|
53
|
+
|
54
|
+
task :excel, [:model, :result] => [:environment] do |t, args|
|
55
|
+
|
56
|
+
require 'excel_generator'
|
57
|
+
|
58
|
+
# in familiar ruby style args seems to have been become empty using this new style for rake 0.9.2
|
59
|
+
# whatever format i try, on both Win and OSX .. so had to revert back to ENV
|
60
|
+
model = ENV['model']
|
61
|
+
result = ENV['result']
|
62
|
+
|
63
|
+
raise "USAGE: jruby -S rake datashift:gen:excel model=<Class> result=<file.xls>" unless(result)
|
64
|
+
raise "ERROR: Cannot process without AR Model - please supply model=<Class>" unless(model)
|
65
|
+
|
66
|
+
begin
|
67
|
+
klass = Kernel.const_get(model)
|
68
|
+
rescue NameError
|
69
|
+
raise "ERROR: No such AR Model found - check valid model supplied via model=<Class>"
|
70
|
+
end
|
71
|
+
|
72
|
+
gen = DataShift::ExcelGenerator.new(result)
|
73
|
+
|
74
|
+
gen.export(klass.all)
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
79
|
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,50 @@
|
|
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
|
-
klass = Kernel.const_get(model)
|
38
|
-
rescue NameError
|
39
|
-
raise "ERROR: No such AR Model found - check valid model supplied via model=<Class>"
|
40
|
-
end
|
41
|
-
|
42
|
-
puts "INFO: Using CSV loader"
|
43
|
-
|
44
|
-
loader = DataShift::CsvLoader.new(klass)
|
45
|
-
|
46
|
-
loader.perform_load(input)
|
47
|
-
end
|
48
|
-
end
|
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
|
+
klass = Kernel.const_get(model)
|
38
|
+
rescue NameError
|
39
|
+
raise "ERROR: No such AR Model found - check valid model supplied via model=<Class>"
|
40
|
+
end
|
41
|
+
|
42
|
+
puts "INFO: Using CSV loader"
|
43
|
+
|
44
|
+
loader = DataShift::CsvLoader.new(klass)
|
45
|
+
|
46
|
+
loader.perform_load(input)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
50
|
end
|