Class: SeedFu::Writer
- Inherits:
-
Object
- Object
- SeedFu::Writer
- Defined in:
- lib/seed-fu/writer.rb
Overview
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.
Constant Summary collapse
- @@default_options =
{ :chunk_size => 100, :constraints => [:id], :seed_type => :seed }
Class Method Summary collapse
Instance Method Summary collapse
-
#<<(seed) ⇒ Object
(also: #add)
Add a seed.
-
#initialize(options = {}) ⇒ Writer
constructor
A new instance of Writer.
-
#write(io_or_filename) {|self| ... } ⇒ Object
Writes the necessary headers and footers, and yields to a block within which the actual seed data should be writting using the ‘#<<` method.
Constructor Details
#initialize(options = {}) ⇒ Writer
Returns a new instance of Writer.
34 35 36 37 |
# File 'lib/seed-fu/writer.rb', line 34 def initialize( = {}) @options = self.class..merge() raise ArgumentError, "missing option :class_name" unless @options[:class_name] end |
Class Method Details
.write(io_or_filename, options = {}, &block) ⇒ Object
Creates a new instance of SeedFu::Writer with the ‘options`, and then calls #write with the `io_or_filename` and `block`
41 42 43 |
# File 'lib/seed-fu/writer.rb', line 41 def self.write(io_or_filename, = {}, &block) new().write(io_or_filename, &block) end |
Instance Method Details
#<<(seed) ⇒ Object Also known as: add
Add a seed. Must be called within a block passed to #write.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/seed-fu/writer.rb', line 67 def <<(seed) raise "You must add seeds inside a SeedFu::Writer#write block" unless @io buffer = '' if chunk_this_seed? buffer << buffer << "# BREAK EVAL\n" buffer << seed_header end buffer << ",\n" buffer << ' ' + seed.inspect @io.write(buffer) @count += 1 end |
#write(io_or_filename) {|self| ... } ⇒ Object
Writes the necessary headers and footers, and yields to a block within which the actual seed data should be writting using the ‘#<<` method.
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/seed-fu/writer.rb', line 53 def write(io_or_filename, &block) raise ArgumentError, "missing block" unless block_given? if io_or_filename.respond_to?(:write) write_to_io(io_or_filename, &block) else File.open(io_or_filename, 'w') do |file| write_to_io(file, &block) end end end |