Class: BusinessCatalyst::CSV::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/business_catalyst/csv/row.rb

Overview

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.

Direct Known Subclasses

CatalogRow, ProductRow

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Row

Returns a new instance of Row.



13
14
# File 'lib/business_catalyst/csv/row.rb', line 13

def initialize(*args)
end

Class Method Details

.columnsObject

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.

Raises:

  • (NotImplementedError)


25
26
27
# 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) ⇒ Object

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

class MyRow < BusinessCatalyst::CSV:Row
  default_currency "US"
end


45
46
47
# File 'lib/business_catalyst/csv/row.rb', line 45

def self.default_currency(currency)
  CurrencyTransformer.default_currency = currency
end

.generate(file_name, collection = nil) ⇒ Object

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


66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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

.headersObject



87
88
89
# File 'lib/business_catalyst/csv/row.rb', line 87

def self.headers
  @headers ||= columns.map(&:first)
end

.map(column, &block) ⇒ Object

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



31
32
33
34
35
36
# 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

Instance Method Details

#csv_value(method, config = nil) ⇒ Object

Raises:



91
92
93
94
95
96
97
98
99
100
101
102
103
# 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_aObject



81
82
83
84
85
# File 'lib/business_catalyst/csv/row.rb', line 81

def to_a
  self.class.columns.map { |column|
    csv_value(column[1], column)
  }
end