dreader 1.1.0 → 1.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 40c16cce6fa4d2c813f381b7adefd8a6dd241db8b2133188aaf483c433e054ed
4
- data.tar.gz: 4a35caf1ee4703628db442a9852090dbe87055301c0963ba00580bc7eb9f4e38
3
+ metadata.gz: d59887423bcb1658823534ca3d8a5505899cd70a5f8d23eda235159c590cf364
4
+ data.tar.gz: b17d71ed8d7db969fcc872a3f01cb6fdfbfaff69e0030ffc304ebc481a15675d
5
5
  SHA512:
6
- metadata.gz: b17fc5631ac685e5117d6ef9c1199422c7ef9a347c8d05436b2fd28fbf421465a9035930c0f45adcc19bc94279bf7ff2ba8a4f7f6b932ef2befcc82743e03ee4
7
- data.tar.gz: 9a124ddd059c973710800f2b058482b13d155852e0ebb1740e54e90d92100396f9b391a17806c9e1f561c527680d0ab50d59ef3b2162f1fa9ce5da350b29fe3a
6
+ metadata.gz: 1407751e4baa35d4c6644cc9c9ebea7a8185febd2211e6b76c86a3001e8972d58061b6946a178dd007043c897a9943d41971e0868dab2e9e332350a5436bfff1
7
+ data.tar.gz: da163158d92a1618de0218f5285fe189f3ec5fe78b0d815c6b873574175c817ef4134725ff70a174b0206fb0da1770f9888044b076dbfc8e62b79f22e10930a6
data/CHANGELOG.org CHANGED
@@ -1,5 +1,18 @@
1
1
  #+TITLE: Changelog
2
2
 
3
+ * Version 1.1.2 - <2023-10-31 Tue>
4
+ ** Fixes an issue with the :extension option
5
+
6
+ - Fixes a bug related to =:extension= and adds a working example, to test
7
+ the feature
8
+ - Changes the extension from a string to a symbol. No initial dot required
9
+ any longer
10
+
11
+ * Version 1.1.1 - <2023-10-16 Mon>
12
+ ** Adds option :extension
13
+
14
+ - Adds options =extension= to the class options and to the =open_spreadsheet=
15
+ function, to be able to determine the type of a file with no extension
3
16
 
4
17
  * Version 1.1.0
5
18
  ** Fixes an issue with visibility of variables
data/README.org CHANGED
@@ -165,11 +165,13 @@ Require =dreader= and declare a class which extends =Dreader::Engine=:
165
165
  end
166
166
  #+END_EXAMPLE
167
167
 
168
- In the class specify parsing option, using the following syntax:
168
+ Specify parsing option in the class, using the following syntax:
169
169
 
170
170
  #+BEGIN_EXAMPLE ruby
171
171
  options do
172
172
  filename 'example.ods'
173
+ # this optional. Use it when the file does not have an extension
174
+ extension :ods
173
175
 
174
176
  sheet 'Sheet 1'
175
177
 
@@ -185,10 +187,17 @@ In the class specify parsing option, using the following syntax:
185
187
 
186
188
  where:
187
189
 
188
- - (optional) =filename= is the file to read. If not specified, you will
189
- have to supply a filename when loading the file (see =read=, below).
190
- The extension determines the file type. *Use =.tsv= for tab-separated
191
- files.*
190
+ - (optional) =filename= is the file to read. If not specified, you will have
191
+ to supply a filename when loading the file (see =read=, below). *Use
192
+ =.tsv= for tab-separated files.*
193
+ - (optional) =extension= overrides or specify the extension of =filename=.
194
+ Takes as input a symbol (e.g., =:xlsx=).
195
+ Notice that **value of this option is not appended to filename** (see =read=
196
+ below). Filename must thus be a valid reference to a file in the file
197
+ system. This option is useful in one of these two circumstances:
198
+ 1. When =filename= has no extension
199
+ 2. When you want to override the extension of the filename, e.g., to force
200
+ reading a "file.csv" as a tab separated file
192
201
  - (optional) =first_row= is the first line to read (use =2= if your file
193
202
  has a header)
194
203
  - (optional) =last_row= is the last line to read. If not specified, we
@@ -451,9 +460,17 @@ A typical scenario works as follows:
451
460
 
452
461
  #+BEGIN_EXAMPLE ruby
453
462
  i = Reader
463
+
464
+ # read uses the options if defined and takes the same arguments as options
465
+ # examples:
466
+ # i.read
467
+ # i.read filename: "example.ods"
468
+ # i.read filename: "example.ods", extension: :ods
469
+ # i.read filename: "example", extension: :ods
470
+ # (the line above opens the file "example" as an Open Document Spreasdheet)
454
471
  i.read
455
472
 
456
- # alternatively
473
+ # alternately
457
474
  Reader.read
458
475
  #+END_EXAMPLE
459
476
 
@@ -0,0 +1,13 @@
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
@@ -0,0 +1,13 @@
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"
@@ -0,0 +1,55 @@
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
@@ -0,0 +1,73 @@
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
@@ -150,7 +150,7 @@ module Dreader
150
150
  raise Exception
151
151
  end
152
152
 
153
- spreadsheet = open_spreadsheet (options[:filename])
153
+ spreadsheet = open_spreadsheet(options)
154
154
  sheet = spreadsheet.sheet(options[:sheet] || 0)
155
155
  first_row = options[:first_row] || 1
156
156
  last_row = options[:last_row] || sheet.last_row
@@ -324,7 +324,7 @@ module Dreader
324
324
  def compare_headers(hash = {})
325
325
  options = @declared_options.merge(hash)
326
326
 
327
- spreadsheet = open_spreadsheet(options[:filename])
327
+ spreadsheet = open_spreadsheet(options)
328
328
  sheet = spreadsheet.sheet(options[:sheet] || 0)
329
329
  header_row_number = options[:first_row] - 1 || 1
330
330
 
@@ -398,27 +398,36 @@ module Dreader
398
398
  # list of keys we support in options. We remove them when reading
399
399
  # the CSV file
400
400
  OPTION_KEYS = %i[
401
- filename sheet first_row last_row logger logger_level
401
+ filename extension sheet first_row last_row
402
+ logger logger_level
403
+ debug
402
404
  ]
403
405
 
404
- def open_spreadsheet(filename)
405
- ext = File.extname(filename)
406
+ def open_spreadsheet(options)
407
+ filename = options[:filename]
408
+ # use the extension option or make ".CSV" into :csv
409
+ extension = options[:extension] || File.extname(filename).downcase[1..-1]&.to_sym
406
410
 
407
- case ext
408
- when ".csv"
411
+
412
+ # TODO: MAKE DEBUG AND LOGGER INTO REAL CLASS VARIABLES OR MAKE LOCAL AND/OR FUNCTIONS
413
+ @debug = @declared_options.merge(options)[:debug] == true
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
409
422
  csv_options = @declared_options.except(*OPTION_KEYS)
410
423
  Roo::CSV.new(filename, csv_options:)
411
- when ".tsv"
424
+ when :tsv
412
425
  csv_options = @declared_options.except(*OPTION_KEYS).merge({ col_sep: "\t" })
413
426
  Roo::CSV.new(filename, csv_options:)
414
- when ".ods"
415
- Roo::OpenOffice.new(filename)
416
- when ".xls"
417
- Roo::Excel.new(filename)
418
- when ".xlsx"
419
- Roo::Excelx.new(filename)
427
+ when :ods, :xls, :xlsx
428
+ Roo::Spreadsheet.open(filename, extension:)
420
429
  else
421
- raise "Unknown extension: #{ext}"
430
+ raise "Unknown extension: #{ext}. Use the :extension option."
422
431
  end
423
432
  end
424
433
 
@@ -1,3 +1,3 @@
1
1
  module Dreader
2
- VERSION = "1.1.0"
2
+ VERSION = "1.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dreader
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adolfo Villafiorita
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-10-01 00:00:00.000000000 Z
11
+ date: 2023-11-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: roo
@@ -108,6 +108,13 @@ files:
108
108
  - dreader.gemspec
109
109
  - examples/age/Birthdays.ods
110
110
  - examples/age/age.rb
111
+ - examples/age_csv/Birthdays-TabSeparated.csv
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
111
118
  - examples/age_with_multiple_checks/Birthdays.ods
112
119
  - examples/age_with_multiple_checks/age_with_multiple_checks.rb
113
120
  - examples/local_vars/local_vars.rb
@@ -142,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
149
  - !ruby/object:Gem::Version
143
150
  version: '0'
144
151
  requirements: []
145
- rubygems_version: 3.3.26
152
+ rubygems_version: 3.4.10
146
153
  signing_key:
147
154
  specification_version: 4
148
155
  summary: Process and import data from cvs and spreadsheets