Skip to content

Unify collection #to_comma extensions #165

Description

@eitoball

Summary

Replace duplicated #to_comma implementations on Array, Mongoid::Criteria, and DataMapper::Collection with a shared module.

Problem

Three files implement the same one-liner pattern:

def to_comma(style = :default)
  Comma::Generator.new(self, style).run(:each)
end

Locations:

  • lib/comma/array.rb
  • lib/comma/mongoid.rb
  • lib/comma/data_mapper_collection.rb

ActiveRecord::Relation (lib/comma/relation.rb) is different — it chooses :find_each vs :each based on limit/order and logs a warning. That logic should stay separate.

Proposed approach

# lib/comma/collection_export.rb
module Comma
  module CollectionExport
    def to_comma(style = :default, iterator: :each)
      Comma::Generator.new(self, style).run(iterator)
    end
  end
end

Then:

class Array
  include Comma::CollectionExport
end

Same for Mongoid and DataMapper hooks.

Files likely involved

  • New: lib/comma/collection_export.rb
  • lib/comma/array.rb
  • lib/comma/mongoid.rb
  • lib/comma/data_mapper_collection.rb
  • lib/comma.rb (require new file)
  • Existing collection specs (comma_spec, mongoid_spec, data_mapper_collection_spec)

Acceptance criteria

  • Array#to_comma, Mongoid criteria, and DataMapper collection behavior unchanged
  • ActiveRecord::Relation#to_comma unchanged (still uses custom iterator)
  • No public API change
  • Duplicated #to_comma bodies removed from three files

Labels (suggested)

refactor, good first issue

Depends on

None.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions