diff options
Diffstat (limited to 'gems/csv_builder/README.md')
-rw-r--r-- | gems/csv_builder/README.md | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/gems/csv_builder/README.md b/gems/csv_builder/README.md new file mode 100644 index 00000000000..37dde4b334c --- /dev/null +++ b/gems/csv_builder/README.md @@ -0,0 +1,48 @@ +# CsvBuilder + +## Usage + +Generate a CSV given a collection and a mapping. + +```ruby +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: + +```ruby +CsvBuilder.new(@posts, columns, [:author, :comments]).render +``` + +### SingleBatch builder + +When the collection is an array or enumerable you can use: + +```ruby +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. + +```ruby +CsvBuilder::Stream.new(@posts, columns).render(1_000) +``` + +## Development + +Follow the GitLab [gems development guidelines](../../doc/development/gems.md). |