Class: Trowel::Sow

Inherits:
Object
  • Object
show all
Defined in:
lib/trowel/sow.rb

Defined Under Namespace

Classes: RecordNotSaved

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Sow

Returns a new instance of Sow.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/trowel/sow.rb', line 3

def initialize(path, options = {})
  @path, @options = path, options
  $stdout << "[Trowel::Sow] Seeding #{klass} from #{path}\n" if options[:verbose]
  YAML::load_file(path).each do |attributes|
    object = klass.new
    attributes.each { |k,v| object.send("#{k}=", v) }
    begin
      process_callback(:before_save, object)
      object.save!
      process_callback(:after_save, object)
    rescue => e # ActiveRecord::RecordInvalid, SuperModel::InvalidRecord
      message = e.message + "\n[FATAL] Error saving #{object} with attributes: #{object.attributes.inspect}"
      raise(RecordNotSaved, message)
    end
  end
end