From a389684a1398b69fce339df2c2927e5321d8c6a7 Mon Sep 17 00:00:00 2001 From: Eito Katagiri Date: Sat, 5 Sep 2026 20:56:09 +0900 Subject: [PATCH] Unify collection #to_comma extensions into shared module Array, Mongoid::Criteria, and DataMapper::Collection each defined an identical one-liner #to_comma. Extract it into Comma::CollectionExport and include it in all three, removing the duplication. ActiveRecord::Relation keeps its own implementation since it selects :find_each vs :each based on limit/order and logs a warning. Fixes #165 Co-Authored-By: Claude Sonnet 5 --- .rubocop_todo.yml | 3 ++- lib/comma.rb | 4 ++-- lib/comma/array.rb | 4 +--- lib/comma/collection_export.rb | 9 +++++++++ lib/comma/data_mapper_collection.rb | 4 +--- lib/comma/mongoid.rb | 4 +--- 6 files changed, 16 insertions(+), 12 deletions(-) create mode 100644 lib/comma/collection_export.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a024c1c8..690c7bbb 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -108,13 +108,14 @@ Style/BlockDelimiters: - 'spec/comma/data_extractor_spec.rb' - 'spec/comma/header_extractor_spec.rb' -# Offense count: 11 +# Offense count: 12 # Configuration parameters: AllowedConstants. Style/Documentation: Exclude: - 'spec/**/*' - 'test/**/*' - 'lib/comma/array.rb' + - 'lib/comma/collection_export.rb' - 'lib/comma/data_extractor.rb' - 'lib/comma/data_mapper_collection.rb' - 'lib/comma/extractor.rb' diff --git a/lib/comma.rb b/lib/comma.rb index 1350d1c1..3a1dd78c 100644 --- a/lib/comma.rb +++ b/lib/comma.rb @@ -11,10 +11,10 @@ module Comma }.freeze end -require 'comma/data_mapper_collection' if defined? DataMapper - require 'comma/options' require 'comma/generator' +require 'comma/collection_export' +require 'comma/data_mapper_collection' if defined? DataMapper require 'comma/array' require 'comma/object' diff --git a/lib/comma/array.rb b/lib/comma/array.rb index f1b1c505..f15906fe 100644 --- a/lib/comma/array.rb +++ b/lib/comma/array.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true class Array - def to_comma(style = :default) - Comma::Generator.new(self, style).run(:each) - end + include Comma::CollectionExport end diff --git a/lib/comma/collection_export.rb b/lib/comma/collection_export.rb new file mode 100644 index 00000000..2f88b436 --- /dev/null +++ b/lib/comma/collection_export.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module Comma + module CollectionExport + def to_comma(style = :default) + Comma::Generator.new(self, style).run(:each) + end + end +end diff --git a/lib/comma/data_mapper_collection.rb b/lib/comma/data_mapper_collection.rb index 9f613350..d05c6cce 100644 --- a/lib/comma/data_mapper_collection.rb +++ b/lib/comma/data_mapper_collection.rb @@ -3,9 +3,7 @@ if defined?(DataMapper) module DataMapper class Collection - def to_comma(style = :default) - Comma::Generator.new(self, style).run(:each) - end + include Comma::CollectionExport end end end diff --git a/lib/comma/mongoid.rb b/lib/comma/mongoid.rb index 33d7399c..fed12543 100644 --- a/lib/comma/mongoid.rb +++ b/lib/comma/mongoid.rb @@ -6,9 +6,7 @@ module Mongoid class Criteria - def to_comma(style = :default) - Comma::Generator.new(self, style).run(:each) - end + include Comma::CollectionExport end end rescue LoadError => e