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
-
#array ⇒ Object
readonly
the data we read.
-
#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.
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.
-
#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.
-
#read(filename = nil) ⇒ Object
read a file and store it internally return the data we read in the form of an array of hashes.
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ Engine
47 48 49 50 |
# File 'lib/dreader.rb', line 47 def initialize = {} @colspec = [] end |
Instance Attribute Details
#array ⇒ Object (readonly)
the data we read
45 46 47 |
# File 'lib/dreader.rb', line 45 def array @array end |
#colspec ⇒ Object (readonly)
the specification of the columns to process
43 44 45 |
# File 'lib/dreader.rb', line 43 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
41 42 43 |
# File 'lib/dreader.rb', line 41 def 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
67 68 69 70 71 72 |
# File 'lib/dreader.rb', line 67 def column name, &block column = Column.new column.instance_eval(&block) @colspec << column.to_hash.merge({name: name}) end |
#errors ⇒ Object
return an array of strings with all the errors we have encounterd an empty array is a good news
122 123 124 |
# File 'lib/dreader.rb', line 122 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
79 80 81 |
# File 'lib/dreader.rb', line 79 def mapping &block @mapping = block end |
#process ⇒ Object
apply the mapping code to the array it makes sense to invoke it only
128 129 130 131 132 |
# File 'lib/dreader.rb', line 128 def process @array.each do |r| @mapping.call(r) end end |
#read(filename = nil) ⇒ Object
read a file and store it internally return the data we read in the form of an array of hashes
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/dreader.rb', line 85 def read filename=nil spreadsheet = self.open_spreadsheet filename || [:filename] sheet = spreadsheet.sheet([:sheet] || 0) @array = Array.new @errors = Array.new first_row = [:start_at] || 1 (first_row..spreadsheet.last_row).each do |row_number| row = spreadsheet.row(row_number) r = Hash.new @colspec.each_with_index do |colspec, index| colname = colspec[:name] r[colname] = Hash.new r[colname][:row_number] = row_number r[colname][:col_number] = index + 1 r[colname][:value] = value = colspec[:process] ? colspec[:process].call(row[index]) : row[index] if colspec[:check] and not colspec[:check].call(value) then r[colname][:error] = true @errors << "Error: value \"#{row[index]}\" for #{colname} at row #{row_number} (col #{index + 1}) is incorrect" else r[colname][:error] = false end end @array << r end @array end |
#to_s ⇒ Object
134 135 136 |
# File 'lib/dreader.rb', line 134 def to_s @array.to_s end |