Skip to content

Commit

Permalink
Fix keyword argument separation warnings on Ruby master branch in csv…
Browse files Browse the repository at this point in the history
…_serializer plugin

As Ruby 1.9 does not support **opts syntax, this requires some
extra work.  I was considering dropping Ruby 1.9 support, but
as this is the only part of Sequel that causes keyword argument
separation warnings, I'm OK keeping 1.9 support.

Remove reference to FasterCSV, which hasn't been used since 1.8
support was dropped.
  • Loading branch information
jeremyevans committed Sep 7, 2019
1 parent b143e45 commit f7dfdd0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
=== master

* Fix keyword argument separation warnings on Ruby master branch in csv_serializer plugin (jeremyevans)

* Add association_multi_add_remove plugin for adding/removing multiple associated objects in a single method call (AlexWayfer, jeremyevans) (#1641)

* Make sharding plugin integrate with server_block extension (jeremyevans)
Expand Down
33 changes: 24 additions & 9 deletions lib/sequel/plugins/csv_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ module Plugins
# # Add CSV output capability to Album class instances
# Album.plugin :csv_serializer
module CsvSerializer
CSV = Object.const_defined?(:CSV) ? ::CSV : ::FasterCSV

# Set up the column readers to do deserialization and the column writers
# to save the value in deserialized_values
def self.configure(model, opts = OPTS)
Expand All @@ -75,13 +73,27 @@ def self.configure(model, opts = OPTS)
end
end

# Avoid keyword argument separation warnings on Ruby 2.7, while still
# being compatible with 1.9.
if RUBY_VERSION >= "2.0"
instance_eval(<<-END, __FILE__, __LINE__+1)
def self.csv_call(*args, opts, &block)
CSV.send(*args, **opts, &block)
end
END
else
def self.csv_call(*args, opts, &block)
CSV.send(*args, opts, &block)
end
end

module ClassMethods
# The default opts to use when serializing model objects to CSV
attr_reader :csv_serializer_opts

# Attempt to parse an array of instances from the given CSV string
def array_from_csv(csv, opts = OPTS)
CSV.parse(csv, process_csv_serializer_opts(opts)).map do |row|
CsvSerializer.csv_call(:parse, csv, process_csv_serializer_opts(opts)).map do |row|
row = row.to_hash
row.delete(nil)
new(row)
Expand All @@ -108,7 +120,8 @@ def process_csv_serializer_opts(opts)
opts_cols = opts.delete(:columns)
opts_include = opts.delete(:include)
opts_except = opts.delete(:except)
opts[:headers] ||= Array(opts.delete(:only) || opts_cols || columns) + Array(opts_include) - Array(opts_except)
only = opts.delete(:only)
opts[:headers] ||= Array(only || opts_cols || columns) + Array(opts_include) - Array(opts_except)
opts
end

Expand All @@ -130,7 +143,7 @@ module InstanceMethods
# :headers :: The headers to use for the CSV line. Use nil for a header
# to specify the column should be ignored.
def from_csv(csv, opts = OPTS)
row = CSV.parse_line(csv, model.process_csv_serializer_opts(opts)).to_hash
row = CsvSerializer.csv_call(:parse_line, csv, model.process_csv_serializer_opts(opts)).to_hash
row.delete(nil)
set(row)
end
Expand All @@ -146,9 +159,10 @@ def from_csv(csv, opts = OPTS)
# attributes to include in the CSV output.
def to_csv(opts = OPTS)
opts = model.process_csv_serializer_opts(opts)
headers = opts[:headers]

CSV.generate(opts) do |csv|
csv << opts[:headers].map{|k| public_send(k)}
CsvSerializer.csv_call(:generate, model.process_csv_serializer_opts(opts)) do |csv|
csv << headers.map{|k| public_send(k)}
end
end
end
Expand All @@ -164,10 +178,11 @@ module DatasetMethods
def to_csv(opts = OPTS)
opts = model.process_csv_serializer_opts({:columns=>columns}.merge!(opts))
items = opts.delete(:array) || self
headers = opts[:headers]

CSV.generate(opts) do |csv|
CsvSerializer.csv_call(:generate, opts) do |csv|
items.each do |object|
csv << opts[:headers].map{|header| object.public_send(header) }
csv << headers.map{|header| object.public_send(header)}
end
end
end
Expand Down

0 comments on commit f7dfdd0

Please sign in to comment.