Class SeedFu::Writer
In: lib/seed-fu/writer.rb
Parent: Object

{Writer} is used to programmatically generated seed files. For example, you might want to write a script which converts data in a CSV file to a valid Seed Fu seed file, which can then be imported.

@example Basic usage

  SeedFu::Writer.write('path/to/file.rb', :class_name => 'Person', :constraints => [:first_name, :last_name]) do |writer|
    writer.add(:first_name => 'Jon',   :last_name => 'Smith',    :age => 21)
    writer.add(:first_name => 'Emily', :last_name => 'McDonald', :age => 24)
  end

  # Writes the following to the file:
  #
  # Person.seed(:first_name, :last_name,
  #   {:first_name=>"Jon", :last_name=>"Smith", :age=>21},
  #   {:first_name=>"Emily", :last_name=>"McDonald", :age=>24}
  # )

Methods

<<   add   new   write   write  

Public Class methods

@param [Hash] options @option options [String] :class_name Required The name of the Active Record model to

  generate seeds for

@option options [Fixnum] :chunk_size (100) The number of seeds to write before generating a

  `# BREAK EVAL` line. (Chunking reduces memory usage when loading seeds.)

@option options [:seed, :seed_once] :seed_type (:seed) The method to use when generating

  seeds. See {ActiveRecordExtension} for details.

@option options [Array<Symbol>] :constraints ([:id]) The constraining attributes for the seeds

Creates a new instance of {Writer} with the `options`, and then calls {write} with the `io_or_filename` and `block`

Public Instance methods

Add a seed. Must be called within a block passed to {write}. @param [Hash] seed The attributes for the seed

add(seed)

Alias for #<<

Writes the necessary headers and footers, and yields to a block within which the actual seed data should be writting using the `#<<` method.

@param [IO] io_or_filename The IO to which writes will be made. (If an `IO` is given, it is

  your responsibility to close it after writing.)

@param [String] io_or_filename The filename of a file to make writes to. (Will be opened and

  closed automatically.)

@yield [self] make calls to `#<<` within the block

[Validate]