datashift 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- 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
data/lib/java/poi-3.7/NOTICE
CHANGED
@@ -1,21 +1,21 @@
|
|
1
|
-
Apache POI
|
2
|
-
Copyright 2009 The Apache Software Foundation
|
3
|
-
|
4
|
-
This product includes software developed by
|
5
|
-
The Apache Software Foundation (http://www.apache.org/).
|
6
|
-
|
7
|
-
This product contains the DOM4J library (http://www.dom4j.org).
|
8
|
-
Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
|
9
|
-
|
10
|
-
This product contains parts that were originally based on software from BEA.
|
11
|
-
Copyright (c) 2000-2003, BEA Systems, <http://www.bea.com/>.
|
12
|
-
|
13
|
-
This product contains W3C XML Schema documents. Copyright 2001-2003 (c)
|
14
|
-
World Wide Web Consortium (Massachusetts Institute of Technology, European
|
15
|
-
Research Consortium for Informatics and Mathematics, Keio University)
|
16
|
-
|
17
|
-
This product contains the Piccolo XML Parser for Java
|
18
|
-
(http://piccolo.sourceforge.net/). Copyright 2002 Yuval Oren.
|
19
|
-
|
20
|
-
This product contains the chunks_parse_cmds.tbl file from the vsdump program.
|
21
|
-
Copyright (C) 2006-2007 Valek Filippov (frob@df.ru)
|
1
|
+
Apache POI
|
2
|
+
Copyright 2009 The Apache Software Foundation
|
3
|
+
|
4
|
+
This product includes software developed by
|
5
|
+
The Apache Software Foundation (http://www.apache.org/).
|
6
|
+
|
7
|
+
This product contains the DOM4J library (http://www.dom4j.org).
|
8
|
+
Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
|
9
|
+
|
10
|
+
This product contains parts that were originally based on software from BEA.
|
11
|
+
Copyright (c) 2000-2003, BEA Systems, <http://www.bea.com/>.
|
12
|
+
|
13
|
+
This product contains W3C XML Schema documents. Copyright 2001-2003 (c)
|
14
|
+
World Wide Web Consortium (Massachusetts Institute of Technology, European
|
15
|
+
Research Consortium for Informatics and Mathematics, Keio University)
|
16
|
+
|
17
|
+
This product contains the Piccolo XML Parser for Java
|
18
|
+
(http://piccolo.sourceforge.net/). Copyright 2002 Yuval Oren.
|
19
|
+
|
20
|
+
This product contains the chunks_parse_cmds.tbl file from the vsdump program.
|
21
|
+
Copyright (C) 2006-2007 Valek Filippov (frob@df.ru)
|
@@ -1,115 +1,115 @@
|
|
1
|
-
The Apache POI is pleased to announce the release of POI 3.7.
|
2
|
-
|
3
|
-
See the downloads page for binary and source distributions: http://poi.apache.org/download.html
|
4
|
-
|
5
|
-
|
6
|
-
Release Notes -- Apache POI -- Version 3.7
|
7
|
-
|
8
|
-
Apache POI is an open source Java library for working with Microsoft Office documents.
|
9
|
-
POI 3.7 is an incremental feature release based on the earlier 3.6 release.
|
10
|
-
|
11
|
-
The most notable changes since POI 3.6 are:
|
12
|
-
-------------------------------------------
|
13
|
-
OOXML
|
14
|
-
* support for reading aes-encrypted/write-protected ooxml files
|
15
|
-
* support Java 1.5 in auto-generated xmlbeans for ooxml schemas
|
16
|
-
|
17
|
-
Spreadsheet (Excel)
|
18
|
-
* initial support for autofilters
|
19
|
-
* support for data validation for ooxml format
|
20
|
-
* initial support for themes for ooxml format
|
21
|
-
* added implementation for new functions: RANDBETWEEN, POISSON, SUBTOTAL, TEXT, TRUNC
|
22
|
-
* support evaluation of indirect defined names in INDIRECT
|
23
|
-
* numerous fixes and performance optimizations in the Formula Evaluation module
|
24
|
-
* ability to add, modify and remove series from HSSF Charts
|
25
|
-
* numerous improvements in the cell data formatter (handling more formatting rules, better color detection, allow overriding of default locale)
|
26
|
-
* more examples including a rich "spreadsheet to HTML" converter
|
27
|
-
|
28
|
-
Document (Word)
|
29
|
-
* initial support for the HWPF revision marks authors list
|
30
|
-
* support for border codes in HWPF
|
31
|
-
* support for processing of symbols in HWPF
|
32
|
-
* support sections in Word 6 and Word 95 files
|
33
|
-
* improved reading of auto-saved ("complex") documents in HWPF
|
34
|
-
* improved support for manipulation of tables and paragraphs in XWPF
|
35
|
-
|
36
|
-
SlideShow (PowerPoint)
|
37
|
-
* allow editing workbooks embedded into HSLF slide shows
|
38
|
-
|
39
|
-
Text Extraction
|
40
|
-
* support for text extraction from XSLF tables
|
41
|
-
* add PublisherTextExtractor support to extractorfactory
|
42
|
-
* support attachments as embedded documents within the new OutlookTextExtactor
|
43
|
-
* new event based XSSF text extractor (XSSFEventBasedExcelExtractor)
|
44
|
-
* make it easier to tell which content types each POIXMLTextExtractor handles
|
45
|
-
* paragraph level as well as whole-file text extraction for word 6/95 files
|
46
|
-
|
47
|
-
|
48
|
-
* ...and much much more: code cleanup, many bug fixes and performance improvements
|
49
|
-
|
50
|
-
Contributors
|
51
|
-
------------
|
52
|
-
|
53
|
-
The following people have contributed to this release by submitting bug
|
54
|
-
reports or by participating in the issue resolution process (in strict alphabetical order).
|
55
|
-
|
56
|
-
Alexey Butchik Jeff Lavezzo Philippe Laflamme
|
57
|
-
Andrew Shirley Jens Gatze Raiko Eckstein
|
58
|
-
Andrzej Bialecki Jerry Soung Rainer Schwarze
|
59
|
-
Antoni Mylka Jonathan Holloway Ranvijay Singh
|
60
|
-
Antony Bowesman Josh Micich Rick Cameron
|
61
|
-
Antti Koskimaki Jukka Zitting Robert Kish
|
62
|
-
Attila Kiraily Kai Zimmermann Robin Salkeld
|
63
|
-
Bob Smith Kalpesh Parmar Ryan Lauck
|
64
|
-
Brendan Nolan Kamil Soltys Ryan Skow
|
65
|
-
Charlie Chang Karl Eilebrecht Samuel Yung
|
66
|
-
Chris Barlock Ken Arnold Simon Kelly
|
67
|
-
Chris Lott Liu Yan Stefan Stern
|
68
|
-
Christiaan Fluit Lon Binder Steve Wolke
|
69
|
-
Dave Fisher Martin Studer TK Gospodinov
|
70
|
-
Dave Syer Martin W. Kirst Tao Jiang
|
71
|
-
David Agnew Maxim Valyanskiy Ted Schrader
|
72
|
-
David Lewis Michael Vilensky Thomas Herre
|
73
|
-
Dmitry Sviridov Michel Boudinot Tomas Prochazka
|
74
|
-
Domenico Napoletano Nick Burch Tony Harvey
|
75
|
-
Ed Beaty Paul Spencer Trejkaz (pen name)
|
76
|
-
Fabio Ebner Payam Hekmat Tsutomu YANO
|
77
|
-
Fred Ross Peter Kutak Viveck Shastri
|
78
|
-
Grzegorz Bloch Petr Udalau Vladimir Korenev
|
79
|
-
Henry Huang Phil Dunlea William J. Coleda
|
80
|
-
Immad Naseer Phil Varner Yegor Kozlov
|
81
|
-
Jan Stette Philipp Epp Zhang Zhang
|
82
|
-
|
83
|
-
|
84
|
-
Thank you all very much! Contributions are always welcome, come join the project.
|
85
|
-
|
86
|
-
Release Contents
|
87
|
-
----------------
|
88
|
-
|
89
|
-
This release comes in two forms:
|
90
|
-
- pre-built binaries containing compiled versions of all Apache POI components and documentation
|
91
|
-
(poi-bin-3.7-20101029.zip or poi-bin-3.7-20101029.tar.gz)
|
92
|
-
- source archive you can build POI from (poi-src-3.7-20101029.zip or poi-src-3.7-20101029.tar.gz)
|
93
|
-
|
94
|
-
Pre-built versions of all POI components are also available in the central Maven repository
|
95
|
-
under Group ID "org.apache.poi" and Version "3.7"
|
96
|
-
|
97
|
-
All release artifacts are accompanied by MD5 checksums and a PGP signatures
|
98
|
-
that you can use to verify the authenticity of your download.
|
99
|
-
The public key used for the PGP signature can be found at
|
100
|
-
http://svn.apache.org/repos/asf/poi/tags/REL_3_7/KEYS
|
101
|
-
|
102
|
-
|
103
|
-
About Apache POI
|
104
|
-
-----------------------
|
105
|
-
|
106
|
-
Apache POI is well-known in the Java field as a library for reading and
|
107
|
-
writing Microsoft Office file formats, such as Excel, PowerPoint, Visio and
|
108
|
-
Word. Since POI 3.5, the new OOXML (Office Open XML) formats introduced in Office 2007 have been supported.
|
109
|
-
|
110
|
-
For more information, visit http://poi.apache.org/
|
111
|
-
|
112
|
-
|
113
|
-
For the Apache POI Team
|
114
|
-
Yegor Kozlov
|
115
|
-
|
1
|
+
The Apache POI is pleased to announce the release of POI 3.7.
|
2
|
+
|
3
|
+
See the downloads page for binary and source distributions: http://poi.apache.org/download.html
|
4
|
+
|
5
|
+
|
6
|
+
Release Notes -- Apache POI -- Version 3.7
|
7
|
+
|
8
|
+
Apache POI is an open source Java library for working with Microsoft Office documents.
|
9
|
+
POI 3.7 is an incremental feature release based on the earlier 3.6 release.
|
10
|
+
|
11
|
+
The most notable changes since POI 3.6 are:
|
12
|
+
-------------------------------------------
|
13
|
+
OOXML
|
14
|
+
* support for reading aes-encrypted/write-protected ooxml files
|
15
|
+
* support Java 1.5 in auto-generated xmlbeans for ooxml schemas
|
16
|
+
|
17
|
+
Spreadsheet (Excel)
|
18
|
+
* initial support for autofilters
|
19
|
+
* support for data validation for ooxml format
|
20
|
+
* initial support for themes for ooxml format
|
21
|
+
* added implementation for new functions: RANDBETWEEN, POISSON, SUBTOTAL, TEXT, TRUNC
|
22
|
+
* support evaluation of indirect defined names in INDIRECT
|
23
|
+
* numerous fixes and performance optimizations in the Formula Evaluation module
|
24
|
+
* ability to add, modify and remove series from HSSF Charts
|
25
|
+
* numerous improvements in the cell data formatter (handling more formatting rules, better color detection, allow overriding of default locale)
|
26
|
+
* more examples including a rich "spreadsheet to HTML" converter
|
27
|
+
|
28
|
+
Document (Word)
|
29
|
+
* initial support for the HWPF revision marks authors list
|
30
|
+
* support for border codes in HWPF
|
31
|
+
* support for processing of symbols in HWPF
|
32
|
+
* support sections in Word 6 and Word 95 files
|
33
|
+
* improved reading of auto-saved ("complex") documents in HWPF
|
34
|
+
* improved support for manipulation of tables and paragraphs in XWPF
|
35
|
+
|
36
|
+
SlideShow (PowerPoint)
|
37
|
+
* allow editing workbooks embedded into HSLF slide shows
|
38
|
+
|
39
|
+
Text Extraction
|
40
|
+
* support for text extraction from XSLF tables
|
41
|
+
* add PublisherTextExtractor support to extractorfactory
|
42
|
+
* support attachments as embedded documents within the new OutlookTextExtactor
|
43
|
+
* new event based XSSF text extractor (XSSFEventBasedExcelExtractor)
|
44
|
+
* make it easier to tell which content types each POIXMLTextExtractor handles
|
45
|
+
* paragraph level as well as whole-file text extraction for word 6/95 files
|
46
|
+
|
47
|
+
|
48
|
+
* ...and much much more: code cleanup, many bug fixes and performance improvements
|
49
|
+
|
50
|
+
Contributors
|
51
|
+
------------
|
52
|
+
|
53
|
+
The following people have contributed to this release by submitting bug
|
54
|
+
reports or by participating in the issue resolution process (in strict alphabetical order).
|
55
|
+
|
56
|
+
Alexey Butchik Jeff Lavezzo Philippe Laflamme
|
57
|
+
Andrew Shirley Jens Gatze Raiko Eckstein
|
58
|
+
Andrzej Bialecki Jerry Soung Rainer Schwarze
|
59
|
+
Antoni Mylka Jonathan Holloway Ranvijay Singh
|
60
|
+
Antony Bowesman Josh Micich Rick Cameron
|
61
|
+
Antti Koskimaki Jukka Zitting Robert Kish
|
62
|
+
Attila Kiraily Kai Zimmermann Robin Salkeld
|
63
|
+
Bob Smith Kalpesh Parmar Ryan Lauck
|
64
|
+
Brendan Nolan Kamil Soltys Ryan Skow
|
65
|
+
Charlie Chang Karl Eilebrecht Samuel Yung
|
66
|
+
Chris Barlock Ken Arnold Simon Kelly
|
67
|
+
Chris Lott Liu Yan Stefan Stern
|
68
|
+
Christiaan Fluit Lon Binder Steve Wolke
|
69
|
+
Dave Fisher Martin Studer TK Gospodinov
|
70
|
+
Dave Syer Martin W. Kirst Tao Jiang
|
71
|
+
David Agnew Maxim Valyanskiy Ted Schrader
|
72
|
+
David Lewis Michael Vilensky Thomas Herre
|
73
|
+
Dmitry Sviridov Michel Boudinot Tomas Prochazka
|
74
|
+
Domenico Napoletano Nick Burch Tony Harvey
|
75
|
+
Ed Beaty Paul Spencer Trejkaz (pen name)
|
76
|
+
Fabio Ebner Payam Hekmat Tsutomu YANO
|
77
|
+
Fred Ross Peter Kutak Viveck Shastri
|
78
|
+
Grzegorz Bloch Petr Udalau Vladimir Korenev
|
79
|
+
Henry Huang Phil Dunlea William J. Coleda
|
80
|
+
Immad Naseer Phil Varner Yegor Kozlov
|
81
|
+
Jan Stette Philipp Epp Zhang Zhang
|
82
|
+
|
83
|
+
|
84
|
+
Thank you all very much! Contributions are always welcome, come join the project.
|
85
|
+
|
86
|
+
Release Contents
|
87
|
+
----------------
|
88
|
+
|
89
|
+
This release comes in two forms:
|
90
|
+
- pre-built binaries containing compiled versions of all Apache POI components and documentation
|
91
|
+
(poi-bin-3.7-20101029.zip or poi-bin-3.7-20101029.tar.gz)
|
92
|
+
- source archive you can build POI from (poi-src-3.7-20101029.zip or poi-src-3.7-20101029.tar.gz)
|
93
|
+
|
94
|
+
Pre-built versions of all POI components are also available in the central Maven repository
|
95
|
+
under Group ID "org.apache.poi" and Version "3.7"
|
96
|
+
|
97
|
+
All release artifacts are accompanied by MD5 checksums and a PGP signatures
|
98
|
+
that you can use to verify the authenticity of your download.
|
99
|
+
The public key used for the PGP signature can be found at
|
100
|
+
http://svn.apache.org/repos/asf/poi/tags/REL_3_7/KEYS
|
101
|
+
|
102
|
+
|
103
|
+
About Apache POI
|
104
|
+
-----------------------
|
105
|
+
|
106
|
+
Apache POI is well-known in the Java field as a library for reading and
|
107
|
+
writing Microsoft Office file formats, such as Excel, PowerPoint, Visio and
|
108
|
+
Word. Since POI 3.5, the new OOXML (Office Open XML) formats introduced in Office 2007 have been supported.
|
109
|
+
|
110
|
+
For more information, visit http://poi.apache.org/
|
111
|
+
|
112
|
+
|
113
|
+
For the Apache POI Team
|
114
|
+
Yegor Kozlov
|
115
|
+
|
data/lib/loaders/csv_loader.rb
CHANGED
@@ -1,99 +1,99 @@
|
|
1
|
-
# Copyright:: (c) Autotelik Media Ltd 2011
|
2
|
-
# Author :: Tom Statter
|
3
|
-
# Date :: Aug 2011
|
4
|
-
# License:: MIT
|
5
|
-
#
|
6
|
-
# Details:: Specific loader to support CSV files.
|
7
|
-
#
|
8
|
-
#
|
9
|
-
require 'loaders/loader_base'
|
10
|
-
require 'datashift/exceptions'
|
11
|
-
require 'datashift/method_mapper'
|
12
|
-
|
13
|
-
module DataShift
|
14
|
-
|
15
|
-
module CsvLoading
|
16
|
-
|
17
|
-
def perform_csv_load(file_name, options = {})
|
18
|
-
|
19
|
-
require "csv"
|
20
|
-
|
21
|
-
# TODO - can we abstract out what a 'parsed file' is - so a common object can represent excel,csv etc
|
22
|
-
# then we can make load() more generic
|
23
|
-
|
24
|
-
@parsed_file = CSV.read(file_name)
|
25
|
-
|
26
|
-
|
27
|
-
@method_mapper = DataShift::MethodMapper.new
|
28
|
-
|
29
|
-
@mandatory = options[:mandatory] || []
|
30
|
-
|
31
|
-
# Create a method_mapper which maps list of headers into suitable calls on the Active Record class
|
32
|
-
map_headers_to_operators( @parsed_file.shift, options[:strict] , @mandatory )
|
33
|
-
|
34
|
-
unless(@method_mapper.missing_methods.empty?)
|
35
|
-
puts "WARNING: Following column headings could not be mapped : #{@method_mapper.missing_methods.inspect}"
|
36
|
-
raise MappingDefinitionError, "ERROR: Missing mappings for #{@method_mapper.missing_methods.size} column headings"
|
37
|
-
end
|
38
|
-
|
39
|
-
#if(options[:verbose])
|
40
|
-
puts "\n\n\nLoading from CSV file: #{file_name}"
|
41
|
-
puts "Processing #{@parsed_file.size} rows"
|
42
|
-
# end
|
43
|
-
|
44
|
-
load_object_class.transaction do
|
45
|
-
@loaded_objects = []
|
46
|
-
|
47
|
-
@parsed_file.each do |row|
|
48
|
-
|
49
|
-
# TODO - Smart sorting of column processing order ....
|
50
|
-
# Does not currently ensure mandatory columns (for valid?) processed first but model needs saving
|
51
|
-
# before associations can be processed so user should ensure mandatory columns are prior to associations
|
52
|
-
|
53
|
-
# as part of this we also attempt to save early, for example before assigning to
|
54
|
-
# has_and_belongs_to associations which require the load_object has an id for the join table
|
55
|
-
|
56
|
-
# Iterate over the columns method_mapper found in Excel,
|
57
|
-
# pulling data out of associated column
|
58
|
-
@method_mapper.method_details.each_with_index do |method_detail, col|
|
59
|
-
|
60
|
-
value = row[col]
|
61
|
-
|
62
|
-
prepare_data(method_detail, value)
|
63
|
-
|
64
|
-
process()
|
65
|
-
end
|
66
|
-
|
67
|
-
# TODO - handle when it's not valid ?
|
68
|
-
# Process rest and dump out an exception list of Products ??
|
69
|
-
|
70
|
-
puts "SAVING ROW #{row} : #{load_object.inspect}" #if options[:verbose]
|
71
|
-
|
72
|
-
save
|
73
|
-
|
74
|
-
# don't forget to reset the object or we'll update rather than create
|
75
|
-
new_load_object
|
76
|
-
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
class CsvLoader < LoaderBase
|
83
|
-
|
84
|
-
include DataShift::CsvLoading
|
85
|
-
|
86
|
-
def initialize(klass, object = nil, options = {})
|
87
|
-
super( klass, object, options )
|
88
|
-
raise "Cannot load - failed to create a #{klass}" unless @load_object
|
89
|
-
end
|
90
|
-
|
91
|
-
def perform_load( file_name, options = {} )
|
92
|
-
perform_csv_load( file_name, options )
|
93
|
-
|
94
|
-
puts "CSV loading stage complete - #{loaded_objects.size} rows added."
|
95
|
-
end
|
96
|
-
|
97
|
-
end
|
98
|
-
|
1
|
+
# Copyright:: (c) Autotelik Media Ltd 2011
|
2
|
+
# Author :: Tom Statter
|
3
|
+
# Date :: Aug 2011
|
4
|
+
# License:: MIT
|
5
|
+
#
|
6
|
+
# Details:: Specific loader to support CSV files.
|
7
|
+
#
|
8
|
+
#
|
9
|
+
require 'loaders/loader_base'
|
10
|
+
require 'datashift/exceptions'
|
11
|
+
require 'datashift/method_mapper'
|
12
|
+
|
13
|
+
module DataShift
|
14
|
+
|
15
|
+
module CsvLoading
|
16
|
+
|
17
|
+
def perform_csv_load(file_name, options = {})
|
18
|
+
|
19
|
+
require "csv"
|
20
|
+
|
21
|
+
# TODO - can we abstract out what a 'parsed file' is - so a common object can represent excel,csv etc
|
22
|
+
# then we can make load() more generic
|
23
|
+
|
24
|
+
@parsed_file = CSV.read(file_name)
|
25
|
+
|
26
|
+
|
27
|
+
@method_mapper = DataShift::MethodMapper.new
|
28
|
+
|
29
|
+
@mandatory = options[:mandatory] || []
|
30
|
+
|
31
|
+
# Create a method_mapper which maps list of headers into suitable calls on the Active Record class
|
32
|
+
map_headers_to_operators( @parsed_file.shift, options[:strict] , @mandatory )
|
33
|
+
|
34
|
+
unless(@method_mapper.missing_methods.empty?)
|
35
|
+
puts "WARNING: Following column headings could not be mapped : #{@method_mapper.missing_methods.inspect}"
|
36
|
+
raise MappingDefinitionError, "ERROR: Missing mappings for #{@method_mapper.missing_methods.size} column headings"
|
37
|
+
end
|
38
|
+
|
39
|
+
#if(options[:verbose])
|
40
|
+
puts "\n\n\nLoading from CSV file: #{file_name}"
|
41
|
+
puts "Processing #{@parsed_file.size} rows"
|
42
|
+
# end
|
43
|
+
|
44
|
+
load_object_class.transaction do
|
45
|
+
@loaded_objects = []
|
46
|
+
|
47
|
+
@parsed_file.each do |row|
|
48
|
+
|
49
|
+
# TODO - Smart sorting of column processing order ....
|
50
|
+
# Does not currently ensure mandatory columns (for valid?) processed first but model needs saving
|
51
|
+
# before associations can be processed so user should ensure mandatory columns are prior to associations
|
52
|
+
|
53
|
+
# as part of this we also attempt to save early, for example before assigning to
|
54
|
+
# has_and_belongs_to associations which require the load_object has an id for the join table
|
55
|
+
|
56
|
+
# Iterate over the columns method_mapper found in Excel,
|
57
|
+
# pulling data out of associated column
|
58
|
+
@method_mapper.method_details.each_with_index do |method_detail, col|
|
59
|
+
|
60
|
+
value = row[col]
|
61
|
+
|
62
|
+
prepare_data(method_detail, value)
|
63
|
+
|
64
|
+
process()
|
65
|
+
end
|
66
|
+
|
67
|
+
# TODO - handle when it's not valid ?
|
68
|
+
# Process rest and dump out an exception list of Products ??
|
69
|
+
|
70
|
+
puts "SAVING ROW #{row} : #{load_object.inspect}" #if options[:verbose]
|
71
|
+
|
72
|
+
save
|
73
|
+
|
74
|
+
# don't forget to reset the object or we'll update rather than create
|
75
|
+
new_load_object
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
class CsvLoader < LoaderBase
|
83
|
+
|
84
|
+
include DataShift::CsvLoading
|
85
|
+
|
86
|
+
def initialize(klass, object = nil, options = {})
|
87
|
+
super( klass, object, options )
|
88
|
+
raise "Cannot load - failed to create a #{klass}" unless @load_object
|
89
|
+
end
|
90
|
+
|
91
|
+
def perform_load( file_name, options = {} )
|
92
|
+
perform_csv_load( file_name, options )
|
93
|
+
|
94
|
+
puts "CSV loading stage complete - #{loaded_objects.size} rows added."
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
99
|
end
|