datashift 0.0.1

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.
Files changed (108) hide show
  1. data/.document +5 -0
  2. data/Gemfile +25 -0
  3. data/Gemfile.lock +211 -0
  4. data/LICENSE.txt +27 -0
  5. data/README.markdown +286 -0
  6. data/README.rdoc +19 -0
  7. data/Rakefile +96 -0
  8. data/VERSION +5 -0
  9. data/bin/autospec +16 -0
  10. data/bin/convert_to_should_syntax +16 -0
  11. data/bin/erubis +16 -0
  12. data/bin/htmldiff +16 -0
  13. data/bin/jeweler +16 -0
  14. data/bin/ldiff +16 -0
  15. data/bin/nokogiri +16 -0
  16. data/bin/rackup +16 -0
  17. data/bin/rails +16 -0
  18. data/bin/rake +16 -0
  19. data/bin/rake2thor +16 -0
  20. data/bin/ri +16 -0
  21. data/bin/rspec +16 -0
  22. data/bin/spree +16 -0
  23. data/bin/thor +16 -0
  24. data/bin/tilt +16 -0
  25. data/bin/tt +16 -0
  26. data/datashift.gemspec +178 -0
  27. data/lib/applications/jruby/jexcel_file.rb +397 -0
  28. data/lib/applications/jruby/word.rb +79 -0
  29. data/lib/datashift.rb +114 -0
  30. data/lib/datashift/exceptions.rb +12 -0
  31. data/lib/datashift/file_definitions.rb +353 -0
  32. data/lib/datashift/mapping_file_definitions.rb +88 -0
  33. data/lib/datashift/method_detail.rb +237 -0
  34. data/lib/datashift/method_mapper.rb +257 -0
  35. data/lib/generators/csv_generator.rb +36 -0
  36. data/lib/generators/excel_generator.rb +122 -0
  37. data/lib/generators/generator_base.rb +14 -0
  38. data/lib/helpers/core_ext/to_b.rb +24 -0
  39. data/lib/helpers/spree_helper.rb +131 -0
  40. data/lib/java/poi-3.7/._poi-3.7-20101029.jar5645100390082102460.tmp +0 -0
  41. data/lib/java/poi-3.7/LICENSE +507 -0
  42. data/lib/java/poi-3.7/NOTICE +21 -0
  43. data/lib/java/poi-3.7/RELEASE_NOTES.txt +115 -0
  44. data/lib/java/poi-3.7/lib/commons-logging-1.1.jar +0 -0
  45. data/lib/java/poi-3.7/lib/junit-3.8.1.jar +0 -0
  46. data/lib/java/poi-3.7/lib/log4j-1.2.13.jar +0 -0
  47. data/lib/java/poi-3.7/ooxml-lib/dom4j-1.6.1.jar +0 -0
  48. data/lib/java/poi-3.7/ooxml-lib/geronimo-stax-api_1.0_spec-1.0.jar +0 -0
  49. data/lib/java/poi-3.7/ooxml-lib/xmlbeans-2.3.0.jar +0 -0
  50. data/lib/java/poi-3.7/poi-3.7-20101029.jar +0 -0
  51. data/lib/java/poi-3.7/poi-examples-3.7-20101029.jar +0 -0
  52. data/lib/java/poi-3.7/poi-ooxml-3.7-20101029.jar +0 -0
  53. data/lib/java/poi-3.7/poi-ooxml-schemas-3.7-20101029.jar +0 -0
  54. data/lib/java/poi-3.7/poi-scratchpad-3.7-20101029.jar +0 -0
  55. data/lib/loaders/csv_loader.rb +99 -0
  56. data/lib/loaders/excel_loader.rb +150 -0
  57. data/lib/loaders/loader_base.rb +332 -0
  58. data/lib/loaders/spreadsheet_loader.rb +137 -0
  59. data/lib/loaders/spree/image_loader.rb +46 -0
  60. data/lib/loaders/spree/product_loader.rb +225 -0
  61. data/spec/csv_loader_spec.rb +31 -0
  62. data/spec/datashift_spec.rb +27 -0
  63. data/spec/db/migrate/20110803201325_create_test_bed.rb +85 -0
  64. data/spec/excel_generator_spec.rb +79 -0
  65. data/spec/excel_loader_spec.rb +177 -0
  66. data/spec/file_definitions.rb +141 -0
  67. data/spec/fixtures/BadAssociationName.xls +0 -0
  68. data/spec/fixtures/DemoNegativeTesting.xls +0 -0
  69. data/spec/fixtures/ProjectsMultiCategories.xls +0 -0
  70. data/spec/fixtures/ProjectsSingleCategories.xls +0 -0
  71. data/spec/fixtures/SimpleProjects.xls +0 -0
  72. data/spec/fixtures/config/database.yml +25 -0
  73. data/spec/fixtures/interact_models_db.sqlite +0 -0
  74. data/spec/fixtures/interact_spree_db.sqlite +0 -0
  75. data/spec/fixtures/negative/SpreeProdMiss1Mandatory.csv +4 -0
  76. data/spec/fixtures/negative/SpreeProdMiss1Mandatory.xls +0 -0
  77. data/spec/fixtures/negative/SpreeProdMissManyMandatory.csv +4 -0
  78. data/spec/fixtures/negative/SpreeProdMissManyMandatory.xls +0 -0
  79. data/spec/fixtures/simple_export_spec.xls +0 -0
  80. data/spec/fixtures/simple_template_spec.xls +0 -0
  81. data/spec/fixtures/spree/SpreeProducts.csv +4 -0
  82. data/spec/fixtures/spree/SpreeProducts.xls +0 -0
  83. data/spec/fixtures/spree/SpreeProductsMultiColumn.csv +4 -0
  84. data/spec/fixtures/spree/SpreeProductsMultiColumn.xls +0 -0
  85. data/spec/fixtures/spree/SpreeProductsSimple.csv +4 -0
  86. data/spec/fixtures/spree/SpreeProductsSimple.xls +0 -0
  87. data/spec/fixtures/spree/SpreeZoneExample.csv +5 -0
  88. data/spec/fixtures/spree/SpreeZoneExample.xls +0 -0
  89. data/spec/fixtures/test_model_defs.rb +57 -0
  90. data/spec/loader_spec.rb +121 -0
  91. data/spec/method_mapper_spec.rb +238 -0
  92. data/spec/spec_helper.rb +116 -0
  93. data/spec/spree_generator_spec.rb +65 -0
  94. data/spec/spree_loader_spec.rb +311 -0
  95. data/spec/spree_method_mapping_spec.rb +215 -0
  96. data/tasks/config/seed_fu_product_template.erb +15 -0
  97. data/tasks/config/tidy_config.txt +13 -0
  98. data/tasks/db_tasks.rake +65 -0
  99. data/tasks/excel_generator.rake +79 -0
  100. data/tasks/file_tasks.rake +37 -0
  101. data/tasks/import/csv.rake +50 -0
  102. data/tasks/import/excel.rake +67 -0
  103. data/tasks/spree/image_load.rake +109 -0
  104. data/tasks/spree/product_loader.rake +44 -0
  105. data/tasks/word_to_seedfu.rake +167 -0
  106. data/test/helper.rb +18 -0
  107. data/test/test_interact.rb +7 -0
  108. metadata +301 -0
@@ -0,0 +1,215 @@
1
+ # Copyright:: (c) Autotelik Media Ltd 2011
2
+ # Author :: Tom Statter
3
+ # Date :: Summer 2011
4
+ #
5
+ # License:: MIT - Free, OpenSource
6
+ #
7
+ # Details:: Specification for Spree aspect of datashift gem.
8
+ #
9
+ # Tests the method mapping aspect, such as that we correctly identify
10
+ # Spree Product attributes and associations
11
+ #
12
+ require File.dirname(__FILE__) + '/spec_helper'
13
+
14
+ require 'spree_helper'
15
+ require 'product_loader'
16
+
17
+ include DataShift
18
+
19
+ describe 'SpreeLoader' do
20
+
21
+ before(:all) do
22
+
23
+ # we are not a Spree project, nor is it practical to externally generate
24
+ # a complete Spree application for testing so we implement a mini migrate/boot of our own
25
+ Spree.load() # require Spree gems
26
+
27
+ # key to YAML db e.g test_memory, test_mysql
28
+ db_connect( 'test_spree_standalone' )
29
+
30
+ Spree.boot # create a sort-of Spree app
31
+
32
+ Spree.migrate_up # create an sqlite Spree database on the fly
33
+
34
+ @klazz = Product
35
+
36
+ # Reset main tables - TODO should really purge properly, or roll back a transaction
37
+ [OptionType, OptionValue, Product, Property, Variant, Taxonomy, Taxon, Zone].each { |x| x.delete_all }
38
+ end
39
+
40
+ before(:each) do
41
+ MethodMapper.clear
42
+ MethodMapper.find_operators( @klazz )
43
+ end
44
+
45
+
46
+ it "should populate operators for a Spree Product" do
47
+
48
+ MethodMapper.has_many.should_not be_empty
49
+ MethodMapper.belongs_to.should_not be_empty
50
+ MethodMapper.assignments.should_not be_empty
51
+
52
+ assign = MethodMapper.assignments_for(@klazz)
53
+
54
+ assign.should include('available_on') # Example of a simple column
55
+
56
+ MethodMapper.assignments[@klazz].should include('available_on')
57
+
58
+ has_many_ops = MethodMapper.has_many_for(@klazz)
59
+
60
+ has_many_ops.should include('properties') # Product can have many properties
61
+
62
+ MethodMapper.has_many[@klazz].should include('properties')
63
+
64
+ btf = MethodMapper.belongs_to_for(@klazz)
65
+
66
+ btf.should include('tax_category') # Example of a belongs_to assignment
67
+
68
+ MethodMapper.belongs_to[@klazz].should include('tax_category')
69
+
70
+ MethodMapper.column_types[@klazz].size.should == @klazz.columns.size
71
+ end
72
+
73
+
74
+ it "should find method details correctly for different forms of a column name" do
75
+
76
+ ["available On", 'available_on', "Available On", "AVAILABLE_ON"].each do |format|
77
+
78
+ method_details = MethodMapper.find_method_detail( @klazz, format )
79
+
80
+ method_details.operator.should == 'available_on'
81
+ method_details.operator_for(:assignment).should == 'available_on'
82
+
83
+ method_details.operator_for(:belongs_to).should be_nil
84
+ method_details.operator_for(:has_many).should be_nil
85
+
86
+ method_details.col_type.should_not be_nil
87
+ method_details.col_type.name.should == 'available_on'
88
+ method_details.col_type.default.should == nil
89
+ method_details.col_type.sql_type.should include 'datetime' # works on mysql and sqlite
90
+ method_details.col_type.type.should == :datetime
91
+ end
92
+ end
93
+
94
+ it "should populate method details correctly for has_many forms of association name" do
95
+
96
+ MethodMapper.has_many[@klazz].should include('product_option_types')
97
+
98
+ ["product_option_types", "product option types", 'product Option_types', "ProductOptionTypes", "Product_Option_Types"].each do |format|
99
+ method_detail = MethodMapper.find_method_detail( @klazz, format )
100
+
101
+ method_detail.should_not be_nil
102
+
103
+ method_detail.operator_for(:has_many).should eq('product_option_types')
104
+ method_detail.operator_for(:belongs_to).should be_nil
105
+ method_detail.operator_for(:assignment).should be_nil
106
+ end
107
+ end
108
+
109
+
110
+ it "should populate method details correctly for assignment operators (none columns on #{@klazz})" do
111
+
112
+ MethodMapper.find_operators( @klazz, :reload => true, :instance_methods => true )
113
+
114
+ # Example of delegates i.e. cost_price column on Variant, delegated to Variant by Product
115
+
116
+ MethodMapper.assignments[@klazz].should include('cost_price')
117
+ MethodMapper.assignments[@klazz].should include('sku')
118
+
119
+
120
+ count_on_hand = MethodMapper.find_method_detail( @klazz, 'count on hand' )
121
+ count_on_hand.should_not be_nil
122
+ count_on_hand.operator.should == 'count_on_hand'
123
+
124
+ method = MethodMapper.find_method_detail( @klazz, 'sku' )
125
+ method.should_not be_nil
126
+ method.operator.should == 'sku'
127
+ end
128
+
129
+ it "should enable assignment via operators for none columns on #{@klazz}" do
130
+
131
+ MethodMapper.find_operators( @klazz, :reload => true, :instance_methods => true )
132
+
133
+ klazz_object = @klazz.new
134
+
135
+ klazz_object.should be_new_record
136
+
137
+ # we can use method details to populate a new AR object, essentailly same as
138
+ # klazz_object.send( count_on_hand.operator, 2)
139
+ count_on_hand = MethodMapper.find_method_detail( @klazz, 'count on hand' )
140
+
141
+ count_on_hand.assign( klazz_object, 2 )
142
+ klazz_object.count_on_hand.should == 2
143
+
144
+ count_on_hand.assign( klazz_object, 5 )
145
+ klazz_object.count_on_hand.should == 5
146
+
147
+ method = MethodMapper.find_method_detail( @klazz, 'sku' )
148
+ method.should_not be_nil
149
+
150
+ method.operator.should == 'sku'
151
+
152
+ method.assign( klazz_object, 'TEST_SK 001')
153
+ klazz_object.sku.should == 'TEST_SK 001'
154
+
155
+ end
156
+
157
+ it "should enable assignment to has_many association on new object" do
158
+
159
+ method_detail = MethodMapper.find_method_detail( @klazz, 'taxons' )
160
+
161
+ method_detail.operator.should == 'taxons'
162
+
163
+ klazz_object = @klazz.new
164
+
165
+ klazz_object.taxons.size.should == 0
166
+
167
+ # NEW ASSOCIATION ASSIGNMENT
168
+
169
+ # assign via the send operator directly on load object
170
+ klazz_object.send( method_detail.operator ) << Taxon.new
171
+
172
+ klazz_object.taxons.size.should == 1
173
+
174
+ klazz_object.send( method_detail.operator ) << [Taxon.new, Taxon.new]
175
+ klazz_object.taxons.size.should == 3
176
+
177
+ # Use generic assignment on method detail - expect has_many to use << not =
178
+ method_detail.assign( klazz_object, Taxon.new )
179
+ klazz_object.taxons.size.should == 4
180
+
181
+ method_detail.assign( klazz_object, [Taxon.new, Taxon.new])
182
+ klazz_object.taxons.size.should == 6
183
+ end
184
+
185
+ it "should enable assignment to has_many association using existing objects" do
186
+
187
+ MethodMapper.find_operators( @klazz )
188
+
189
+ method_detail = MethodMapper.find_method_detail( @klazz, 'product_properties' )
190
+
191
+ method_detail.operator.should == 'product_properties'
192
+
193
+ klazz_object = @klazz.new
194
+
195
+ ProductProperty.new(:property => @prop1)
196
+
197
+ # NEW ASSOCIATION ASSIGNMENT
198
+ klazz_object.send( method_detail.operator ) << ProductProperty.new
199
+
200
+ klazz_object.product_properties.size.should == 1
201
+
202
+ klazz_object.send( method_detail.operator ) << [ProductProperty.new, ProductProperty.new]
203
+ klazz_object.product_properties.size.should == 3
204
+
205
+ # Use generic assignment on method detail - expect has_many to use << not =
206
+ method_detail.assign( klazz_object, ProductProperty.new(:property => @prop1) )
207
+ klazz_object.product_properties.size.should == 4
208
+
209
+ method_detail.assign( klazz_object, [ProductProperty.new(:property => @prop2), ProductProperty.new(:property => @prop3)])
210
+ klazz_object.product_properties.size.should == 6
211
+
212
+ end
213
+
214
+
215
+ end
@@ -0,0 +1,15 @@
1
+ p = Product.seed( :name ) do |s|
2
+ s.name = <%= @name %>
3
+ s.available_on = '2009-09-01 09:00:00.0'
4
+ s.meta_keywords = ['training', 'training']
5
+ s.meta_description = ""
6
+ s.description = '<%= @description %>'
7
+ s.price = 0.00
8
+ s.sku = '<%= @sku %>'
9
+
10
+ s.is_physical = false
11
+ s.is_private = false
12
+
13
+ s.append_association :taxons, Taxon.find_by_name( 'Training' )
14
+
15
+ end
@@ -0,0 +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
+
13
+ new-empty-tags: o:smarttagtype, st1:placename, st1:place, st1:placetype
@@ -0,0 +1,65 @@
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
+ task :purge, :exclude_system_tables, :needs => [:environment] do |t, args|
16
+ require 'highline/import'
17
+
18
+ if(RAILS_ENV == 'production')
19
+ agree("WARNING: In Production database, REALLY PURGE ? [y]:")
20
+ end
21
+
22
+ config = ActiveRecord::Base.configurations[RAILS_ENV || 'development']
23
+ case config['adapter']
24
+ when "mysql", "jdbcmysql"
25
+ ActiveRecord::Base.establish_connection(config)
26
+ ActiveRecord::Base.connection.tables.each do |table|
27
+ next if(args[:exclude_system_tables] && SYSTEM_TABLE_EXCLUSION_LIST.include?(table) )
28
+ puts "purging table: #{table}"
29
+ ActiveRecord::Base.connection.execute("TRUNCATE #{table}")
30
+ end
31
+ when "sqlite","sqlite3"
32
+ dbfile = config["database"] || config["dbfile"]
33
+ File.delete(dbfile) if File.exist?(dbfile)
34
+ when "sqlserver"
35
+ dropfkscript = "#{config["host"]}.#{config["database"]}.DP1".gsub(/\\/,'-')
36
+ `osql -E -S #{config["host"]} -d #{config["database"]} -i db\\#{dropfkscript}`
37
+ `osql -E -S #{config["host"]} -d #{config["database"]} -i db\\#{RAILS_ENV}_structure.sql`
38
+ when "oci", "oracle"
39
+ ActiveRecord::Base.establish_connection(config)
40
+ ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|
41
+ ActiveRecord::Base.connection.execute(ddl)
42
+ end
43
+ when "firebird"
44
+ ActiveRecord::Base.establish_connection(config)
45
+ ActiveRecord::Base.connection.recreate_database!
46
+ else
47
+ raise "Task not supported by '#{config["adapter"]}'"
48
+ end
49
+ end
50
+
51
+ desc "Clear database and optional directories such as assets, then run db:seed"
52
+ task :seed_again, :assets, :needs => [:environment] do |t, args|
53
+
54
+ Rake::Task['autotelik:db:purge'].invoke( true ) # i.e ENV['exclude_system_tables'] = true
55
+
56
+ if(args[:assets])
57
+ assets = "#{Rails.root}/public/assets"
58
+ FileUtils::rm_rf(assets) if(File.exists?(assets))
59
+ end
60
+
61
+ Rake::Task['db:seed'].invoke
62
+ end
63
+
64
+ end # db
65
+ end # autotelik
@@ -0,0 +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
+
79
+ end
@@ -0,0 +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
+
37
+ end
@@ -0,0 +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
+
50
+ end