aboutsummaryrefslogtreecommitdiffstats
path: root/debian/patches/ruby3.0_compatibility.patch
blob: c35dc0b655d11d60c84cbe561594e338a70627ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
From: taichi <taichi730@gmail.com>
Subject: Grab two patches from upstream to fix compatibility with Ruby 3.0.
 [PATCH] fixed warnings caused by keyword argument change
 [PATCH] adapted for obsoletion of URI.encode method

Origin: upstream
Bug-Debian: https://bugs.debian.org/996371

---
 lib/roo/csv.rb         |   27 +++++++++++++++++++++++----
 lib/roo/spreadsheet.rb |   10 ++++++++--
 2 files changed, 31 insertions(+), 6 deletions(-)

--- a/lib/roo/csv.rb	2021-10-30 21:50:24.101318627 -0400
+++ b/lib/roo/csv.rb	2021-10-30 21:50:24.089318711 -0400
@@ -90,17 +90,36 @@
     def each_row(options, &block)
       if uri?(filename)
         each_row_using_tempdir(options, &block)
-      elsif is_stream?(filename_or_stream)
-        ::CSV.new(filename_or_stream, options).each(&block)
       else
-        ::CSV.foreach(filename, options, &block)
+        csv_foreach(filename_or_stream, options, &block)
       end
     end
 
     def each_row_using_tempdir(options, &block)
       ::Dir.mktmpdir(Roo::TEMP_PREFIX, ENV["ROO_TMP"]) do |tmpdir|
         tmp_filename = download_uri(filename, tmpdir)
-        ::CSV.foreach(tmp_filename, options, &block)
+        csv_foreach(tmp_filename, options, &block)
+      end
+    end
+
+    # From Ruby 2.5, options argument of CSV.new/CSV.foreach is a keyword argument.
+    # Before Ruby 2.5, that argument is a Hash.
+    # Therefore, this workaround can be removed if Ruby 2.3 and 2.4 are dropped.
+    if RUBY_VERSION >= '2.5.0'
+      def csv_foreach(path_or_io, options, &block)
+        if is_stream?(path_or_io)
+          ::CSV.new(path_or_io, **options).each(&block)
+        else
+          ::CSV.foreach(path_or_io, **options, &block)
+        end
+      end
+    else
+      def csv_foreach(path_or_io, options, &block)
+        if is_stream?(path_or_io)
+          ::CSV.new(path_or_io, options).each(&block)
+        else
+          ::CSV.foreach(path_or_io, options, &block)
+        end
       end
     end
 
--- a/lib/roo/spreadsheet.rb	2021-10-30 21:50:24.101318627 -0400
+++ b/lib/roo/spreadsheet.rb	2021-10-30 21:50:24.089318711 -0400
@@ -24,8 +24,14 @@
           options[:file_warning] = :ignore
           extension.tr('.', '').downcase.to_sym
         else
-          res = ::File.extname((path =~ /\A#{::URI::DEFAULT_PARSER.make_regexp}\z/) ? ::URI.parse(::URI.encode(path)).path : path)
-          res.tr('.', '').downcase.to_sym
+          parsed_path =
+            if path =~ /\A#{::URI::DEFAULT_PARSER.make_regexp}\z/
+              # path is 7th match
+              Regexp.last_match[7]
+            else
+              path
+            end
+          ::File.extname(parsed_path).tr('.', '').downcase.to_sym
         end
       end
     end