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.
Instance Method Summary collapse
-
#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.
-
#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.
108 109 110 111 112 |
# File 'lib/dreader.rb', line 108 def initialize @options = {} @colspec = [] @virtualcols = [] end |
Instance Attribute Details
#colspec ⇒ Object (readonly)
the specification of the columns to process
104 105 106 |
# File 'lib/dreader.rb', line 104 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
102 103 104 |
# File 'lib/dreader.rb', line 102 def @options end |
#table ⇒ Object (readonly)
the data we read
106 107 108 |
# File 'lib/dreader.rb', line 106 def table @table end |
Instance Method Details
#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
129 130 131 132 133 134 |
# File 'lib/dreader.rb', line 129 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
210 211 212 213 214 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 |
# File 'lib/dreader.rb', line 210 def debug args = {} hash = @options.merge(args) # 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:" @options.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] processed = colspec[:process] ? colspec[:process].call(cell) : cell processed_str = "processed: '#{processed}' (#{processed.class})" end if hash[:check] processed = colspec[:process] ? colspec[:process].call(cell) : cell check = colspec[:check] ? colspec[:check].call(processed) : "no check specified" checked_str = "checked: '#{check}'" 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
265 266 267 |
# File 'lib/dreader.rb', line 265 def errors @errors 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
154 155 156 |
# File 'lib/dreader.rb', line 154 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
286 287 288 289 290 |
# File 'lib/dreader.rb', line 286 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
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/dreader.rb', line 167 def read args = {} hash = @options.merge(args) 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] r[colname][:value] = value = colspec[:process] ? colspec[:process].call(cell) : cell if colspec[:check] and not colspec[:check].call(value) then r[colname][:error] = true @errors << "Error: value \"#{cell}\" for #{colname} at row #{row_number} (col #{index + 1}) does not pass the check function" else r[colname][:error] = false end end @table << r end @table end |
#to_s ⇒ Object
292 293 294 |
# File 'lib/dreader.rb', line 292 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
142 143 144 145 146 147 |
# File 'lib/dreader.rb', line 142 def virtual_column name, &block column = Column.new column.instance_eval &block @virtualcols << column.to_hash.merge({name: name}) end |
#virtual_columns ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/dreader.rb', line 269 def virtual_columns # execute the virtual column specification @virtualcols.each do |virtualcol| @table.each do |r| # add the cell to the table r[virtualcol[:name]] = { value: virtualcol[:process].call(r), virtual: true, } end end end |