Class: SeedFu::Seeder
- Inherits:
-
Object
- Object
- SeedFu::Seeder
- Defined in:
- lib/seed-fu.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(model_class) ⇒ Seeder
constructor
A new instance of Seeder.
-
#method_missing(method_name, *args) ⇒ Object
:nodoc:.
- #plant! ⇒ Object
- #set_attribute(name, value) ⇒ Object
- #set_constraints(*constraints) ⇒ Object
Constructor Details
#initialize(model_class) ⇒ Seeder
Returns a new instance of Seeder.
11 12 13 14 15 |
# File 'lib/seed-fu.rb', line 11 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:
40 41 42 43 44 45 46 |
# File 'lib/seed-fu.rb', line 40 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
Instance Method Details
#plant! ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/seed-fu.rb', line 30 def plant! record = get @data.each do |k, v| record.send("#{k}=", v) end record.save! puts " - #{@model_class} #{condition_hash.inspect}" record end |
#set_attribute(name, value) ⇒ Object
26 27 28 |
# File 'lib/seed-fu.rb', line 26 def set_attribute(name, value) @data[name.to_sym] = value end |
#set_constraints(*constraints) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/seed-fu.rb', line 17 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 |