class SpeakyCsv::ConfigBuilder

An instance of this class is yielded to the block passed to define_csv_fields. Used to configure speaky csv.

Attributes

config[R]

Public Class Methods

new(config: Config.new, root: true) click to toggle source
# File lib/speaky_csv/config_builder.rb, line 7
def initialize(config: Config.new, root: true)
  @config = config
  @config.root = root
end

Public Instance Methods

belongs_to(name)
Alias for: has_one
field(*fields, export_only: false) click to toggle source

Add one or many fields to the csv format.

If options are passed, they apply to all given fields.

# File lib/speaky_csv/config_builder.rb, line 15
def field(*fields, export_only: false)
  @config.fields += fields.map(&:to_sym)
  @config.fields.uniq!

  if export_only
    @config.export_only_fields += fields.map(&:to_sym)
    @config.export_only_fields.uniq!
  end

  nil
end
has_many(name) { |class.new config: has_manys, root: false| ... } click to toggle source

Define a one to many association. Expect a name and a block to define the fields on associated records.

For example:

define_csv_fields do |c|
  has_many 'reviews' do |r|
    r.field :id, :name, :_destroy
  end
end
# File lib/speaky_csv/config_builder.rb, line 66
def has_many(name)
  @config.root or raise NotImplementedError, "nested associations are not supported"
  @config.has_manys[name.to_sym] ||= Config.new
  yield self.class.new config: @config.has_manys[name.to_sym], root: false

  nil
end
has_one(name) { |class.new config: has_ones, root: false| ... } click to toggle source

Define a one to one association. This is also aliased as `belongs_to`. Expects a name and a block to define the fields on associated record.

For example:

define_csv_fields do |c|
  has_many 'publisher' do |p|
    p.field :id, :name, :_destroy
  end
end
# File lib/speaky_csv/config_builder.rb, line 46
def has_one(name)
  @config.root or raise NotImplementedError, "nested associations are not supported"
  @config.has_ones[name.to_sym] ||= Config.new
  yield self.class.new config: @config.has_ones[name.to_sym], root: false

  nil
end
Also aliased as: belongs_to
primary_key=(name, options = {}) click to toggle source

Define a custom primary key. By default an `id` column as used.

Accepts the same options as field

# File lib/speaky_csv/config_builder.rb, line 30
def primary_key=(name, options = {})
  field name, options
  @config.primary_key = name.to_sym
end