Class: Dreader::Engine
- Inherits:
-
Object
- Object
- Dreader::Engine
- Defined in:
- lib/dreader.rb
Overview
This is where the real stuff begins
Instance Attribute Summary collapse
-
#colspec ⇒ Object
readonly
the specification of the columns to process.
-
#options(&block) ⇒ Object
readonly
define a DSL for options any string is processed as an option and it ends up in the.
-
#table ⇒ Object
readonly
the data we read.
-
#virtualcols ⇒ Object
readonly
the specification of the virtual columns.
Instance Method Summary collapse
-
#bulk_declare(hash, &block) ⇒ Object
bulk declare columns we intend to read.
-
#column(name, &block) ⇒ Object
define a DSL for column specification - ‘name` is the name of the column - `block` contains two declarations, `process` and `check`, which are used, respectively, to make a cell into the desired data and to check whether the desired data is ok.
-
#debug(args = {}) ⇒ Object
show to stdout the first ‘n` records we read from the file given the current configuration.
-
#errors ⇒ Object
return an array of strings with all the errors we have encounterd an empty array is a good news.
-
#get_row(row_number) ⇒ Object
get (processed) row number.
-
#initialize ⇒ Engine
constructor
A new instance of Engine.
-
#mapping(&block) ⇒ Object
define what we do with each line we read - ‘block` is the code which takes as input a `row` and processes `row` is a hash in which each spreadsheet cell is accessible under the column names.
-
#process ⇒ Object
apply the mapping code to the array it makes sense to invoke it only once.
-
#read(args = {}) ⇒ Object
(also: #load)
read a file and store it internally.
- #to_s ⇒ Object
-
#virtual_column(name, &block) ⇒ Object
virtual columns define derived attributes the code specified in the virtual column is executed after reading a row and before applying the mapping function.
- #virtual_columns ⇒ Object
Constructor Details
#initialize ⇒ Engine
Returns a new instance of Engine.
111 112 113 114 115 |
# File 'lib/dreader.rb', line 111 def initialize = {} @colspec = [] @virtualcols = [] end |
Instance Attribute Details
#colspec ⇒ Object (readonly)
the specification of the columns to process
105 106 107 |
# File 'lib/dreader.rb', line 105 def colspec @colspec end |
#options(&block) ⇒ Object (readonly)
define a DSL for options any string is processed as an option and it ends up in the
103 104 105 |
# File 'lib/dreader.rb', line 103 def end |
#table ⇒ Object (readonly)
the data we read
109 110 111 |
# File 'lib/dreader.rb', line 109 def table @table end |
#virtualcols ⇒ Object (readonly)
the specification of the virtual columns
107 108 109 |
# File 'lib/dreader.rb', line 107 def virtualcols @virtualcols end |
Instance Method Details
#bulk_declare(hash, &block) ⇒ Object
bulk declare columns we intend to read
-
hash is a hash in the form { symbolic_name: colref }
i.bulk_declare ‘B’, age: ‘C’ is equivalent to:
i.column :name do
colref 'B'
end i.column :age do
colref 'C'
end
i.bulk_declare ‘B’, age: ‘C’ do
process do |cell|
cell.strip
end
end
is equivalent to:
i.column :name do
colref 'B'
process do |cell|
cell.strip
end
end i.column :age do
colref 'C'
process do |cell|
cell.strip
end
end
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/dreader.rb', line 172 def bulk_declare hash, &block hash.keys.each do |key| column = Column.new column.colref hash[key] if block column.instance_eval(&block) end @colspec << column.to_hash.merge({name: key}) end end |
#column(name, &block) ⇒ Object
define a DSL for column specification
-
‘name` is the name of the column
-
‘block` contains two declarations, `process` and `check`, which are used, respectively, to make a cell into the desired data and to check whether the desired data is ok
132 133 134 135 136 137 |
# File 'lib/dreader.rb', line 132 def column name, &block column = Column.new column.instance_eval(&block) @colspec << column.to_hash.merge({name: name}) end |
#debug(args = {}) ⇒ Object
show to stdout the first ‘n` records we read from the file given the current configuration
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/dreader.rb', line 292 def debug args = {} if args.class == Hash hash = .merge(args) else puts "dreader error at #{__callee__}: this function takes a Hash as input" exit end # apply some defaults, if not defined in the options hash[:process] = true if not hash.has_key? :process # shall we apply the process function? hash[:check] = true if not hash.has_key? :check # shall we check the data read? hash[:n] = 10 if not hash[:n] spreadsheet = Dreader::Engine.open_spreadsheet (hash[:filename]) sheet = spreadsheet.sheet(hash[:sheet] || 0) puts "Current configuration:" .each do |k, v| puts " #{k}: #{v}" end puts "Configuration used by debug:" hash.each do |k, v| puts " #{k}: #{v}" end n = hash[:n] first_row = hash[:first_row] || 1 last_row = first_row + n - 1 puts " Last row (according to roo): #{sheet.last_row}" puts " Number of rows I will read in this session: #{n} (from #{first_row} to #{last_row})" (first_row..last_row).each do |row_number| puts "Row #{row_number} is:" r = Hash.new @colspec.each_with_index do |colspec, index| colname = colspec[:name] cell = sheet.cell(row_number, colspec[:colref]) processed_str = "" checked_str = "" if hash[:process] begin processed = colspec[:process] ? colspec[:process].call(cell) : cell processed_str = "processed: '#{processed}' (#{processed.class})" rescue => e puts "dreader error at #{__callee__}: 'check' specification for :#{colname} raised an exception at row #{row_number} (col #{index + 1}, value: #{cell})" raise e end end if hash[:check] begin processed = colspec[:process] ? colspec[:process].call(cell) : cell check = colspec[:check] ? colspec[:check].call(processed) : "no check specified" checked_str = "checked: '#{check}'" rescue => e puts "dreader error at #{__callee__}: 'check' specification for #{colname} at row #{row_number} raised an exception (col #{index + 1}, value: #{cell})" raise e end end puts " #{colname} => orig: '#{cell}' (#{cell.class}) #{processed_str} #{checked_str} (column: '#{colspec[:colref]}')" end end end |
#errors ⇒ Object
return an array of strings with all the errors we have encounterd an empty array is a good news
362 363 364 |
# File 'lib/dreader.rb', line 362 def errors @errors end |
#get_row(row_number) ⇒ Object
get (processed) row number
-
row_number is the row to get: index starts at 1.
get_row(1) get the first line read, that is, the row specified by ‘first_row` in `options` (or in read)
You need to invoke read first
279 280 281 282 283 284 285 286 287 288 |
# File 'lib/dreader.rb', line 279 def get_row row_number if row_number > @table.size puts "dreader error at #{__callee__}: 'row_number' is out of range (did you invoke read first?)" exit elsif row_number <= 0 puts "dreader error at #{__callee__}: 'row_number' is zero or negative (first row is 1)." else @table[row_number - 1] end end |
#mapping(&block) ⇒ Object
define what we do with each line we read
-
‘block` is the code which takes as input a `row` and processes `row` is a hash in which each spreadsheet cell is accessible under the column names. Each cell has the following values: :value, :error, :row_number, :col_number
202 203 204 |
# File 'lib/dreader.rb', line 202 def mapping &block @mapping = block end |
#process ⇒ Object
apply the mapping code to the array it makes sense to invoke it only once
the mapping is applied only if it defined
388 389 390 391 392 |
# File 'lib/dreader.rb', line 388 def process @table.each do |r| @mapping.call(r) if @mapping end end |
#read(args = {}) ⇒ Object Also known as: load
read a file and store it internally
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/dreader.rb', line 215 def read args = {} if args.class == Hash hash = .merge(args) else puts "dreader error at #{__callee__}: this function takes a Hash as input" exit end spreadsheet = Dreader::Engine.open_spreadsheet (hash[:filename]) sheet = spreadsheet.sheet(hash[:sheet] || 0) @table = Array.new @errors = Array.new first_row = hash[:first_row] || 1 last_row = hash[:last_row] || sheet.last_row (first_row..last_row).each do |row_number| r = Hash.new @colspec.each_with_index do |colspec, index| cell = sheet.cell(row_number, colspec[:colref]) colname = colspec[:name] r[colname] = Hash.new r[colname][:row_number] = row_number r[colname][:col_number] = colspec[:colref] begin r[colname][:value] = value = colspec[:process] ? colspec[:process].call(cell) : cell rescue => e puts "dreader error at #{__callee__}: 'process' specification for :#{colname} raised an exception at row #{row_number} (col #{index + 1}, value: #{cell})" raise e end begin if colspec[:check] and not colspec[:check].call(value) then r[colname][:error] = true @errors << "dreader error at #{__callee__}: value \"#{cell}\" for #{colname} at row #{row_number} (col #{index + 1}) does not pass the check function" else r[colname][:error] = false end rescue => e puts "dreader error at #{__callee__}: 'check' specification for :#{colname} raised an exception at row #{row_number} (col #{index + 1}, value: #{cell})" raise e end end @table << r end @table end |
#to_s ⇒ Object
394 395 396 |
# File 'lib/dreader.rb', line 394 def to_s @table.to_s end |
#virtual_column(name, &block) ⇒ Object
virtual columns define derived attributes the code specified in the virtual column is executed after reading a row and before applying the mapping function
virtual colum declarations are executed in the order in which they are defined
190 191 192 193 194 195 |
# File 'lib/dreader.rb', line 190 def virtual_column name, &block column = Column.new column.instance_eval &block @virtualcols << column.to_hash.merge({name: name}) end |
#virtual_columns ⇒ Object
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/dreader.rb', line 366 def virtual_columns # execute the virtual column specification @table.each do |r| @virtualcols.each do |virtualcol| begin # add the cell to the table r[virtualcol[:name]] = { value: virtualcol[:process].call(r), virtual: true, } rescue => e puts "dreader error at #{__callee__}: 'process' specification for :#{virtualcol[:name]} raised an exception at row #{r[r.keys.first][:row_number]}" raise e end end end end |