ar_loader 0.0.6 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/LICENSE +9 -9
  2. data/README.markdown +268 -221
  3. data/Rakefile +76 -76
  4. data/lib/VERSION +1 -1
  5. data/lib/ar_loader.rb +87 -66
  6. data/lib/ar_loader/exceptions.rb +2 -0
  7. data/lib/{engine → ar_loader}/file_definitions.rb +353 -353
  8. data/lib/{engine → ar_loader}/mapping_file_definitions.rb +87 -87
  9. data/lib/ar_loader/method_detail.rb +257 -0
  10. data/lib/ar_loader/method_mapper.rb +213 -0
  11. data/lib/helpers/jruby/jexcel_file.rb +187 -0
  12. data/lib/{engine → helpers/jruby}/word.rb +79 -70
  13. data/lib/helpers/spree_helper.rb +85 -0
  14. data/lib/loaders/csv_loader.rb +87 -0
  15. data/lib/loaders/excel_loader.rb +132 -0
  16. data/lib/loaders/loader_base.rb +205 -73
  17. data/lib/loaders/spree/image_loader.rb +45 -41
  18. data/lib/loaders/spree/product_loader.rb +140 -91
  19. data/lib/to_b.rb +24 -24
  20. data/spec/csv_loader_spec.rb +27 -0
  21. data/spec/database.yml +19 -6
  22. data/spec/db/migrate/20110803201325_create_test_bed.rb +78 -0
  23. data/spec/excel_loader_spec.rb +113 -98
  24. data/spec/fixtures/BadAssociationName.xls +0 -0
  25. data/spec/fixtures/DemoNegativeTesting.xls +0 -0
  26. data/spec/fixtures/DemoTestModelAssoc.xls +0 -0
  27. data/spec/fixtures/ProjectsMultiCategories.xls +0 -0
  28. data/spec/fixtures/SimpleProjects.xls +0 -0
  29. data/spec/fixtures/SpreeProducts.xls +0 -0
  30. data/spec/fixtures/SpreeZoneExample.csv +5 -0
  31. data/spec/fixtures/SpreeZoneExample.xls +0 -0
  32. data/spec/loader_spec.rb +116 -0
  33. data/spec/logs/test.log +5000 -0
  34. data/spec/method_mapper_spec.rb +222 -0
  35. data/spec/models.rb +55 -0
  36. data/spec/spec_helper.rb +85 -18
  37. data/spec/spree_loader_spec.rb +223 -157
  38. data/tasks/config/seed_fu_product_template.erb +15 -15
  39. data/tasks/config/tidy_config.txt +12 -12
  40. data/tasks/db_tasks.rake +64 -64
  41. data/tasks/excel_loader.rake +63 -113
  42. data/tasks/file_tasks.rake +36 -37
  43. data/tasks/loader.rake +45 -0
  44. data/tasks/spree/image_load.rake +108 -107
  45. data/tasks/spree/product_loader.rake +49 -107
  46. data/tasks/word_to_seedfu.rake +166 -166
  47. metadata +66 -61
  48. data/lib/engine/jruby/jexcel_file.rb +0 -182
  49. data/lib/engine/jruby/method_mapper_excel.rb +0 -44
  50. data/lib/engine/method_detail.rb +0 -140
  51. data/lib/engine/method_mapper.rb +0 -157
  52. data/lib/engine/method_mapper_csv.rb +0 -28
  53. data/spec/db/migrate/20110803201325_create_testbed.rb +0 -25
metadata CHANGED
@@ -1,21 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar_loader
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
5
4
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 6
10
- version: 0.0.6
5
+ version: 0.0.8
11
6
  platform: ruby
12
7
  authors:
13
- - thomas statter
8
+ - thomas statter
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-08-03 00:00:00 +01:00
13
+ date: 2011-08-15 00:00:00 +01:00
19
14
  default_executable:
20
15
  dependencies: []
21
16
 
@@ -26,46 +21,62 @@ executables: []
26
21
  extensions: []
27
22
 
28
23
  extra_rdoc_files:
29
- - README.markdown
30
- - LICENSE
24
+ - README.markdown
25
+ - LICENSE
31
26
  files:
32
- - LICENSE
33
- - README.markdown
34
- - Rakefile
35
- - lib/ar_loader.rb
36
- - lib/engine/file_definitions.rb
37
- - lib/engine/jruby/jexcel_file.rb
38
- - lib/engine/jruby/method_mapper_excel.rb
39
- - lib/engine/mapping_file_definitions.rb
40
- - lib/engine/method_detail.rb
41
- - lib/engine/method_mapper.rb
42
- - lib/engine/method_mapper_csv.rb
43
- - lib/engine/word.rb
44
- - lib/java/poi-3.2-FINAL-20081019.jar
45
- - lib/java/poi-3.6.jar
46
- - lib/java/poi-contrib-3.6-20091214.jar
47
- - lib/java/poi-examples-3.6-20091214.jar
48
- - lib/java/poi-ooxml-3.6-20091214.jar
49
- - lib/java/poi-ooxml-schemas-3.6-20091214.jar
50
- - lib/java/poi-scratchpad-3.6-20091214.jar
51
- - lib/loaders/loader_base.rb
52
- - lib/loaders/spree/image_loader.rb
53
- - lib/loaders/spree/product_loader.rb
54
- - lib/to_b.rb
55
- - lib/VERSION
56
- - spec/database.yml
57
- - spec/db/migrate/20110803201325_create_testbed.rb
58
- - spec/excel_loader_spec.rb
59
- - spec/spec_helper.rb
60
- - spec/spree_loader_spec.rb
61
- - tasks/config/seed_fu_product_template.erb
62
- - tasks/config/tidy_config.txt
63
- - tasks/db_tasks.rake
64
- - tasks/excel_loader.rake
65
- - tasks/file_tasks.rake
66
- - tasks/spree/image_load.rake
67
- - tasks/spree/product_loader.rake
68
- - tasks/word_to_seedfu.rake
27
+ - LICENSE
28
+ - README.markdown
29
+ - Rakefile
30
+ - lib/ar_loader.rb
31
+ - lib/to_b.rb
32
+ - lib/VERSION
33
+ - lib/ar_loader/exceptions.rb
34
+ - lib/ar_loader/file_definitions.rb
35
+ - lib/ar_loader/mapping_file_definitions.rb
36
+ - lib/ar_loader/method_detail.rb
37
+ - lib/ar_loader/method_mapper.rb
38
+ - lib/helpers/spree_helper.rb
39
+ - lib/helpers/jruby/jexcel_file.rb
40
+ - lib/helpers/jruby/word.rb
41
+ - lib/java/poi-3.2-FINAL-20081019.jar
42
+ - lib/java/poi-3.6.jar
43
+ - lib/java/poi-contrib-3.6-20091214.jar
44
+ - lib/java/poi-examples-3.6-20091214.jar
45
+ - lib/java/poi-ooxml-3.6-20091214.jar
46
+ - lib/java/poi-ooxml-schemas-3.6-20091214.jar
47
+ - lib/java/poi-scratchpad-3.6-20091214.jar
48
+ - lib/loaders/csv_loader.rb
49
+ - lib/loaders/excel_loader.rb
50
+ - lib/loaders/loader_base.rb
51
+ - lib/loaders/spree/image_loader.rb
52
+ - lib/loaders/spree/product_loader.rb
53
+ - spec/csv_loader_spec.rb
54
+ - spec/database.yml
55
+ - spec/excel_loader_spec.rb
56
+ - spec/loader_spec.rb
57
+ - spec/method_mapper_spec.rb
58
+ - spec/models.rb
59
+ - spec/spec_helper.rb
60
+ - spec/spree_loader_spec.rb
61
+ - spec/db/migrate/20110803201325_create_test_bed.rb
62
+ - spec/fixtures/BadAssociationName.xls
63
+ - spec/fixtures/DemoNegativeTesting.xls
64
+ - spec/fixtures/DemoTestModelAssoc.xls
65
+ - spec/fixtures/ProjectsMultiCategories.xls
66
+ - spec/fixtures/SimpleProjects.xls
67
+ - spec/fixtures/SpreeProducts.xls
68
+ - spec/fixtures/SpreeZoneExample.csv
69
+ - spec/fixtures/SpreeZoneExample.xls
70
+ - spec/logs/test.log
71
+ - tasks/db_tasks.rake
72
+ - tasks/excel_loader.rake
73
+ - tasks/file_tasks.rake
74
+ - tasks/loader.rake
75
+ - tasks/word_to_seedfu.rake
76
+ - tasks/config/seed_fu_product_template.erb
77
+ - tasks/config/tidy_config.txt
78
+ - tasks/spree/image_load.rake
79
+ - tasks/spree/product_loader.rake
69
80
  has_rdoc: true
70
81
  homepage: http://www.autotelik.co.uk
71
82
  licenses: []
@@ -74,29 +85,23 @@ post_install_message:
74
85
  rdoc_options: []
75
86
 
76
87
  require_paths:
77
- - lib
88
+ - lib
78
89
  required_ruby_version: !ruby/object:Gem::Requirement
79
90
  none: false
80
91
  requirements:
81
- - - ">="
82
- - !ruby/object:Gem::Version
83
- hash: 3
84
- segments:
85
- - 0
86
- version: "0"
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
87
95
  required_rubygems_version: !ruby/object:Gem::Requirement
88
96
  none: false
89
97
  requirements:
90
- - - ">="
91
- - !ruby/object:Gem::Version
92
- hash: 3
93
- segments:
94
- - 0
95
- version: "0"
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: "0"
96
101
  requirements: []
97
102
 
98
103
  rubyforge_project:
99
- rubygems_version: 1.5.2
104
+ rubygems_version: 1.5.1
100
105
  signing_key:
101
106
  specification_version: 3
102
107
  summary: File based loader for Active Record models
@@ -1,182 +0,0 @@
1
- # Copyright:: (c) Autotelik Media Ltd 2011
2
- # Author :: Tom Statter
3
- # Date :: Aug 2010
4
- # License:: MIT
5
- #
6
- # An Excel file helper. Create and populate XSL files
7
- #
8
- # The maximum number of columns and rows in an Excel file is fixed at 256 Columns and 65536 Rows
9
- #
10
- # POI jar location needs to be added to class path.
11
- #
12
- # TODO - Check out http://poi.apache.org/poi-ruby.html
13
- #
14
- class Object
15
- def add_to_classpath(path)
16
- $CLASSPATH << File.join( ArLoader.root_path, 'lib', path.gsub("\\", "/") )
17
- end
18
- end
19
-
20
- require 'java'
21
- require 'rubygems'
22
-
23
- add_to_classpath 'java/poi-3.6.jar'
24
-
25
- class JExcelFile
26
- include_class 'org.apache.poi.poifs.filesystem.POIFSFileSystem'
27
- include_class 'org.apache.poi.hssf.usermodel.HSSFCell'
28
- include_class 'org.apache.poi.hssf.usermodel.HSSFWorkbook'
29
- include_class 'org.apache.poi.hssf.usermodel.HSSFCellStyle'
30
- include_class 'org.apache.poi.hssf.usermodel.HSSFDataFormat'
31
-
32
- include_class 'java.io.ByteArrayOutputStream'
33
- include_class 'java.util.Date'
34
- include_class 'java.io.FileInputStream'
35
-
36
- attr_accessor :book, :row, :current_sheet
37
-
38
- attr_reader :sheet
39
-
40
- MAX_COLUMNS = 256.freeze
41
- MAX_ROWS = 65536.freeze
42
-
43
- # The HSSFWorkbook uses 0 based indexes
44
-
45
- def initialize()
46
- @book = nil
47
- end
48
-
49
- def open(filename)
50
- inp = FileInputStream.new(filename)
51
-
52
- @book = HSSFWorkbook.new(inp)
53
-
54
- sheet(0) # also sets @current_sheet
55
- end
56
-
57
- def create(sheet_name)
58
- @book = HSSFWorkbook.new()
59
- @sheet = @book.createSheet(sheet_name.gsub(" ", ''))
60
- date_style = @book.createCellStyle()
61
- date_style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"))
62
- end
63
-
64
- # Return the current or specified HSSFSheet
65
- def sheet(i = nil)
66
- @current_sheet = i if i
67
- @sheet = @book.getSheetAt(@current_sheet)
68
- @sheet
69
- end
70
-
71
- def num_rows
72
- @sheet.getPhysicalNumberOfRows
73
- end
74
-
75
- # Process each row. (type is org.apache.poi.hssf.usermodel.HSSFRow)
76
-
77
- def each_row
78
- @sheet.rowIterator.each { |row| yield row }
79
- end
80
-
81
-
82
- # Create new row, bring index in line with POI usage (our 1 is their 0)
83
- def create_row(index)
84
- @row = @sheet.createRow(index)
85
- @row
86
- end
87
-
88
- def set_cell(row, column, data)
89
- @row = @sheet.getRow(row) || create_row(row)
90
- @row.createCell(column).setCellValue(data)
91
- end
92
-
93
- def value(row, column)
94
- raise TypeError, "Expect row argument of type HSSFRow" unless row.is_a?(Java::OrgApachePoiHssfUsermodel::HSSFRow)
95
- #puts "DEBUG - CELL VALUE : #{column} => #{ cell_value( row.getCell(column) ).inspect}"
96
- cell_value( row.getCell(column) )
97
- end
98
-
99
- def cell_value(cell)
100
- return nil unless cell
101
- #puts "DEBUG CELL TYPE : #{cell} => #{cell.getCellType().inspect}"
102
- case (cell.getCellType())
103
- when HSSFCell::CELL_TYPE_FORMULA then return cell.getCellFormula()
104
- when HSSFCell::CELL_TYPE_NUMERIC then return cell.getNumericCellValue()
105
- when HSSFCell::CELL_TYPE_STRING then return cell.getStringCellValue()
106
- when HSSFCell::CELL_TYPE_BOOLEAN then return cell.getBooleanCellValue()
107
- when HSSFCell::CELL_TYPE_BLANK then return ""
108
- end
109
- end
110
-
111
- def save( filename )
112
- File.open( filename, 'w') {|f| f.write(to_s) }
113
- end
114
-
115
-
116
- # The internal representation of a Excel File
117
-
118
- def to_s
119
- outs = ByteArrayOutputStream.new
120
- @book.write(outs);
121
- outs.close();
122
- String.from_java_bytes(outs.toByteArray)
123
- end
124
-
125
- end
126
-
127
- module ExcelHelper
128
- require 'java'
129
-
130
- include_class 'org.apache.poi.poifs.filesystem.POIFSFileSystem'
131
- include_class 'org.apache.poi.hssf.usermodel.HSSFCell'
132
- include_class 'org.apache.poi.hssf.usermodel.HSSFWorkbook'
133
- include_class 'org.apache.poi.hssf.usermodel.HSSFCellStyle'
134
- include_class 'org.apache.poi.hssf.usermodel.HSSFDataFormat'
135
- include_class 'java.io.ByteArrayOutputStream'
136
- include_class 'java.util.Date'
137
-
138
- # ActiveRecord Helper - Export model data to XLS file format
139
- #
140
- def to_xls(items=[])
141
-
142
- @excel = ExcelFile.new(items[0].class.name)
143
-
144
- @excel.create_row(0)
145
-
146
- sheet = @excel.sheet
147
-
148
- # header row
149
- if !items.empty?
150
- row = sheet.createRow(0)
151
- cell_index = 0
152
- items[0].class.columns.each do |column|
153
- row.createCell(cell_index).setCellValue(column.name)
154
- cell_index += 1
155
- end
156
- end
157
-
158
- # value rows
159
- row_index = 1
160
- items.each do |item|
161
- row = sheet.createRow(row_index);
162
-
163
- cell_index = 0
164
- item.class.columns.each do |column|
165
- cell = row.createCell(cell_index)
166
- if column.sql_type =~ /date/ then
167
- millis = item.send(column.name).to_f * 1000
168
- cell.setCellValue(Date.new(millis))
169
- cell.setCellStyle(dateStyle);
170
- elsif column.sql_type =~ /int/ then
171
- cell.setCellValue(item.send(column.name).to_i)
172
- else
173
- value = item.send(column.name)
174
- cell.setCellValue(item.send(column.name)) unless value.nil?
175
- end
176
- cell_index += 1
177
- end
178
- row_index += 1
179
- end
180
- @excel.to_s
181
- end
182
- end
@@ -1,44 +0,0 @@
1
- # Copyright:: (c) Autotelik Media Ltd 2011
2
- # Author :: Tom Statter
3
- # Date :: Jan 2011
4
- # License:: MIT
5
- #
6
- # JAVA SPECIFIC LOAD
7
- require 'java'
8
- require 'rubygems'
9
- require 'jexcel_file'
10
- require 'method_mapper'
11
-
12
- class MethodMapperExcel < MethodMapper
13
-
14
- attr_accessor :excel, :sheet
15
-
16
- # Read the headers from a spreadsheet and map to ActiveRecord members/associations
17
-
18
- def initialize( file_name, klass, sheet_number = 0 )
19
- super()
20
-
21
- @excel = JExcelFile.new
22
-
23
- @excel.open(file_name)
24
-
25
- @sheet = @excel.sheet( sheet_number )
26
-
27
- @header_row = @sheet.getRow(0)
28
-
29
- raise "ERROR: No headers found - Check Sheet #{@sheet} is completed sheet and Row 1 contains headers" unless @header_row
30
-
31
- @headers = []
32
- (0..JExcelFile::MAX_COLUMNS).each do |i|
33
- cell = @header_row.getCell(i)
34
- break unless cell
35
- @headers << "#{@excel.cell_value(cell).to_s}".strip
36
- end
37
-
38
- # Gather list of all possible 'setter' methods on AR class (instance variables and associations)
39
- MethodMapperExcel.find_operators( klass )
40
-
41
- # Convert the list of headers into suitable calls on the Active Record class
42
- find_method_details( klass, @headers )
43
- end
44
- end
@@ -1,140 +0,0 @@
1
- # Copyright:: (c) Autotelik Media Ltd 2011
2
- # Author :: Tom Statter
3
- # Date :: Aug 2010
4
- # License:: MIT
5
- #
6
- # Details:: This class provides information and access to the individual methods
7
- # on an AR model. Populated by, and coupled with MethodMapper,
8
- # which does the model interrogation work.
9
- # Enables 'loaders' to iterate over the MethodMapper results set,
10
- # and assign values to AR object, without knowing anything about that receiving object.
11
- #
12
- # =>
13
- require 'to_b'
14
-
15
- class MethodDetail
16
-
17
- # When looking up an association, try each of these in turn till a match
18
- # i.e find_by_name .. find_by_title and so on
19
- @@insistent_find_by_list ||= [:id, :name, :title]
20
-
21
- attr_accessor :klass, :name, :assignment, :col_type
22
- attr_accessor :has_many, :has_many_class_name, :has_many_class
23
- attr_accessor :belongs_to, :belongs_to_class_name, :belongs_to_class
24
-
25
- @@default_values = {}
26
- @@prefixes = {}
27
-
28
-
29
- def initialize(klass, name, assignment, belongs_to, has_many, col_type = nil)
30
- @klass, @name, @assignment, @has_many, @belongs_to, @col_type = klass, name, assignment, has_many, belongs_to, col_type
31
-
32
- if(@has_many)
33
- begin
34
- @has_many_class = Kernel.const_get(@has_many.classify)
35
- @has_many_class_name = @has_many.classify
36
- rescue
37
- end
38
- end
39
-
40
- if(@belongs_to)
41
- begin
42
- @belongs_to_class = Kernel.const_get(@belongs_to.classify)
43
- @belongs_to_class_name = @belongs_to.classify
44
- rescue
45
- # TODO - try other forms of the name, set to nil, or bomb out ?
46
- end
47
- end
48
- end
49
-
50
- def assign( record, value )
51
- #puts "DEBUG: assign: [#{@name}]"
52
-
53
- data = value
54
-
55
- if(@@default_values[@name])
56
- puts "WARNING nil value supplied for [#{@name}] - Using default : [#{@@default_values[@name]}]"
57
- data = @@default_values[@name]
58
- else
59
- puts "WARNING nil value supplied for [#{@name}] - No default"
60
- end if(data.nil?)
61
-
62
- data = "#{@@prefixes[@name]}#{data}" if(@@prefixes[@name])
63
-
64
- if( @belongs_to )
65
-
66
- #puts "DEBUG : BELONGS_TO #{@belongs_to} - Lookup #{data} in DB"
67
- insistent_belongs_to(record, data)
68
-
69
- elsif( @assignment && @col_type )
70
- puts "DEBUG : COl TYPE defined for #{@name} : #{@assignment} => #{data} #{@col_type.inspect}"
71
- record.send( @assignment, @col_type.type_cast( data ) )
72
-
73
- elsif( @assignment )
74
- puts "DEBUG : No COL TYPE found for #{@name} : #{@assignment} => #{data}"
75
- insistent_assignment(record, data)
76
- end
77
- end
78
-
79
- # Attempt to find the associated object via id, name, title ....
80
- def insistent_belongs_to( record, value )
81
-
82
- @@insistent_find_by_list.each do |x|
83
- begin
84
- item = @belongs_to_class.send( "find_by_#{x}", value)
85
- if(item)
86
- record.send("#{@belongs_to}=", item)
87
- break
88
- end
89
- rescue => e
90
- puts "ERROR: #{e.inspect}"
91
- if(x == @@insistent_method_list.last)
92
- raise "I'm sorry I have failed to assign [#{value}] to #{@assignment}" unless value.nil?
93
- end
94
- end
95
- end
96
- end
97
-
98
- def insistent_assignment( record, value )
99
- puts "DEBUG: RECORD CLASS #{record.class}"
100
- @@insistent_method_list ||= [:to_s, :to_i, :to_f, :to_b]
101
- begin
102
- record.send(@assignment, value)
103
- rescue => e
104
- puts e.inspect
105
- @@insistent_method_list.each do |f|
106
- begin
107
-
108
- record.send(@assignment, value.send( f) )
109
- break
110
- rescue => e
111
- #puts "DEBUG: insistent_assignment: #{e.inspect}"
112
- if f == @@insistent_method_list.last
113
- puts "I'm sorry I have failed to assign [#{value}] to #{@assignment}"
114
- raise "I'm sorry I have failed to assign [#{value}] to #{@assignment}" unless value.nil?
115
- end
116
- end
117
- end
118
- end
119
- end
120
-
121
- def self.set_default_value( name, value )
122
- @@default_values[name] = value
123
- end
124
-
125
- def self.default_value(name)
126
- @@default_values[name]
127
- end
128
-
129
- def self.set_prefix( name, value )
130
- @@prefixes[name] = value
131
- end
132
-
133
- def self.default_value(name)
134
- @@prefixes[name]
135
- end
136
-
137
- def pp
138
- "#{@name} => #{@assignment} : #{@has_many}"
139
- end
140
- end