lib/seed_dump/perform.rb in seed_dump-0.3.2 vs lib/seed_dump/perform.rb in seed_dump-0.3.3
- old
+ new
@@ -1,45 +1,48 @@
module SeedDump
class Perform
- @opts = {}
- @ar_options = {}
- @indent = ""
- @models = []
- @seed_rb = ""
- @id_set_string = ""
- @verbose = true
+ def initialize
+ @opts = {}
+ @ar_options = {}
+ @indent = ""
+ @models = []
+ @seed_rb = ""
+ @id_set_string = ""
+ @verbose = true
+ @model_dir = 'app/models/*.rb'
+ end
- def self.setup(env)
+ def setup(env)
# config
@opts['with_id'] = !env["WITH_ID"].nil?
@opts['no-data'] = !env['NO_DATA'].nil?
@opts['models'] = env['MODELS'] || (env['MODEL'] ? env['MODEL'] : "")
@opts['file'] = env['FILE'] || "#{Rails.root}/db/seeds.rb"
@opts['append'] = (!env['APPEND'].nil? && File.exists?(@opts['file']) )
@ar_options = env['LIMIT'].to_i > 0 ? { :limit => env['LIMIT'].to_i } : {}
@indent = " " * (env['INDENT'].nil? ? 2 : env['INDENT'].to_i)
- @opts['models'] = @opts['models'].split(',').collect {|x| x.underscore.singularize.camelize.constantize }
+ @opts['models'] = @opts['models'].split(',').collect {|x| x.underscore.singularize.camelize }
end
- def self.loadModels
- Dir['app/models/*.rb'].sort.each do |f|
- model = File.basename(f, '.*').camelize.constantize
+ def loadModels
+ Dir[@model_dir].sort.each do |f|
+ model = File.basename(f, '.*').camelize
@models.push model if @opts['models'].include?(model) || @opts['models'].empty?
end
end
- def self.dumpAttribute(a_s,r,k,v)
+ def dumpAttribute(a_s,r,k,v)
v = r.attribute_for_inspect(k)
if k == 'id' && @opts['with_id']
@id_set_string = "{ |c| c.#{k} = #{v} }.save"
else
a_s.push("#{k.to_sym.inspect} => #{v}") unless k == 'id' && !@opts['with_id']
end
end
- def self.dumpModel(model)
+ def dumpModel(model)
@id_set_string = ''
create_hash = ""
rows = []
arr = []
arr = model.find(:all, @ar_options) unless @opts['no-data']
@@ -58,25 +61,25 @@
else
create_hash
end
end
- def self.dumpModels
+ def dumpModels
@seed_rb = ""
@models.sort.each do |model|
puts "Adding #{model} seeds." if @verbose
- @seed_rb << dumpModel(model) << "\n\n"
+ @seed_rb << dumpModel(model.constantize) << "\n\n"
end
end
- def self.writeFile
+ def writeFile
File.open(@opts['file'], (@opts['append'] ? "a" : "w")) { |f|
f << "# Autogenerated by the db:seed:dump task\n# Do not hesitate to tweak this to your needs\n" unless @opts['append']
f << "#{@seed_rb}"
}
end
- def self.run(env)
+ def run(env)
setup env
loadModels