ar_loader 0.0.4 → 0.0.6
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/LICENSE +9 -9
- data/README.markdown +221 -210
- data/Rakefile +76 -76
- data/lib/VERSION +1 -1
- data/lib/ar_loader.rb +66 -53
- data/lib/engine/file_definitions.rb +353 -353
- data/lib/engine/jruby/jexcel_file.rb +181 -181
- data/lib/engine/jruby/method_mapper_excel.rb +43 -43
- data/lib/engine/mapping_file_definitions.rb +87 -87
- data/lib/engine/method_detail.rb +140 -139
- data/lib/engine/method_mapper.rb +156 -156
- data/lib/engine/method_mapper_csv.rb +27 -27
- data/lib/engine/word.rb +70 -70
- data/lib/loaders/loader_base.rb +73 -60
- data/lib/loaders/spree/image_loader.rb +41 -46
- data/lib/loaders/spree/product_loader.rb +91 -92
- data/lib/to_b.rb +24 -24
- data/spec/database.yml +6 -0
- data/spec/db/migrate/20110803201325_create_testbed.rb +25 -0
- data/spec/excel_loader_spec.rb +98 -137
- data/spec/spec_helper.rb +18 -36
- data/spec/spree_loader_spec.rb +158 -0
- data/tasks/{seed_fu_product_template.erb → config/seed_fu_product_template.erb} +15 -15
- data/tasks/{tidy_config.txt → config/tidy_config.txt} +12 -12
- data/tasks/db_tasks.rake +64 -64
- data/tasks/excel_loader.rake +113 -100
- data/tasks/file_tasks.rake +37 -37
- data/tasks/spree/image_load.rake +107 -102
- data/tasks/spree/product_loader.rake +107 -106
- data/tasks/word_to_seedfu.rake +166 -166
- metadata +61 -47
@@ -1,182 +1,182 @@
|
|
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
|
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
182
|
end
|
@@ -1,44 +1,44 @@
|
|
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
|
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
44
|
end
|
@@ -1,88 +1,88 @@
|
|
1
|
-
# This class provides a value map (hash) from a text mapping file
|
2
|
-
#
|
3
|
-
# The map file is a text file of delimeted key -> values pairs
|
4
|
-
#
|
5
|
-
# SUPPORTED FILE FORMATS:
|
6
|
-
#
|
7
|
-
# 2 column e.g. a,b
|
8
|
-
# creates a simple hash {a => b)
|
9
|
-
#
|
10
|
-
# 3 column e.g. a,b,c
|
11
|
-
# a,b becomes the key, c is the vaule
|
12
|
-
# creates a hash { [a,b] => c }
|
13
|
-
#
|
14
|
-
# 4 column e.g. a,b,c,d
|
15
|
-
# a,b becomes the key, c,d the value
|
16
|
-
# creates a hash { [a,b] => [c,d] }
|
17
|
-
#
|
18
|
-
# TODO allow mapping file to be an xml file
|
19
|
-
#
|
20
|
-
class ValueMapFromFile < Hash
|
21
|
-
|
22
|
-
def intialize(file_path, delim = ',')
|
23
|
-
@delegate_to = {}
|
24
|
-
@delim = delim
|
25
|
-
load_map(file_path)
|
26
|
-
end
|
27
|
-
|
28
|
-
def load_map(file_path = nil, delim = ',')
|
29
|
-
@file = file_path unless file_path.nil?
|
30
|
-
@delim = delim
|
31
|
-
|
32
|
-
raise BadConfigError.new("Can not read map file: #{@file}") unless File.readable?(@file)
|
33
|
-
|
34
|
-
File.open(@file).each_line do |line|
|
35
|
-
next unless(line && line.chomp!)
|
36
|
-
|
37
|
-
values = line.split(@delim)
|
38
|
-
|
39
|
-
case values.nitems
|
40
|
-
when 2: self.store(values[0], values[1])
|
41
|
-
when 3: self.store([values[0], values[1]], values[2])
|
42
|
-
when 4: self.store([values[0], values[1]],[values[2], values[3]])
|
43
|
-
else
|
44
|
-
raise BadConfigError.new("Bad key,value row in #{@file}: #{values.nitems} number of columns not supported")
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
return self
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
|
53
|
-
# Expects file of format [TradeType,LDN_TradeId,HUB_TradeId,LDN_AssetId,HUB_AssetId,LDN_StrutureId,HUB_StructureId,LDN_ProductType,HUB_ProductType]
|
54
|
-
# Convets in to and araya containing rows [LDN_TradeId, LDN_AssetId, HUB_TradeId, HUB_AssetId]
|
55
|
-
class AssetMapFromFile < Array
|
56
|
-
|
57
|
-
def intialize(file_path, delim = ',')
|
58
|
-
@delegate_to = {}
|
59
|
-
@delim = delim
|
60
|
-
load_map(file_path)
|
61
|
-
end
|
62
|
-
|
63
|
-
def load_map(file_path = nil, delim = ',')
|
64
|
-
@file = file_path unless file_path.nil?
|
65
|
-
@delim = delim
|
66
|
-
|
67
|
-
raise BadConfigError.new("Can not read asset map file: #{@file}") unless File.readable?(@file)
|
68
|
-
|
69
|
-
File.open(@file).each_line do |line|
|
70
|
-
next unless(line && line.chomp!)
|
71
|
-
# skip the header row
|
72
|
-
next if line.include?('TradeType')
|
73
|
-
|
74
|
-
values = line.split(@delim)
|
75
|
-
|
76
|
-
self.push(Array[values[1], values[3], values[2], values[4]])
|
77
|
-
end
|
78
|
-
|
79
|
-
return self
|
80
|
-
end
|
81
|
-
|
82
|
-
def write_map(file_path = nil, delim = ',')
|
83
|
-
mapfile = File.open( file_path, 'w')
|
84
|
-
|
85
|
-
self.each{|row| mapfile.write(row.join(delim)+"\n")}
|
86
|
-
end
|
87
|
-
|
1
|
+
# This class provides a value map (hash) from a text mapping file
|
2
|
+
#
|
3
|
+
# The map file is a text file of delimeted key -> values pairs
|
4
|
+
#
|
5
|
+
# SUPPORTED FILE FORMATS:
|
6
|
+
#
|
7
|
+
# 2 column e.g. a,b
|
8
|
+
# creates a simple hash {a => b)
|
9
|
+
#
|
10
|
+
# 3 column e.g. a,b,c
|
11
|
+
# a,b becomes the key, c is the vaule
|
12
|
+
# creates a hash { [a,b] => c }
|
13
|
+
#
|
14
|
+
# 4 column e.g. a,b,c,d
|
15
|
+
# a,b becomes the key, c,d the value
|
16
|
+
# creates a hash { [a,b] => [c,d] }
|
17
|
+
#
|
18
|
+
# TODO allow mapping file to be an xml file
|
19
|
+
#
|
20
|
+
class ValueMapFromFile < Hash
|
21
|
+
|
22
|
+
def intialize(file_path, delim = ',')
|
23
|
+
@delegate_to = {}
|
24
|
+
@delim = delim
|
25
|
+
load_map(file_path)
|
26
|
+
end
|
27
|
+
|
28
|
+
def load_map(file_path = nil, delim = ',')
|
29
|
+
@file = file_path unless file_path.nil?
|
30
|
+
@delim = delim
|
31
|
+
|
32
|
+
raise BadConfigError.new("Can not read map file: #{@file}") unless File.readable?(@file)
|
33
|
+
|
34
|
+
File.open(@file).each_line do |line|
|
35
|
+
next unless(line && line.chomp!)
|
36
|
+
|
37
|
+
values = line.split(@delim)
|
38
|
+
|
39
|
+
case values.nitems
|
40
|
+
when 2: self.store(values[0], values[1])
|
41
|
+
when 3: self.store([values[0], values[1]], values[2])
|
42
|
+
when 4: self.store([values[0], values[1]],[values[2], values[3]])
|
43
|
+
else
|
44
|
+
raise BadConfigError.new("Bad key,value row in #{@file}: #{values.nitems} number of columns not supported")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
return self
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
# Expects file of format [TradeType,LDN_TradeId,HUB_TradeId,LDN_AssetId,HUB_AssetId,LDN_StrutureId,HUB_StructureId,LDN_ProductType,HUB_ProductType]
|
54
|
+
# Convets in to and araya containing rows [LDN_TradeId, LDN_AssetId, HUB_TradeId, HUB_AssetId]
|
55
|
+
class AssetMapFromFile < Array
|
56
|
+
|
57
|
+
def intialize(file_path, delim = ',')
|
58
|
+
@delegate_to = {}
|
59
|
+
@delim = delim
|
60
|
+
load_map(file_path)
|
61
|
+
end
|
62
|
+
|
63
|
+
def load_map(file_path = nil, delim = ',')
|
64
|
+
@file = file_path unless file_path.nil?
|
65
|
+
@delim = delim
|
66
|
+
|
67
|
+
raise BadConfigError.new("Can not read asset map file: #{@file}") unless File.readable?(@file)
|
68
|
+
|
69
|
+
File.open(@file).each_line do |line|
|
70
|
+
next unless(line && line.chomp!)
|
71
|
+
# skip the header row
|
72
|
+
next if line.include?('TradeType')
|
73
|
+
|
74
|
+
values = line.split(@delim)
|
75
|
+
|
76
|
+
self.push(Array[values[1], values[3], values[2], values[4]])
|
77
|
+
end
|
78
|
+
|
79
|
+
return self
|
80
|
+
end
|
81
|
+
|
82
|
+
def write_map(file_path = nil, delim = ',')
|
83
|
+
mapfile = File.open( file_path, 'w')
|
84
|
+
|
85
|
+
self.each{|row| mapfile.write(row.join(delim)+"\n")}
|
86
|
+
end
|
87
|
+
|
88
88
|
end
|