class SpeakyCsv::Base

Inherit from this class when using SpeakyCsv

Public Class Methods

active_record_importer(input_io_or_enumerable, klass) click to toggle source

Return a new active record instance from the given IO, which is expected to be able to read a csv file. The importer will be an Enumerator that returns successive active records.

For example:

class UserCsv < SpeakyCsv::Base

define_csv_fields do |c|
  c.fields :id, :name
end

end

File.open('sample.csv', 'r') do |io|

importer = SomeFormat.new.active_record_importer io, User

importer.each do |record|
  # record will be a User instance or nil
end

end

Optionally an Enumerable instance can be passed instead of an IO instance. The enumerable should return attr hashes. This may be helpful for transforming or chaining Enumerables.

# File lib/speaky_csv/base.rb, line 64
def self.active_record_importer(input_io_or_enumerable, klass)
  ActiveRecordImport.new \
    speaky_csv_config,
    input_io_or_enumerable,
    klass
end
attr_importer(input_io) click to toggle source

Return a new attr importer instance from the given IO, which is expected to be able to read a csv file. The importer will be an Enumerator that returns successive attribute hashes.

For example:

class SomeFormat < SpeakyCsv::Base

define_csv_fields do |c|
  c.fields :id, :name
end

end

File.open('sample.csv', 'r') do |io|

importer = SomeFormat.new.attr_importer io

importer.each do |attrs|
  # attrs will be hashes like { "id" => 123, "name" => "Curley" }
end

end

# File lib/speaky_csv/base.rb, line 37
def self.attr_importer(input_io)
  AttrImport.new speaky_csv_config,
                 input_io
end
define_csv_fields() { |config_builder(config: speaky_csv_config)| ... } click to toggle source
# File lib/speaky_csv/base.rb, line 7
def self.define_csv_fields
  self.speaky_csv_config = speaky_csv_config.deep_dup
  yield ConfigBuilder.new(config: speaky_csv_config)
end
exporter(records_enumerator) click to toggle source

Return a new exporter instance

# File lib/speaky_csv/base.rb, line 13
def self.exporter(records_enumerator)
  Export.new speaky_csv_config,
             records_enumerator
end