Class: MerbSeed::Seeder

Inherits:
Object
  • Object
show all
Defined in:
lib/merb_seed/seeder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class) ⇒ Seeder

Returns a new instance of Seeder.



19
20
21
22
23
# File 'lib/merb_seed/seeder.rb', line 19

def initialize(model_class)
  @model_class = model_class
  @constraints = [:id]
  @data = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

:nodoc:



63
64
65
66
67
68
69
# File 'lib/merb_seed/seeder.rb', line 63

def method_missing(method_name, *args) #:nodoc:
  if (match = method_name.to_s.match(/(.*)=$/)) && args.size == 1
    set_attribute(match[1], args.first)
  else
    super
  end
end

Class Method Details

.plant(model_class, *constraints) {|seed| ... } ⇒ Object

Yields:

  • (seed)


5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/merb_seed/seeder.rb', line 5

def self.plant(model_class, *constraints, &block)
  constraints = [:id] if constraints.empty?
  seed = Seeder.new(model_class)

  options = constraints.last if (constraints.last.is_a? Hash)
  constraints.delete_at(*constraints.length-1) if (constraints.last.is_a? Hash)

  insert_only = constraints.last.is_a? TrueClass
  constraints.delete_at(*constraints.length-1) if (constraints.last.is_a? TrueClass or constraints.last.is_a? FalseClass)
  seed.set_constraints(*constraints)
  yield seed
  seed.plant!({:insert_only => insert_only}.merge(options||{}))
end

Instance Method Details

#after_seed(&block) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/merb_seed/seeder.rb', line 38

def after_seed(&block)
  if block
    @after_seed_block = block
  else
    @after_seed_block
  end
end

#plant!(options) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/merb_seed/seeder.rb', line 46

def plant!(options)
  insert_only = options[:insert_only].nil? ? false : options[:insert_only]

  record = get
  return if !record.new_record? and insert_only
  @data.each do |k, v|
    record.send("#{k}=", v)
  end
  record.save || raise("Validation Failed. Use :validate => false in order to skip this:\n#{record.errors.inspect}")
  puts " - #{@model_class} #{condition_hash.inspect}"      
  
  # fire off after_seed event
  self.after_seed.call(record) if self.after_seed
  
  record
end

#set_attribute(name, value) ⇒ Object



34
35
36
# File 'lib/merb_seed/seeder.rb', line 34

def set_attribute(name, value)
  @data[name.to_sym] = value
end

#set_constraints(*constraints) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/merb_seed/seeder.rb', line 25

def set_constraints(*constraints)
  raise "You must set at least one constraint." if constraints.empty?
  @constraints = []
  constraints.each do |constraint|
    raise "Your constraint `#{constraint}` is not a column in #{@model_class}. Valid columns are `#{@model_class.column_names.join("`, `")}`." unless @model_class.column_names.include?(constraint.to_s)
    @constraints << constraint.to_sym
  end
end