百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

comma

> 编程语言
开源

Comma 是一个针对 Ruby 对象的小型 CSV (即逗号分隔值) 生成扩展,可通过一个小型 DSL 无缝定义 CSV 输出格式。

518 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

Comma 是一个针对 Ruby 对象的小型 CSV (即逗号分隔值) 生成扩展,可通过一个小型 DSL 无缝定义 CSV 输出格式。

Comma

A library for generating comma-separated values (CSV) from Ruby objects, arrays, and supported ORM collections.

Getting Started

Prerequisites

You need Ruby 3.1 or later.

For Rails / ActiveRecord integration, this repository is currently tested against ActiveRecord and Rails 7.1 and 7.2 on Ruby 3.1 through 4.0, and Rails 8.0 and 8.1 on Ruby 3.2 through 4.0 (Rails 8.x requires Ruby >= 3.2).

Installing

Comma is distributed as a gem, best installed via Bundler.

Include the gem in your Gemfile:

ruby
gem 'comma', '~> 5.1.0'

Or, if you want to live life on the edge, you can get master from the main comma repository:

ruby
gem 'comma', git: 'https://github.com/comma-csv/comma.git'

Then, run bundle install.

Usage

Define a CSV format on your object with comma. Calling to_comma on a single object returns the row values, while calling it on an array or supported collection generates CSV output:

ruby
class User
  attr_reader :first_name, :last_name

  def initialize(first_name, last_name)
    @first_name = first_name
    @last_name = last_name
  end

  comma do
    first_name
    last_name
    full_name 'Name'
  end

  def full_name
    "#{first_name} #{last_name}".strip
  end
end

user = User.new('Ada', 'Lovelace')
user.to_comma
# => ["Ada", "Lovelace", "Ada Lovelace"]

users = [User.new('Ada', 'Lovelace'), User.new('Grace', 'Hopper')]
users.to_comma
# => "First name,Last name,Name\nAda,Lovelace,Ada Lovelace\nGrace,Hopper,Grace Hopper\n"

You can also define named styles and select them when generating CSV:

ruby
class User
  comma :short do
    first_name
    last_name
  end
end

users.to_comma(:short)

Dynamic columns

Formats are evaluated separately to produce headers and to produce each row. For CSVs whose columns are configured at runtime, resolve and order the full field-definition list before declaring the comma format, and keep that format isolated to the current export so another export cannot redefine it midway through generation.

The following self-contained example uses Struct rather than Ruby 3.2+'s Data. It resolves the runtime field list once, attaches the format to a row class created only for that export, emits headers from field.label, and reads each value from the wrapped user's associated dynamic values via field.key:

…

In the example, field.label supplies each runtime header and row.user.dynamic_value(field.key) reads the matching value from the associated dynamic_values records.

When field definitions come from tenant or platform configuration, fetch and order them once for the requested export before the comma block is defined. That lets header generation and row generation iterate over the same field list, preserving header/value alignment. Creating row_class inside export_users also isolates the :export format to that one CSV, so a simultaneous export builds its own class instead of overwriting the format being used here.

In Rails controllers, requiring the gem registers render csv: support:

ruby
def index
  render csv: User.all
end

See the wiki for more usage examples.

Running the tests

Install dependencies:

bash
bundle install
bundle exec appraisal install

Run the default test suite and linter:

bash
bundle exec rspec spec
bundle exec rubocop -P

Run a single example:

bash
bundle exec rspec spec/comma/comma_spec.rb:205

To run the test suite across the Rails / ActiveRecord gemfile matrix, this repository uses Appraisal:

bash
bundle exec appraisal rake spec

You can also run a specific spec under one appraisal:

bash
bundle exec appraisal rails7.1.6 bundle exec rspec spec/controllers/users_controller_spec.rb

Contributing

Please make sure bundle exec rspec spec, bundle exec rubocop -P, and any relevant bundle exec appraisal ... commands pass before opening a pull request.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

  • Marcus Crafter - Initial work
  • Tom Meier - Initial work
  • Eito Katagiri

License

This project is licensed under the MIT License - see the MIT-LICENSE file for details.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubycsvruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言