class Bumblebee::Template

Wraps up columns and provides to main methods: generate_csv: take in an array of objects and return a string (CSV contents) parse_csv: take in a string and return an array of hashes

Constants

BOM
CUSTOM_OPTIONS
Options
UTF8_BOM

Attributes

column_set[R]
object_class[R]

Public Class Methods

new(columns: nil, object_class: Hash) { |self| ... } click to toggle source
# File lib/bumblebee/template.rb, line 22
def initialize(columns: nil, object_class: Hash, &block)
  @column_set   = ColumnSet.new(self.class.all_columns)
  @object_class = object_class

  column_set.add(columns)

  return unless block_given?

  if block.arity == 1
    yield self
  else
    instance_eval(&block)
  end
end

Public Instance Methods

column(header, opts = {}) click to toggle source
# File lib/bumblebee/template.rb, line 37
def column(header, opts = {})
  column_set.column(header, opts)

  self
end
generate(objects, options = {}) click to toggle source
# File lib/bumblebee/template.rb, line 50
def generate(objects, options = {})
  objects = array(objects)
  options = parse_options(options)
  prefix  = options.bom ? UTF8_BOM : ''

  prefix + CSV.generate(options.ruby_csv_options) do |csv|
    objects.each do |object|
      csv << columns.each_with_object({}) do |column, hash|
        column.csv_set(object, hash)
      end
    end
  end
end
parse(string, options = {}) click to toggle source
# File lib/bumblebee/template.rb, line 64
def parse(string, options = {})
  string_without_bom = string.sub(UTF8_BOM, '')

  csv = CSV.new(string_without_bom, options.merge(headers: true))

  csv.to_a.map do |row|
    # Build up a hash using the column one at a time
    columns.each_with_object(object_class.new) do |column, object|
      column.object_set(row, object)
    end
  end
end

Private Instance Methods

array(objects) click to toggle source
# File lib/bumblebee/template.rb, line 85
def array(objects)
  objects.is_a?(Hash) ? [objects] : Array(objects)
end
parse_options(options) click to toggle source
# File lib/bumblebee/template.rb, line 89
def parse_options(options)
  options = (options || {}).symbolize_keys
  bom     = options[BOM] || false

  ruby_csv_options = options.merge(headers: headers, write_headers: true)
                            .reject { |k| CUSTOM_OPTIONS.include?(k) }

  Options.new(ruby_csv_options, bom)
end