Module: ASpaceImport::CSVConvert
- Defined in:
- backend/app/converters/lib/csv_converter.rb
Defined Under Namespace
Modules: ClassMethods
Classes: CSVSyntaxException, CellHandler
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.included(base) ⇒ Object
37
38
39
|
# File 'backend/app/converters/lib/csv_converter.rb', line 37
def self.included(base)
base.extend(ClassMethods)
end
|
Instance Method Details
#configuration ⇒ Object
42
43
44
|
# File 'backend/app/converters/lib/csv_converter.rb', line 42
def configuration
self.class.configuration
end
|
#get_new(key) ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'backend/app/converters/lib/csv_converter.rb', line 111
def get_new(key)
conf = configuration[key.to_sym] || {}
type = conf[:record_type] ? conf[:record_type] : key
proxy = @proxies.get_proxy_for(key, type)
if conf[:on_create]
proxy.on_spawn(conf[:on_create])
end
if conf[:defaults]
conf[:defaults].each do |key, val|
proxy.on_discharge(self, :set_default, key, val)
end
end
if conf[:on_row_complete]
proxy.on_discharge(conf[:on_row_complete], :call, @batch.working_area)
end
proxy
end
|
#get_queued_or_new(key) ⇒ Object
101
102
103
104
105
106
107
108
|
# File 'backend/app/converters/lib/csv_converter.rb', line 101
def get_queued_or_new(key)
if (prox = @batch.working_area.find {|j| j.key == key })
yield prox
elsif (prox = get_new(key))
yield prox
@batch << prox
end
end
|
#parse_cell(handler, cell_contents) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'backend/app/converters/lib/csv_converter.rb', line 87
def parse_cell(handler, cell_contents)
return nil unless handler
val = handler.(cell_contents)
return nil unless val
get_queued_or_new(handler.target_key) do |prox|
property = handler.target_path
prox.set(property, val)
end
end
|
#parse_row(row) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'backend/app/converters/lib/csv_converter.rb', line 73
def parse_row(row)
row.each_with_index { |cell, i| parse_cell(@cell_handlers[i], cell) }
@batch.working_area.map! {|proxy| proxy.spawn }.compact!
@batch.working_area.each { |obj| @proxies.discharge_proxy(obj.key, obj) }
@batch.flush
end
|
#run ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'backend/app/converters/lib/csv_converter.rb', line 47
def run
@cell_handlers = []
@proxies = ASpaceImport::RecordProxyMgr.new
CSV.open(@input_file, 'r:bom|utf-8') do |csv|
csv.each do |row|
next if row.all? {|cell| cell.to_s.strip.empty? }
if @cell_handlers.empty?
@cell_handlers, = self.class.configure_cell_handlers(row)
unless .empty?
Log.warn("Data source has headers that aren't defined: #{bad_headers.join(', ')}")
end
else
parse_row(row)
end
end
end
@proxies.undischarged.each do |prox|
Log.warn("Undischarged: #{prox.to_s}")
end
end
|
#set_default(property, val, obj) ⇒ Object
138
139
140
141
142
|
# File 'backend/app/converters/lib/csv_converter.rb', line 138
def set_default(property, val, obj)
if obj.send("#{property}").nil?
obj.send("#{property}=", val)
end
end
|