class BusinessCatalyst::CSV::Row

Shared logic for building a row for a CSV export for Business Catalust. Instead of sublcassing Row directly in your project, subclass CatalogRow or ProductRow, which have column definitions set up for those tables.

Public Class Methods

columns() click to toggle source

Override to return Array of column config Arrays in the format:

[
  ["Header", :mapping_name, default_value, TransformerClass],
  ...
]

“default_value” and “TransformerClass” are optional, and will default to nil and GenericTransformer, respectively, if not specified.

# File lib/business_catalyst/csv/row.rb, line 25
def self.columns
  raise NotImplementedError, "Implement to return Array of column config Arrays in the format: [[\"Header\", :mapping_name, default_value, TransformerClass]]"
end
default_currency(currency) click to toggle source

Set the default currency for the current application. Alias for CurrencyTransformer.default_currency=

class MyRow < BusinessCatalyst::CSV:Row
  default_currency "US"
end
# File lib/business_catalyst/csv/row.rb, line 45
def self.default_currency(currency)
  CurrencyTransformer.default_currency = currency
end
generate(file_name, collection = nil) { |item| ... } click to toggle source

Nice shortcut for generating a csv. TODO: add option to usea file splitter.

Manual:

ProductRow.generate("products.csv") do |csv|
  products.each do |product|
    csv << ProductRow.new(product).to_a
  end
end

Automatic collection handling:

ProductRow.generate("products.csv", products) do |product|
  ProductRow.new(product)
end
# File lib/business_catalyst/csv/row.rb, line 66
def self.generate(file_name, collection = nil)
  ::CSV.open(file_name, 'wb') do |csv|
    csv << headers
    if collection.respond_to?(:each)
      collection.each do |item|
        row = yield(item)
        raise ArgumentError, "input must be a valid Row" unless row.kind_of?(self)
        csv << row.to_a
      end
    else
      yield csv
    end
  end
end
headers() click to toggle source
# File lib/business_catalyst/csv/row.rb, line 87
def self.headers
  @headers ||= columns.map(&:first)
end
map(column, &block) click to toggle source

Define value for BC column using a block. Column argument should be one of the mapping name symbols returned by Row.columns.

# File lib/business_catalyst/csv/row.rb, line 31
def self.map(column, &block)
  unless columns.any? {|c| c[1] == column}
    raise NoSuchColumnError, "no such column '#{column.inspect}'"
  end
  define_method(column, &block)
end
new(*args) click to toggle source
# File lib/business_catalyst/csv/row.rb, line 13
def initialize(*args)
end

Public Instance Methods

csv_value(method, config = nil) click to toggle source
# File lib/business_catalyst/csv/row.rb, line 91
def csv_value(method, config = nil)
  config ||= self.class.columns.find {|c| c[1] == method}
  raise NoSuchColumnError, "no configuration found for #{method.inspect} in #{self.class.to_s}.columns" if config.nil?

  input = if respond_to?(method)
            send(method)
          else
            config.fetch(2, nil)
          end

  transformer = config.fetch(3, GenericTransformer)
  transformer.transform(input)
end
to_a() click to toggle source
# File lib/business_catalyst/csv/row.rb, line 81
def to_a
  self.class.columns.map { |column|
    csv_value(column[1], column)
  }
end