seed_dump 0.2.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,5 +1,9 @@
1
1
  == Seed Dump
2
2
 
3
+ == 0.3.0 / 2011-04-22
4
+
5
+ * refactor to make testing possible
6
+
3
7
  == 0.2.4 / 2011-04-22
4
8
 
5
9
  * properly quote any value (also fixes Time value generation)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.4
1
+ 0.3.0
data/lib/seed_dump.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # SeedDump
2
2
  module SeedDump
3
3
  require 'seed_dump/railtie' if defined?(Rails)
4
+ require 'seed_dump/perform' if defined?(Rails)
4
5
  end
@@ -0,0 +1,84 @@
1
+ module SeedDump
2
+ class Perform
3
+
4
+ @opts = {}
5
+ @ar_options = {}
6
+ @indent = ""
7
+ @models = []
8
+ @seed_rb = ""
9
+ @id_set_string = ""
10
+ @verbose = true
11
+
12
+ def self.setup(env)
13
+ # config
14
+ @opts['with_id'] = !env["WITH_ID"].nil?
15
+ @opts['no-data'] = !env['NO_DATA'].nil?
16
+ @opts['models'] = env['MODELS'] || (env['MODEL'] ? env['MODEL'] : "")
17
+ @opts['file'] = env['FILE'] || "#{Rails.root}/db/seeds.rb"
18
+ @opts['append'] = (!env['APPEND'].nil? && File.exists?(opts['file']) )
19
+ @ar_options = env['LIMIT'].to_i > 0 ? { :limit => env['LIMIT'].to_i } : {}
20
+ @indent = " " * (env['INDENT'].nil? ? 2 : env['INDENT'].to_i)
21
+ @opts['models'] = @opts['models'].split(',').collect {|x| x.underscore.singularize.camelize.constantize }
22
+ end
23
+
24
+ def self.loadModels
25
+ Dir['app/models/*.rb'].sort.each do |f|
26
+ model = File.basename(f, '.*').camelize.constantize
27
+ @models.push model if @opts['models'].include?(model) || @opts['models'].empty?
28
+ end
29
+ end
30
+
31
+ def self.dumpAttribute(a_s,r,k,v)
32
+ v = r.attribute_for_inspect(k)
33
+ if k == 'id' && @opts['with_id']
34
+ @id_set_string = "{ |c| c.#{k} = #{v} }.save"
35
+ else
36
+ a_s.push("#{k.to_sym.inspect} => #{v}") unless k == 'id' && !@opts['with_id']
37
+ end
38
+ end
39
+
40
+ def self.dumpModel(model)
41
+ create_hash = ""
42
+ arr = []
43
+ arr = model.find(:all, @ar_options) unless @opts['no-data']
44
+ arr = arr.empty? ? [model.new] : arr
45
+ arr.each_with_index { |r,i|
46
+ attr_s = [];
47
+ r.attributes.each { |k,v| dumpAttribute(attr_s,r,k,v) }
48
+ create_hash << "\n#{model}.create" << '( ' << attr_s.join(', ') << ' )' << @id_set_string
49
+ }
50
+ create_hash
51
+ end
52
+
53
+ def self.dumpModels
54
+ @seed_rb = ""
55
+ @models.sort.each do |model|
56
+ @id_set_string = ''
57
+ puts "Adding #{model} seeds." if @verbose
58
+ @seed_rb << dumpModel(model) << "\n\n"
59
+ end
60
+ end
61
+
62
+ def self.writeFile
63
+ File.open(@opts['file'], (@opts['append'] ? "a" : "w")) { |f|
64
+ f << "# Autogenerated by the db:seed:dump task\n# Do not hesitate to tweak this to your needs\n" unless @opts['append']
65
+ f << "#{@seed_rb}"
66
+ }
67
+ end
68
+
69
+ def self.run(env)
70
+
71
+ setup env
72
+
73
+ loadModels
74
+
75
+ puts "Appending seeds to #{@opts['file']}." if @opts['append']
76
+ dumpModels
77
+
78
+ puts "Writing #{@opts['file']}."
79
+ writeFile
80
+
81
+ puts "Done."
82
+ end
83
+ end
84
+ end
@@ -3,73 +3,8 @@ namespace :db do
3
3
  desc "Dump records from the database into db/seeds.rb"
4
4
  task :dump => :environment do
5
5
 
6
- # config
7
- opts = {}
8
- opts['with_id'] = !ENV["WITH_ID"].nil?
9
- opts['no-data'] = !ENV['NO_DATA'].nil?
10
- opts['models'] = ENV['MODELS'] || (ENV['MODEL'] ? ENV['MODEL'] : "")
11
- opts['file'] = ENV['FILE'] || "#{Rails.root}/db/seeds.rb"
12
- opts['append'] = (!ENV['APPEND'].nil? && File.exists?(opts['file']) )
13
- ar_options = ENV['LIMIT'].to_i > 0 ? { :limit => ENV['LIMIT'].to_i } : {}
14
- indent = " " * (ENV['INDENT'].nil? ? 2 : ENV['INDENT'].to_i)
6
+ SeedDump::Perform::run(ENV)
15
7
 
16
- models = opts['models'].split(',').collect {|x| x.underscore.singularize }
17
-
18
- new_line = "\n"
19
- puts "Appending seeds to #{opts['file']}." if opts['append']
20
-
21
- seed_rb = ""
22
- Dir['app/models/*.rb'].sort.each do |f|
23
- model_name = File.basename(f, '.*')
24
- if models.include?(model_name) || models.empty?
25
-
26
- puts "Adding #{model_name.camelize} seeds."
27
-
28
- create_hash = ""
29
-
30
- model = model_name.camelize.constantize
31
- arr = []
32
- arr = model.find(:all, ar_options) unless opts['no-data']
33
- arr = arr.empty? ? [model.new] : arr
34
- arr.each_with_index { |r,i|
35
-
36
- attr_s = [];
37
-
38
- id_set_string = ''
39
- r.attributes.each { |k,v|
40
- v = r.attribute_for_inspect(k)
41
- if k == 'id' && opts['with_id']
42
- id_set_string = "{ |c| c.#{k} = #{v} }.save"
43
- else
44
- attr_s.push("#{k.to_sym.inspect} => #{v}") unless k == 'id' && !opts['with_id']
45
- end
46
- }
47
- create_hash << (i > 0 ? "#{new_line}" : new_line) << "#{model_name.camelize}.create" << '( ' << attr_s.join(', ') << ' )' << id_set_string
48
- }
49
- seed_rb << "#{create_hash}#{new_line}#{new_line}"
50
- end
51
- end
52
-
53
- File.open(opts['file'], (opts['append'] ? "a" : "w")) { |f|
54
-
55
- puts "Writing #{opts['file']}."
56
-
57
- unless opts['append']
58
- cont =<<HERE
59
- # Autogenerated by the db:seed:dump task
60
- # Do not hesitate to tweak this to your needs
61
- HERE
62
- f << cont
63
- end
64
-
65
- cont =<<HERE
66
- #{seed_rb}
67
- HERE
68
- f << cont
69
-
70
- puts "Done."
71
-
72
- }
73
8
  end
74
9
  end
75
10
  end
data/seed_dump.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{seed_dump}
8
- s.version = "0.2.4"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Rob Halff"]
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
22
22
  "Rakefile",
23
23
  "VERSION",
24
24
  "lib/seed_dump.rb",
25
+ "lib/seed_dump/perform.rb",
25
26
  "lib/seed_dump/railtie.rb",
26
27
  "lib/tasks/seed_dump.rake",
27
28
  "seed_dump.gemspec",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seed_dump
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -26,6 +26,7 @@ files:
26
26
  - Rakefile
27
27
  - VERSION
28
28
  - lib/seed_dump.rb
29
+ - lib/seed_dump/perform.rb
29
30
  - lib/seed_dump/railtie.rb
30
31
  - lib/tasks/seed_dump.rake
31
32
  - seed_dump.gemspec