gitlab-ce/gems/csv_builder
GitLab Bot 27211da937 Add latest changes from gitlab-org/gitlab@master 2025-07-11 21:07:14 +00:00
..
lib Add latest changes from gitlab-org/gitlab@master 2025-01-17 12:48:37 +00:00
spec Add latest changes from gitlab-org/gitlab@master 2024-07-24 03:10:24 +00:00
.gitignore
.gitlab-ci.yml Add latest changes from gitlab-org/gitlab@master 2024-04-26 03:17:39 +00:00
.rspec
.rubocop.yml
Gemfile
Gemfile.lock Add latest changes from gitlab-org/gitlab@master 2025-07-11 21:07:14 +00:00
README.md
csv_builder.gemspec

README.md

CsvBuilder

Usage

Generate a CSV given a collection and a mapping.

columns = {
  'Title' => 'title',
  'Comment' => 'comment',
  'Author' => -> (post) { post.author.full_name }
  'Created At (UTC)' => -> (post) { post.created_at&.strftime('%Y-%m-%d %H:%M:%S') }
}

CsvBuilder.new(@posts, columns).render

When the value of the mapping is a string, a method is called with the given name on the record (for example: post.title). When the value of the mapping is a lambda, it is lazily executed.

It's possible to also pass ActiveRecord associations to preload when batching through the collection:

CsvBuilder.new(@posts, columns, [:author, :comments]).render

SingleBatch builder

When the collection is an array or enumerable you can use:

CsvBuilder::SingleBatch.new(@posts, columns).render

Stream builder

A stream builder uses a lazy and more efficient iterator and by default returns up to 100,000 records from the collection.

CsvBuilder::Stream.new(@posts, columns).render(1_000)

Development

Follow the GitLab gems development guidelines.