No release in over a year
Help handle CSVs in a more efficient way
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Project Readme

Csvbuilder::Dynamic::Columns::Exporter

Csvbuilder::Dynamic::Columns::Exporter is part of the csvbuilder-collection

The Dynamic Columns Exporter contains the implementation for exporting a collection of objects within a variable group as a CSV file.

For instance, any object belonging to categories.

Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add csvbuilder-dynamic-columns-exporter

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install csvbuilder-dynamic-columns-exporter

Usage

Let's consider a Developer with languages skill.

class UserCsvRowModel
  include Csvbuilder::Model
  include Csvbuilder::Export

  column :name, header: "Developer"

  dynamic_column :skills

  def skill(skill_name)
    source_model.skills.where(name: skill_name).exists? ? "1" : "0"
  end
end

For each Row, a method called by the singular version name of the declared Dynamic Columns will be called to determine the cell's value. In our example: skill must be implemented in the Exporter Model.

The source_model is provided and accessible within this method. In our example, it is the User. The File Exporter appends this object to the Row.

The dynamic part of the headers must be provided through the context under the same key name as the declared Dynamic Columns, here skills.

context = { skills: ["Ruby", "Python", "Javascript"] }

exporter  = Csvbuilder::Export::File.new(UserCsvExportModel, context)

exporter.headers
# => ["Developer", "Ruby", "Python", "Javascript"]

sub_context = {} # it merged to the context

exporter.generate do |csv|
  User.all.each do |user|
    csv.append_model(user, sub_context) # user here is the source_model.
  end
end

exporter.to_s
# => "Developer,Ruby,Python,Javascript\Bob,1,0,0\n"

Format Dynamic Columns

Instead of taking care of the formatting directly in the dynamic cell method, here skill, it can be delegated to format_dynamic_column_cells.

Consider the following rewrite:

class UserCsvRowModel
  include Csvbuilder::Model

  column :name, header: "Developer"

  dynamic_column :skills
end
class UserCsvExportModel < UserCsvRowModel
  include Csvbuilder::Export

  def skill(skill_name)
    source_model.skills.where(name: skill_name).exists?
  end

  class << self
    def format_dynamic_column_cells(cells, _column_name, _context)
      cells.map do |cell|
        case cell
        when true
          "1"
        when false
          "0"
        else
          "N/A"
        end
      end
    end
  end

end

Propagate default formatting

If a default formatting is defined, it will not automatically be propagated to the dynamic cells, which could cause edge cases.

class UserCsvRowModel
  include Csvbuilder::Model

  column :name

  dynamic_column :skills

  class << self

    def format_cell(cell, _column_name, _context)
      "- #{cell} -"
    end

  end
end

To propagate it, recall the default formatting in dynamic columns formatting as follow:

class UserCsvExportModel < UserCsvRowModel
  include Csvbuilder::Export

  def skill(skill_name)
    source_model.skills.where(name: skill_name).exists? ? "1" : "0"
  end

  class << self
    def format_dynamic_column_cells(cells, _column_name, _context)
      cells.map do |cell|
        format_cell(cell, _column_name, _context)
      end
    end
  end

end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/joel/csvbuilder-dynamic-columns. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Csvbuilder::Dynamic::Columns project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.