Skip to content

Commit

Permalink
[ruby/csv] Resolve CSV::Converters and HeaderConverters lazy
Browse files Browse the repository at this point in the history
It's for Ractor. If you want to use the built-in converters, you
should call Ractor.make_shareable(CSV::Converters) and/or
Ractor.make_shareable(CSV::HeaderConverters).

ruby/csv@b0b1325d6b
  • Loading branch information
kou committed Oct 23, 2021
1 parent 8aaa1c2 commit 39ecdab
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/csv.rb
Expand Up @@ -2575,7 +2575,7 @@ def parser_fields_converter

def build_parser_fields_converter
specific_options = {
builtin_converters: Converters,
builtin_converters_name: :Converters,
}
options = @base_fields_converter_options.merge(specific_options)
build_fields_converter(@initial_converters, options)
Expand All @@ -2587,7 +2587,7 @@ def header_fields_converter

def build_header_fields_converter
specific_options = {
builtin_converters: HeaderConverters,
builtin_converters_name: :HeaderConverters,
accept_nil: true,
}
options = @base_fields_converter_options.merge(specific_options)
Expand Down
8 changes: 6 additions & 2 deletions lib/csv/fields_converter.rb
Expand Up @@ -16,15 +16,15 @@ def initialize(options={})
@empty_value = options[:empty_value]
@empty_value_is_empty_string = (@empty_value == "")
@accept_nil = options[:accept_nil]
@builtin_converters = options[:builtin_converters]
@builtin_converters_name = options[:builtin_converters_name]
@need_static_convert = need_static_convert?
end

def add_converter(name=nil, &converter)
if name.nil? # custom converter
@converters << converter
else # named converter
combo = @builtin_converters[name]
combo = builtin_converters[name]
case combo
when Array # combo converter
combo.each do |sub_name|
Expand Down Expand Up @@ -80,5 +80,9 @@ def need_convert?
@need_static_convert or
(not @converters.empty?)
end

def builtin_converters
@builtin_converters ||= ::CSV.const_get(@builtin_converters_name)
end
end
end

0 comments on commit 39ecdab

Please sign in to comment.