dreader 1.1.2 → 1.2.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.org +8 -6
- data/README.org +48 -21
- data/dreader.gemspec +2 -2
- data/examples/age/ages.txt +10 -0
- data/examples/template/birthdays.xlsx +0 -0
- data/lib/dreader/column.rb +2 -0
- data/lib/dreader/engine.rb +46 -46
- data/lib/dreader/options.rb +10 -0
- data/lib/dreader/util.rb +21 -0
- data/lib/dreader/version.rb +3 -1
- data/lib/dreader.rb +2 -1
- metadata +7 -15
- data/examples/age_csv/Birthdays-TabSeparated.csv +0 -13
- data/examples/age_csv/Birthdays.csv +0 -13
- data/examples/age_csv/age.rb +0 -55
- data/examples/age_noext/Birthdays +0 -0
- data/examples/age_noext/Birthdays-xlsx +0 -0
- data/examples/age_noext/Birthdays-xlsx-with-wrong-extension.xls +0 -0
- data/examples/age_noext/age.rb +0 -73
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 520ff1f682a1b747037ccc7fb1aa13d0619dad6118690552897471cbaf53a580
|
4
|
+
data.tar.gz: 10feef9edfc5511527aecbbcc297ecdb2a05b3f2f8f0268038bc41a55974bf17
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80e982f26d152b30ff25d57180d97139f97d40423ab623754d7c514a42f5e69fbd06f7f882fe85034880f7a3dd4a48e89c63b362dedd5f9dd2e955c4125036f0
|
7
|
+
data.tar.gz: 0df7a5c61ce2a4f72f076fdfa44a2487bd22d1f1b86862aba4cf6b3f858eb5d632a4237cd85be50f262726cf2001d98a6842c1bda157cab8046b93b60e2f7061
|
data/CHANGELOG.org
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
#+TITLE: Changelog
|
2
2
|
|
3
|
-
* Version 1.1
|
4
|
-
** Fixes an issue with the :extension option
|
3
|
+
* Version 1.2.1 - <2025-08-26 Tue>
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
- Back to Github
|
6
|
+
- Fixes
|
7
|
+
|
8
|
+
* Version 1.2.0 - <2023-12-29 Fri>
|
9
|
+
|
10
|
+
** Adds support for type in columns
|
11
|
+
** (Developer) Removes Rubocop Warning
|
10
12
|
|
11
13
|
* Version 1.1.1 - <2023-10-16 Mon>
|
12
14
|
** Adds option :extension
|
data/README.org
CHANGED
@@ -165,13 +165,12 @@ Require =dreader= and declare a class which extends =Dreader::Engine=:
|
|
165
165
|
end
|
166
166
|
#+END_EXAMPLE
|
167
167
|
|
168
|
-
|
168
|
+
In the class specify parsing option, using the following syntax:
|
169
169
|
|
170
170
|
#+BEGIN_EXAMPLE ruby
|
171
171
|
options do
|
172
172
|
filename 'example.ods'
|
173
|
-
|
174
|
-
extension :ods
|
173
|
+
extension ".ods"
|
175
174
|
|
176
175
|
sheet 'Sheet 1'
|
177
176
|
|
@@ -191,10 +190,10 @@ where:
|
|
191
190
|
to supply a filename when loading the file (see =read=, below). *Use
|
192
191
|
=.tsv= for tab-separated files.*
|
193
192
|
- (optional) =extension= overrides or specify the extension of =filename=.
|
194
|
-
Takes as input a
|
195
|
-
|
196
|
-
|
197
|
-
|
193
|
+
Takes as input the extension preceded by a "." (e.g., ".xlsx"). Notice that
|
194
|
+
**value of this option is not appended to filename** (see =read= below).
|
195
|
+
Filename must thus be a valid reference to a file in the file system. This
|
196
|
+
option is useful in one of these two circumstances:
|
198
197
|
1. When =filename= has no extension
|
199
198
|
2. When you want to override the extension of the filename, e.g., to force
|
200
199
|
reading a "file.csv" as a tab separated file
|
@@ -204,6 +203,10 @@ where:
|
|
204
203
|
will rely on =roo= to determine the last row. This is useful for
|
205
204
|
those files in which you only want to process some of the content or
|
206
205
|
contain "garbage" after the records.
|
206
|
+
- (optional) =date_format= specifies the date format, using the notation
|
207
|
+
understood by =strptime=. It is used only when the column declaration
|
208
|
+
contains a type specification (e.g., the column declaration of one or more
|
209
|
+
columns is in the form =[<column>, :date]=
|
207
210
|
- (optional) =sheet= is the sheet name or number to read from. If not
|
208
211
|
specified, the first (default) sheet is used
|
209
212
|
- (optional) =debug= specifies that we are debugging
|
@@ -243,18 +246,43 @@ There are two notations:
|
|
243
246
|
The reference to a column can either be a letter or a number. First column
|
244
247
|
is ='A'= or =1=.
|
245
248
|
|
246
|
-
|
249
|
+
Optionally, the reference to the column can be an array. In this case, the
|
250
|
+
first element of the array is the reference to the column and the second
|
251
|
+
argument being its type, that is, any of =:integer, :float,
|
252
|
+
:big_decimal, :date=:
|
247
253
|
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
254
|
+
#+begin_example ruby
|
255
|
+
# First notation, colref is put in the block
|
256
|
+
column({ name: ['A', :date] })
|
257
|
+
#+end_example
|
258
|
+
|
259
|
+
The effect of this declaration is introducing a =process= directive which
|
260
|
+
takes care of converting the input into the declared type. That is, the
|
261
|
+
notation above is a shortcut for:
|
262
|
+
|
263
|
+
#+begin_example ruby
|
264
|
+
# First notation, colref is put in the block
|
265
|
+
column({ name: 'A' } do
|
266
|
+
process { |value| Date.strptime(value, <the value of the option date_format>) }
|
267
|
+
end
|
268
|
+
#+end_example
|
269
|
+
|
270
|
+
The =column= declaration can contain various Ruby blocks:
|
271
|
+
|
272
|
+
- one or more =check_raw= block. The =check_raw= blocks are run in sequence,
|
273
|
+
to check data as read from the input file. They can be used, for instance,
|
274
|
+
to verify presence of a value in the input file.
|
275
|
+
*Check must return true if there are no errors: any other value (e.g. an
|
276
|
+
array of messages) is considered an error.*
|
252
277
|
- =process= can be used to transform data into something closer to the input
|
253
278
|
data required for the importing (e.g., it can be used for downcase or
|
254
279
|
strip a string)
|
255
|
-
- one or more =check= block
|
256
|
-
|
257
|
-
|
280
|
+
- one or more =check= block. The =check= blocks are run in sequence on the
|
281
|
+
processed data (that is the output of =process=, to check for errors. They
|
282
|
+
can be used, for instance, to check that a model built with =process= is
|
283
|
+
valid.
|
284
|
+
*Check must return true if there are no errors: any other value
|
285
|
+
(e.g. an array of messages) is considered an error.*
|
258
286
|
|
259
287
|
#+begin_example
|
260
288
|
column({ name: 'A' }) do
|
@@ -265,9 +293,9 @@ The =column= declaration can contain Ruby blocks:
|
|
265
293
|
#+end_example
|
266
294
|
|
267
295
|
#+begin_quote
|
268
|
-
|
269
|
-
|
270
|
-
|
296
|
+
If you declare more than a check block of the same type per column, use a
|
297
|
+
unique symbol to distinguish the blocks or the error messages will be
|
298
|
+
overwritten.
|
271
299
|
#+end_quote
|
272
300
|
|
273
301
|
#+begin_example
|
@@ -398,7 +426,6 @@ See [[file:examples/wikipedia_us_cities/us_cities_bulk_declare.rb][us_cities_bul
|
|
398
426
|
hash from the code block.
|
399
427
|
#+END_NOTES
|
400
428
|
|
401
|
-
|
402
429
|
*** Add virtual columns
|
403
430
|
|
404
431
|
Sometimes it is convenient to aggregate or otherwise manipulate the data
|
@@ -465,8 +492,8 @@ A typical scenario works as follows:
|
|
465
492
|
# examples:
|
466
493
|
# i.read
|
467
494
|
# i.read filename: "example.ods"
|
468
|
-
# i.read filename: "example.ods", extension:
|
469
|
-
# i.read filename: "example", extension:
|
495
|
+
# i.read filename: "example.ods", extension: ".ods"
|
496
|
+
# i.read filename: "example", extension: ".ods"
|
470
497
|
# (the line above opens the file "example" as an Open Document Spreasdheet)
|
471
498
|
i.read
|
472
499
|
|
data/dreader.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Adolfo Villafiorita"]
|
10
10
|
spec.email = ["adolfo@shair.tech"]
|
11
11
|
|
12
|
-
spec.summary = %q{
|
12
|
+
spec.summary = %q{Porcelain on top of Roo for declarative importing of CSV and spreadheet files}
|
13
13
|
spec.description = %q{Use this gem to specify the structure of some tabular data
|
14
14
|
you want to process. The input data can be in CSV, LibreOffice, and Excel. Each row
|
15
15
|
can then be passed to a block of code you define.
|
@@ -19,7 +19,7 @@ Rails application, but the gem can used in any Ruby application.
|
|
19
19
|
|
20
20
|
The gem should be relatively easy to use, despite its name. (Dread
|
21
21
|
stands for *d*ata *r*eader)}
|
22
|
-
spec.homepage = "https://
|
22
|
+
spec.homepage = "https://https://github.com/avillafiorita/dreader"
|
23
23
|
spec.license = "MIT"
|
24
24
|
|
25
25
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
Binary file
|
data/lib/dreader/column.rb
CHANGED
data/lib/dreader/engine.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal:true
|
2
|
+
|
1
3
|
require "roo"
|
2
4
|
require "logger"
|
3
5
|
require "fast_excel"
|
@@ -10,6 +12,8 @@ module Dreader
|
|
10
12
|
#
|
11
13
|
# This is where the real stuff begins
|
12
14
|
#
|
15
|
+
# TODO: FIX Metric?
|
16
|
+
# rubocop:disable Module/ModuleLength
|
13
17
|
module Engine
|
14
18
|
# the options we passed
|
15
19
|
attr_accessor :declared_options
|
@@ -21,7 +25,7 @@ module Dreader
|
|
21
25
|
attr_accessor :declared_virtual_columns
|
22
26
|
# the mapping rules
|
23
27
|
attr_accessor :declared_mapping
|
24
|
-
|
28
|
+
|
25
29
|
# the data we read
|
26
30
|
attr_reader :table
|
27
31
|
|
@@ -46,13 +50,13 @@ module Dreader
|
|
46
50
|
|
47
51
|
@declared_columns ||= []
|
48
52
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
53
|
+
@declared_columns << (
|
54
|
+
if name.instance_of?(Hash)
|
55
|
+
columns(name, &block)
|
56
|
+
else
|
57
|
+
column.to_hash.merge({ name: })
|
58
|
+
end
|
59
|
+
)
|
56
60
|
end
|
57
61
|
|
58
62
|
# define a DSL for multiple column specification (bulk_declare)
|
@@ -60,7 +64,7 @@ module Dreader
|
|
60
64
|
# - hash is a hash in the form { symbolic_name: colref }
|
61
65
|
#
|
62
66
|
# i.bulk_declare {name: "B", age: "C"} is equivalent to:
|
63
|
-
#
|
67
|
+
#
|
64
68
|
# i.column :name do
|
65
69
|
# colref "B"
|
66
70
|
# end
|
@@ -89,9 +93,18 @@ module Dreader
|
|
89
93
|
# end
|
90
94
|
# end
|
91
95
|
def columns(hash, &block)
|
92
|
-
hash.
|
96
|
+
hash.each do |key, value|
|
93
97
|
column = Column.new
|
94
|
-
|
98
|
+
|
99
|
+
if value.instance_of?(Array)
|
100
|
+
column.colref value[0]
|
101
|
+
column.process do |string|
|
102
|
+
Util.convert(string, value[1], @declared_options)
|
103
|
+
end
|
104
|
+
else
|
105
|
+
column.colref value
|
106
|
+
end
|
107
|
+
|
95
108
|
column.instance_eval(&block) if block
|
96
109
|
|
97
110
|
@declared_columns ||= []
|
@@ -112,10 +125,10 @@ module Dreader
|
|
112
125
|
# they are defined
|
113
126
|
def virtual_column(name, &block)
|
114
127
|
column = Column.new
|
115
|
-
column.instance_eval
|
128
|
+
column.instance_eval(&block)
|
116
129
|
|
117
130
|
@declared_virtual_columns ||= []
|
118
|
-
@declared_virtual_columns << column.to_hash.merge({ name:
|
131
|
+
@declared_virtual_columns << column.to_hash.merge({ name: })
|
119
132
|
end
|
120
133
|
|
121
134
|
# define what we do with each line we read
|
@@ -215,10 +228,10 @@ module Dreader
|
|
215
228
|
coord = coord(row_number, colspec[:colref], cell)
|
216
229
|
begin
|
217
230
|
processed = colspec[:process] ? colspec[:process].call(cell) : cell
|
218
|
-
@logger.debug "[dreader] #{colname} process #{coord} yields '#{processed}' (#{processed.class})"
|
231
|
+
@logger.debug "[dreader] '#{colname}' process @ #{coord} yields '#{processed}' (#{processed.class})"
|
219
232
|
r[colname][:value] = processed
|
220
233
|
rescue => e
|
221
|
-
@logger.error "[dreader] #{colname} process #{coord} raises an exception"
|
234
|
+
@logger.error "[dreader] '#{colname}' process @ #{coord} raises an exception"
|
222
235
|
raise e
|
223
236
|
end
|
224
237
|
|
@@ -271,7 +284,7 @@ module Dreader
|
|
271
284
|
@declared_virtual_columns.each do |virtualcol|
|
272
285
|
colname = virtualcol[:name]
|
273
286
|
row[colname] = { virtual: true }
|
274
|
-
|
287
|
+
|
275
288
|
check_data(virtualcol[:checks_raw], row, colname, full_row: true)
|
276
289
|
|
277
290
|
begin
|
@@ -395,49 +408,36 @@ module Dreader
|
|
395
408
|
|
396
409
|
private
|
397
410
|
|
398
|
-
# list of keys we support in options. We remove them when reading
|
399
|
-
# the CSV file
|
400
|
-
OPTION_KEYS = %i[
|
401
|
-
filename extension sheet first_row last_row
|
402
|
-
logger logger_level
|
403
|
-
debug
|
404
|
-
]
|
405
|
-
|
406
411
|
def open_spreadsheet(options)
|
407
412
|
filename = options[:filename]
|
408
|
-
|
409
|
-
extension = options[:extension] || File.extname(filename).downcase[1..-1]&.to_sym
|
413
|
+
ext = options[:extension] || File.extname(filename)
|
410
414
|
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
if @debug
|
415
|
-
@logger = options[:logger] || Logger.new($stdout)
|
416
|
-
@logger.debug "[dreader open_spreadsheet] filename: #{filename}"
|
417
|
-
@logger.debug "[dreader open_spreadsheet] extension: #{extension}"
|
418
|
-
end
|
419
|
-
|
420
|
-
case extension
|
421
|
-
when :csv
|
422
|
-
csv_options = @declared_options.except(*OPTION_KEYS)
|
415
|
+
case ext
|
416
|
+
when ".csv"
|
417
|
+
csv_options = @declared_options.except(*Options::NON_CSV_KEYS)
|
423
418
|
Roo::CSV.new(filename, csv_options:)
|
424
|
-
when
|
425
|
-
csv_options = @declared_options.except(*
|
419
|
+
when ".tsv"
|
420
|
+
csv_options = @declared_options.except(*Options::NON_CSV_KEYS).merge({ col_sep: "\t" })
|
426
421
|
Roo::CSV.new(filename, csv_options:)
|
427
|
-
when
|
428
|
-
Roo::
|
422
|
+
when ".ods"
|
423
|
+
Roo::OpenOffice.new(filename)
|
424
|
+
when ".xls"
|
425
|
+
Roo::Excel.new(filename)
|
426
|
+
when ".xlsx"
|
427
|
+
Roo::Excelx.new(filename)
|
429
428
|
else
|
430
|
-
raise "Unknown extension: #{ext}
|
429
|
+
raise "Unknown extension: #{ext}"
|
431
430
|
end
|
432
431
|
end
|
433
432
|
|
434
433
|
def colref_to_i(colref)
|
435
434
|
return colref if colref.instance_of?(Integer)
|
435
|
+
|
436
436
|
value = 0
|
437
437
|
power = 1
|
438
438
|
colref.to_s.reverse.split("").map do |char|
|
439
|
-
value
|
440
|
-
power
|
439
|
+
value += power * (1 + char.ord - 'A'.ord)
|
440
|
+
power *= power
|
441
441
|
end
|
442
442
|
value - 1
|
443
443
|
end
|
@@ -460,7 +460,7 @@ module Dreader
|
|
460
460
|
#
|
461
461
|
# - debug :: a boolean
|
462
462
|
def check_data(check_spec, hash, colname, full_row: false)
|
463
|
-
check_spec.each do |error_message, check_function|
|
463
|
+
(check_spec || []).each do |error_message, check_function|
|
464
464
|
# here we extract values by distinguishing whether the hash is that of
|
465
465
|
# column or that of a row
|
466
466
|
if full_row
|
data/lib/dreader/options.rb
CHANGED
@@ -1,6 +1,16 @@
|
|
1
|
+
# frozen_string_literal:true
|
2
|
+
|
1
3
|
module Dreader
|
2
4
|
# service class to implement the options DSL language
|
3
5
|
class Options
|
6
|
+
# List of keys we support in options and which are not understood by the
|
7
|
+
# CSV reader
|
8
|
+
#
|
9
|
+
# We remove them when reading the CSV file
|
10
|
+
NON_CSV_KEYS = %i[
|
11
|
+
filename sheet first_row last_row logger logger_level date_format
|
12
|
+
].freeze
|
13
|
+
|
4
14
|
def initialize
|
5
15
|
@attributes = {}
|
6
16
|
end
|
data/lib/dreader/util.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Dreader
|
2
4
|
# Utilities function to simplify importing data into
|
3
5
|
# ActiveRecords
|
@@ -82,5 +84,24 @@ module Dreader
|
|
82
84
|
error[:row] == row && (col.nil? || error[:col] == col)
|
83
85
|
end
|
84
86
|
end
|
87
|
+
|
88
|
+
#
|
89
|
+
# Convert a string to a given type
|
90
|
+
#
|
91
|
+
def self.convert(value, type, options = {})
|
92
|
+
case type
|
93
|
+
when :integer
|
94
|
+
value.to_i
|
95
|
+
when :float
|
96
|
+
value.to_f
|
97
|
+
when :big_decimal
|
98
|
+
BigDecimal(value)
|
99
|
+
when :date
|
100
|
+
date_format = options[:date_format] || "%d/%m/%Y"
|
101
|
+
Date.strptime(value, date_format)
|
102
|
+
else
|
103
|
+
value
|
104
|
+
end
|
105
|
+
end
|
85
106
|
end
|
86
107
|
end
|
data/lib/dreader/version.rb
CHANGED
data/lib/dreader.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dreader
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adolfo Villafiorita
|
8
|
-
autorequire:
|
9
8
|
bindir: exe
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
12
11
|
dependencies:
|
13
12
|
- !ruby/object:Gem::Dependency
|
14
13
|
name: roo
|
@@ -108,16 +107,11 @@ files:
|
|
108
107
|
- dreader.gemspec
|
109
108
|
- examples/age/Birthdays.ods
|
110
109
|
- examples/age/age.rb
|
111
|
-
- examples/
|
112
|
-
- examples/age_csv/Birthdays.csv
|
113
|
-
- examples/age_csv/age.rb
|
114
|
-
- examples/age_noext/Birthdays
|
115
|
-
- examples/age_noext/Birthdays-xlsx
|
116
|
-
- examples/age_noext/Birthdays-xlsx-with-wrong-extension.xls
|
117
|
-
- examples/age_noext/age.rb
|
110
|
+
- examples/age/ages.txt
|
118
111
|
- examples/age_with_multiple_checks/Birthdays.ods
|
119
112
|
- examples/age_with_multiple_checks/age_with_multiple_checks.rb
|
120
113
|
- examples/local_vars/local_vars.rb
|
114
|
+
- examples/template/birthdays.xlsx
|
121
115
|
- examples/template/template_generation.rb
|
122
116
|
- examples/wikipedia_big_us_cities/big_us_cities.rb
|
123
117
|
- examples/wikipedia_big_us_cities/cities_by_state.ods
|
@@ -130,11 +124,10 @@ files:
|
|
130
124
|
- lib/dreader/options.rb
|
131
125
|
- lib/dreader/util.rb
|
132
126
|
- lib/dreader/version.rb
|
133
|
-
homepage: https://
|
127
|
+
homepage: https://https://github.com/avillafiorita/dreader
|
134
128
|
licenses:
|
135
129
|
- MIT
|
136
130
|
metadata: {}
|
137
|
-
post_install_message:
|
138
131
|
rdoc_options: []
|
139
132
|
require_paths:
|
140
133
|
- lib
|
@@ -149,8 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
149
142
|
- !ruby/object:Gem::Version
|
150
143
|
version: '0'
|
151
144
|
requirements: []
|
152
|
-
rubygems_version: 3.
|
153
|
-
signing_key:
|
145
|
+
rubygems_version: 3.6.7
|
154
146
|
specification_version: 4
|
155
|
-
summary:
|
147
|
+
summary: Porcelain on top of Roo for declarative importing of CSV and spreadheet files
|
156
148
|
test_files: []
|
@@ -1,13 +0,0 @@
|
|
1
|
-
Name Date of birth
|
2
|
-
Forest Whitaker July 15, 1961
|
3
|
-
Daniel Day-Lewis April 29, 1957
|
4
|
-
Sean Penn August 17, 1960
|
5
|
-
Jeff Bridges December 4, 1949
|
6
|
-
Colin Firth September 10, 1960
|
7
|
-
Jean Dujardin June 19, 1972
|
8
|
-
Daniel Day-Lewis April 29, 1957
|
9
|
-
Matthew McConaughey November 4, 1969
|
10
|
-
Eddie Redmayne January 6, 1982
|
11
|
-
Leonardo DiCaprio November 11, 1974
|
12
|
-
Casey Affleck August 12, 1975
|
13
|
-
Gary Oldman March 21, 1958
|
@@ -1,13 +0,0 @@
|
|
1
|
-
Name,Date of birth
|
2
|
-
Forest Whitaker,"July 15, 1961"
|
3
|
-
Daniel Day-Lewis,"April 29, 1957"
|
4
|
-
Sean Penn,"August 17, 1960"
|
5
|
-
Jeff Bridges,"December 4, 1949"
|
6
|
-
Colin Firth,"September 10, 1960"
|
7
|
-
Jean Dujardin,"June 19, 1972"
|
8
|
-
Daniel Day-Lewis,"April 29, 1957"
|
9
|
-
Matthew McConaughey,"November 4, 1969"
|
10
|
-
Eddie Redmayne,"January 6, 1982"
|
11
|
-
Leonardo DiCaprio,"November 11, 1974"
|
12
|
-
Casey Affleck,"August 12, 1975"
|
13
|
-
Gary Oldman,"March 21, 1958"
|
data/examples/age_csv/age.rb
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
require "dreader"
|
2
|
-
|
3
|
-
class Reader
|
4
|
-
extend Dreader::Engine
|
5
|
-
|
6
|
-
options do
|
7
|
-
first_row 2
|
8
|
-
debug true
|
9
|
-
end
|
10
|
-
|
11
|
-
column :name do
|
12
|
-
doc "A is the name string"
|
13
|
-
colref 'A'
|
14
|
-
end
|
15
|
-
|
16
|
-
column :birthdate do
|
17
|
-
doc "Birthdate contains a full date (i.e., including the year)"
|
18
|
-
colref 'B'
|
19
|
-
|
20
|
-
process do |c|
|
21
|
-
Date.parse(c)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
virtual_column :age do
|
26
|
-
process do |row|
|
27
|
-
birthdate = row[:birthdate][:value]
|
28
|
-
birthday = Date.new(Date.today.year, birthdate.month, birthdate.day)
|
29
|
-
today = Date.today
|
30
|
-
|
31
|
-
[0, today.year - birthdate.year - (birthday < today ? 1 : 0)].max
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
mapping do |row|
|
36
|
-
r = Dreader::Util.simplify(row)
|
37
|
-
puts "#{r[:name]} is #{r[:age]} years old (born on #{r[:birthdate]})"
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
i = Reader
|
42
|
-
i.read filename: "Birthdays.csv", mapping: true
|
43
|
-
|
44
|
-
i.read filename: "Birthdays-TabSeparated.csv", extension: :tsv, mapping: true
|
45
|
-
|
46
|
-
#
|
47
|
-
# Here we can do further processing on the data
|
48
|
-
#
|
49
|
-
File.open("ages.txt", "w") do |file|
|
50
|
-
i.table.each do |row|
|
51
|
-
unless row[:row_errors].any?
|
52
|
-
file.puts "#{row[:name][:value]} #{row[:age][:value]}"
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
Binary file
|
Binary file
|
Binary file
|
data/examples/age_noext/age.rb
DELETED
@@ -1,73 +0,0 @@
|
|
1
|
-
require "dreader"
|
2
|
-
|
3
|
-
class Reader
|
4
|
-
extend Dreader::Engine
|
5
|
-
|
6
|
-
options do
|
7
|
-
first_row 2
|
8
|
-
debug true
|
9
|
-
extension :ods
|
10
|
-
end
|
11
|
-
|
12
|
-
column :name do
|
13
|
-
doc "A is the name string"
|
14
|
-
colref 'A'
|
15
|
-
end
|
16
|
-
|
17
|
-
column :birthdate do
|
18
|
-
doc "Birthdate contains a full date (i.e., including the year)"
|
19
|
-
colref 'B'
|
20
|
-
|
21
|
-
process do |c|
|
22
|
-
Date.parse(c)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
virtual_column :age do
|
27
|
-
process do |row|
|
28
|
-
birthdate = row[:birthdate][:value]
|
29
|
-
birthday = Date.new(Date.today.year, birthdate.month, birthdate.day)
|
30
|
-
today = Date.today
|
31
|
-
|
32
|
-
[0, today.year - birthdate.year - (birthday < today ? 1 : 0)].max
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
mapping do |row|
|
37
|
-
r = Dreader::Util.simplify(row)
|
38
|
-
puts "#{r[:name]} is #{r[:age]} years old (born on #{r[:birthdate]})"
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
puts
|
43
|
-
puts "*****************************************************************"
|
44
|
-
puts "Reading ODS with no extension, using extension set in the options"
|
45
|
-
puts "*****************************************************************"
|
46
|
-
puts
|
47
|
-
|
48
|
-
i = Reader
|
49
|
-
i.read filename: "Birthdays"
|
50
|
-
i.virtual_columns
|
51
|
-
i.mappings
|
52
|
-
|
53
|
-
puts
|
54
|
-
puts "*****************************************************************"
|
55
|
-
puts "Reading XLSX with wrong extension, overriding existing extension"
|
56
|
-
puts "*****************************************************************"
|
57
|
-
puts
|
58
|
-
|
59
|
-
i = Reader
|
60
|
-
i.read filename: "Birthdays-xlsx-with-wrong-extension.xls", extension: :xlsx
|
61
|
-
i.virtual_columns
|
62
|
-
i.mappings
|
63
|
-
|
64
|
-
puts
|
65
|
-
puts "*****************************************************************"
|
66
|
-
puts "Reading XLSX with no extension"
|
67
|
-
puts "*****************************************************************"
|
68
|
-
puts
|
69
|
-
|
70
|
-
i = Reader
|
71
|
-
i.read filename: "Birthdays-xlsx", extension: :xlsx
|
72
|
-
i.virtual_columns
|
73
|
-
i.mappings
|