dreader 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 524e55af5bb94cae3f407a1602069549783e935798a638361f3c98e922ffc54d
4
- data.tar.gz: 2599e048324ccd233e3fa4a0261e134ced0a3347d27af1e978e635639b6284a8
3
+ metadata.gz: d59887423bcb1658823534ca3d8a5505899cd70a5f8d23eda235159c590cf364
4
+ data.tar.gz: b17d71ed8d7db969fcc872a3f01cb6fdfbfaff69e0030ffc304ebc481a15675d
5
5
  SHA512:
6
- metadata.gz: e8a78531d96ef35f9272a38daa5327620026dd66c98529710901d4c5994b9e5369308612cc79d1e0998d46dbb9dd6d26c448ddc4265dd536f1e61a4fc50fb885
7
- data.tar.gz: de71caed5d3df79d0d456b080a72a0edc035ef4e46906aac3f94295b07d57b956554a9d9e07b4b6195c5c09df0badfa9202122d7b6e0568cf89569b6a2277d28
6
+ metadata.gz: 1407751e4baa35d4c6644cc9c9ebea7a8185febd2211e6b76c86a3001e8972d58061b6946a178dd007043c897a9943d41971e0868dab2e9e332350a5436bfff1
7
+ data.tar.gz: da163158d92a1618de0218f5285fe189f3ec5fe78b0d815c6b873574175c817ef4134725ff70a174b0206fb0da1770f9888044b076dbfc8e62b79f22e10930a6
data/CHANGELOG.org CHANGED
@@ -1,5 +1,13 @@
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
+
3
11
  * Version 1.1.1 - <2023-10-16 Mon>
4
12
  ** Adds option :extension
5
13
 
data/README.org CHANGED
@@ -165,12 +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
- extension ".ods"
173
+ # this optional. Use it when the file does not have an extension
174
+ extension :ods
174
175
 
175
176
  sheet 'Sheet 1'
176
177
 
@@ -190,10 +191,10 @@ where:
190
191
  to supply a filename when loading the file (see =read=, below). *Use
191
192
  =.tsv= for tab-separated files.*
192
193
  - (optional) =extension= overrides or specify the extension of =filename=.
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:
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:
197
198
  1. When =filename= has no extension
198
199
  2. When you want to override the extension of the filename, e.g., to force
199
200
  reading a "file.csv" as a tab separated file
@@ -464,8 +465,8 @@ A typical scenario works as follows:
464
465
  # examples:
465
466
  # i.read
466
467
  # i.read filename: "example.ods"
467
- # i.read filename: "example.ods", extension: ".ods"
468
- # i.read filename: "example", extension: ".ods"
468
+ # i.read filename: "example.ods", extension: :ods
469
+ # i.read filename: "example", extension: :ods
469
470
  # (the line above opens the file "example" as an Open Document Spreasdheet)
470
471
  i.read
471
472
 
@@ -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
@@ -398,28 +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
406
  def open_spreadsheet(options)
405
407
  filename = options[:filename]
406
- ext = options[:extension] || File.extname(filename)
408
+ # use the extension option or make ".CSV" into :csv
409
+ extension = options[:extension] || File.extname(filename).downcase[1..-1]&.to_sym
407
410
 
408
- case ext
409
- 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
410
422
  csv_options = @declared_options.except(*OPTION_KEYS)
411
423
  Roo::CSV.new(filename, csv_options:)
412
- when ".tsv"
424
+ when :tsv
413
425
  csv_options = @declared_options.except(*OPTION_KEYS).merge({ col_sep: "\t" })
414
426
  Roo::CSV.new(filename, csv_options:)
415
- when ".ods"
416
- Roo::OpenOffice.new(filename)
417
- when ".xls"
418
- Roo::Excel.new(filename)
419
- when ".xlsx"
420
- Roo::Excelx.new(filename)
427
+ when :ods, :xls, :xlsx
428
+ Roo::Spreadsheet.open(filename, extension:)
421
429
  else
422
- raise "Unknown extension: #{ext}"
430
+ raise "Unknown extension: #{ext}. Use the :extension option."
423
431
  end
424
432
  end
425
433
 
@@ -1,3 +1,3 @@
1
1
  module Dreader
2
- VERSION = "1.1.1"
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.1
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-31 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