active_seed 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,7 @@ Although the inbuilt support for seeding in Rails works well for basic seeding.
6
6
 
7
7
  Simple add the following to your Gemfile
8
8
 
9
- gem 'active_seed', :git => "git://github.com/intrica/active_seed.git"
9
+ gem 'active_seed'
10
10
 
11
11
  Then run:
12
12
 
@@ -42,7 +42,7 @@ The purpose of a set file is to define a "set" of seed files. A set is defined i
42
42
 
43
43
  rails g active_seed
44
44
 
45
- When seeding, the default set file used will be one named after the current RAILS_ENV. So in devleopment mode it will use the file
45
+ When seeding, the default set file used will be one named after the current RAILS_ENV. So in development mode it will use the file
46
46
 
47
47
  RAILS_ROOT/db/active_seed/development.yml
48
48
 
@@ -120,4 +120,10 @@ Evaluations are handy if you want to interpret data in the column rather than ju
120
120
 
121
121
  As you can see, wherever the ? appears in the header, it is replaced with the value of the corresponding column.
122
122
 
123
- == Potential Issues
123
+ == Using from within your code
124
+
125
+ If you would like to seed a set directly from within other code, for example if you wanted to seed the "test" set at the start of your cucumber tests you can add the following to your code:
126
+
127
+ require "active_seed"
128
+
129
+ ActiveSeed.seed("test")
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.3
1
+ 1.0.4
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{active_seed}
8
- s.version = "1.0.3"
8
+ s.version = "1.0.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["David Monagle"]
12
- s.date = %q{2011-04-12}
12
+ s.date = %q{2011-05-16}
13
13
  s.description = %q{ActiveSeed Gives the ability to seed data by inserting it via activerecord. Also allows you to manage sets of data and seed based on the current environment.}
14
14
  s.email = %q{david.monagle@intrica.com.au}
15
15
  s.extra_rdoc_files = [
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
22
22
  "VERSION",
23
23
  "active_seed.gemspec",
24
24
  "lib/active_seed.rb",
25
+ "lib/active_seed/active_seed.rb",
25
26
  "lib/active_seed/engine.rb",
26
27
  "lib/active_seed/seed_csv.rb",
27
28
  "lib/generators/active_seed/example_html_colors_generator.rb",
@@ -1 +1,2 @@
1
1
  require "active_seed/engine"
2
+ require "active_seed/active_seed"
@@ -0,0 +1,99 @@
1
+ require "csv"
2
+
3
+ module ActiveSeed
4
+ def self.seed_csv(modelname, filename)
5
+ data = CSV.read(filename)
6
+ if data.size < 2
7
+ puts "No data found in file " << filename
8
+ return
9
+ end
10
+ header = data[0];
11
+ processed_header = Array.new
12
+ evaluations = Array.new
13
+ statics = Array.new
14
+ header.each do |h|
15
+ # Check if the header field has an assignment
16
+ if h.include? "="
17
+ c = h.split "="
18
+ h = c[0] # Replace the header with just the field name
19
+ c.delete_at(0) # Remove the field name
20
+ evaluation = c.join("=") # Rejoin the rest of the evaluation (if there was another =)
21
+ # If there is no question mark in the evaluation string then we set this column to be
22
+ # static.
23
+ unless evaluation.include? "?"
24
+ statics.push(h + "=" + evaluation)
25
+ else
26
+ processed_header.push(h)
27
+ evaluations.push(evaluation)
28
+ end
29
+ else
30
+ evaluations.push(nil)
31
+ processed_header.push(h)
32
+ end
33
+ end
34
+ header = processed_header
35
+ data.delete_at(0);
36
+ puts "Seeding " + data.size.to_s + " record" + (data.size > 1 ? "s" : "")
37
+ line = 1
38
+ data.each do |d|
39
+ line+=1
40
+ if d.size == header.size
41
+ code = "model = " + modelname + ".new\n"
42
+ for count in 0..(header.size - 1) do
43
+ unless (header[count].strip == "nil")
44
+ value = d[count]
45
+ value = "" if value.nil?
46
+ value = "'" + value.strip.gsub(/'/, "\\\\'") + "'"
47
+ if evaluations[count].nil?
48
+ assignment = value
49
+ else
50
+ assignment = evaluations[count].split("?")
51
+ assignment = assignment.join(value)
52
+ end
53
+ code += "model." + header[count].strip + "=" + assignment + "\n"
54
+ end
55
+ end
56
+ # Add in the statics
57
+ statics.each do |s|
58
+ code += "model." + s + "\n"
59
+ end
60
+ code += "unless model.save\n"
61
+ code += "ActiveSeed::print_errors(model.errors, " + line.to_s + ")"
62
+ code += "end\n"
63
+ eval code
64
+ #puts (line - 1).to_s + "/" + data.size.to_s
65
+ else
66
+ puts "Skipping line " + line.to_s + " with mismatch in number of fields (" + d.size.to_s + ")"
67
+ end
68
+ end
69
+ puts " Done"
70
+ end
71
+
72
+ def self.print_errors(errors, line)
73
+ puts "\nThere were errors on line " + line.to_s
74
+ errors.each do |e|
75
+ puts e.to_s
76
+ end
77
+ end
78
+
79
+ def seed(set)
80
+ set_file = File.join(::Rails.root.to_s, "db", "active_seed", set + ".yml")
81
+ if !File.exists?(set_file)
82
+ puts "Set file doesn't exist: " << set_file
83
+ return
84
+ end
85
+ puts "Seeding from set '" + set + "'"
86
+ fixture_list = YAML::load_file(set_file)
87
+ fixture_list.each do |model, sf|
88
+ seed_file = File.join(::Rails.root.to_s, "db", "active_seed", "data", sf + ".csv")
89
+ if !File.exists?(seed_file)
90
+ puts "Seed file doesn't exist: " << seed_file
91
+ else
92
+ puts "Seeding '" + seed_file + "'..."
93
+ ActiveSeed::seed_csv(model, seed_file)
94
+ end
95
+ end
96
+ end
97
+
98
+ module_function :seed
99
+ end
@@ -1,76 +1,2 @@
1
1
  require "csv"
2
2
 
3
- def seed_csv(modelname, filename)
4
- data = CSV.read(filename)
5
- if data.size < 2
6
- puts "No data found in file " << filename
7
- return
8
- end
9
- header = data[0];
10
- processed_header = Array.new
11
- evaluations = Array.new
12
- statics = Array.new
13
- header.each do |h|
14
- # Check if the header field has an assignment
15
- if h.include? "="
16
- c = h.split "="
17
- h = c[0] # Replace the header with just the field name
18
- c.delete_at(0) # Remove the field name
19
- evaluation = c.join("=") # Rejoin the rest of the evaluation (if there was another =)
20
- # If there is no question mark in the evaluation string then we set this column to be
21
- # static.
22
- unless evaluation.include? "?"
23
- statics.push(h + "=" + evaluation)
24
- else
25
- processed_header.push(h)
26
- evaluations.push(evaluation)
27
- end
28
- else
29
- evaluations.push(nil)
30
- processed_header.push(h)
31
- end
32
- end
33
- header = processed_header
34
- data.delete_at(0);
35
- puts "Seeding " + data.size.to_s + " record" + (data.size > 1 ? "s" : "")
36
- line = 1
37
- data.each do |d|
38
- line+=1
39
- if d.size == header.size
40
- code = "model = " + modelname + ".new\n"
41
- for count in 0..(header.size - 1) do
42
- unless (header[count].strip == "nil")
43
- value = d[count]
44
- value = "" if value.nil?
45
- value = "'" + value.strip.gsub(/'/, "\\\\'") + "'"
46
- if evaluations[count].nil?
47
- assignment = value
48
- else
49
- assignment = evaluations[count].split("?")
50
- assignment = assignment.join(value)
51
- end
52
- code += "model." + header[count].strip + "=" + assignment + "\n"
53
- end
54
- end
55
- # Add in the statics
56
- statics.each do |s|
57
- code += "model." + s + "\n"
58
- end
59
- code += "unless model.save\n"
60
- code += "print_errors(model.errors, " + line.to_s + ")"
61
- code += "end\n"
62
- eval code
63
- #puts (line - 1).to_s + "/" + data.size.to_s
64
- else
65
- puts "Skipping line " + line.to_s + " with mismatch in number of fields (" + d.size.to_s + ")"
66
- end
67
- end
68
- puts " Done"
69
- end
70
-
71
- def print_errors(errors, line)
72
- puts "\nThere were errors on line " + line.to_s
73
- errors.each do |e|
74
- puts e.to_s
75
- end
76
- end
@@ -1,24 +1,9 @@
1
1
  namespace :db do
2
- desc "Load seed files (from db/seeds) into the current environment's database."
2
+ desc "Load seed files (from db/seeds) into the current environment's database."
3
3
  task :active_seed => :environment do
4
- require "active_seed/seed_csv"
5
- set = ENV["set"]
4
+ require "active_seed"
5
+ set = ENV["set"]
6
6
  set = ::Rails.env unless !set.nil?
7
- set_file = File.join(::Rails.root.to_s, "db", "active_seed", set + ".yml")
8
- if !File.exists?(set_file)
9
- puts "Set file doesn't exist: " << set_file
10
- return
11
- end
12
- puts "Seeding from set '" + set + "'"
13
- fixture_list = YAML::load_file(set_file)
14
- fixture_list.each do |model, sf|
15
- seed_file = File.join(::Rails.root.to_s, "db", "active_seed", "data", sf + ".csv")
16
- if !File.exists?(seed_file)
17
- puts "Seed file doesn't exist: " << seed_file
18
- else
19
- puts "Seeding '" + seed_file + "'..."
20
- seed_csv(model, seed_file)
21
- end
22
- end
7
+ ActiveSeed.seed(set)
23
8
  end
24
9
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_seed
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 3
10
- version: 1.0.3
9
+ - 4
10
+ version: 1.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - David Monagle
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-12 00:00:00 +10:00
18
+ date: 2011-05-16 00:00:00 +10:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -65,6 +65,7 @@ files:
65
65
  - VERSION
66
66
  - active_seed.gemspec
67
67
  - lib/active_seed.rb
68
+ - lib/active_seed/active_seed.rb
68
69
  - lib/active_seed/engine.rb
69
70
  - lib/active_seed/seed_csv.rb
70
71
  - lib/generators/active_seed/example_html_colors_generator.rb