kaplan 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +42 -13
  2. data/lib/kaplan.rb +29 -13
  3. data/lib/kaplan/version.rb +1 -1
  4. metadata +4 -4
data/README.md CHANGED
@@ -29,7 +29,7 @@ It's easy to add support for another framework or ORM, though.
29
29
 
30
30
  ## Usage
31
31
 
32
- Here are the Rake tasks.
32
+ ### Available Rake tasks
33
33
 
34
34
  * **kaplan:db:reset** - Resets the database corresponding to the given environment by re-creating it from the development schema and then re-seeding it. Accepts: `RAILS_ENV` (required, must not be "development").
35
35
  * **kaplan:db:seed** - Seeds the database corresponding to the given environment with bootstrap data. The tables that will be seeded are truncated first so you don't have to. Accepts: `RAILS_ENV` (optional, default is "development").
@@ -38,36 +38,61 @@ Here are the Rake tasks.
38
38
  * **kaplan:db:create** - Creates the database corresponding to the given environment. Accepts: `RAILS_ENV` (optional, default is "development").
39
39
  * **kaplan:db:drop** - Drops the database corresponding to the given environment. Accepts: `RAILS_ENV` (optional, default is "development").
40
40
 
41
- Seeding is a bit different than Rails's built-in seeding, so I'll talk a little bit about that. Basically, you can still keep seeds in `db/seeds.rb` like before, but if you want to break them into separate files, you can put them in `seeds/`, and Kaplan will look for them there too. In fact, the seed files don't have to be Ruby, they can also be YAML or text files. So a YAML seed file would look like:
41
+ ### Seeding
42
42
 
43
- people:
43
+ Seeding is a bit different than Rails's built-in seeding, so I'll talk a little bit about that. Basically, you can still keep seeds in `db/seeds.rb` like before, but if you want to break them into separate files, you can put them in `seeds/`, and Kaplan will look for them there too. You name the file after the table the data in the file should be inserted into -- so for instance, you could have a file `seeds/people.rb`, and this might look like:
44
+
45
+ Person.create!(
46
+ :first_name => "Joe",
47
+ :last_name => "Bloe",
48
+ :likes => "eating spaghetti, watching The Godfather"
49
+ )
50
+
51
+ The seed files don't have to be Ruby, though; they can also be YAML or text files. So a YAML seed file would look like this:
52
+
53
+ Person:
54
+ -
55
+ first_name: Joe
56
+ last_name: Bloe
57
+ likes: "eating spaghetti, watching The Godfather"
58
+ -
59
+ first_name: Jill
60
+ last_name: Schmoe
61
+ likes: "biking, watching Family Guy"
62
+
63
+ For a YAML file, the key at the top indicates which model should be fed the records underneath. What if you have an STI situation, where more than one model shares a table? In this case, you keep the filename the same (`seeds/people.yml`), but you can list multiple keys:
64
+
65
+ Man:
44
66
  -
45
67
  first_name: Joe
46
68
  last_name: Bloe
47
69
  likes: eating spaghetti, watching The Godfather
70
+ Woman:
48
71
  -
49
72
  first_name: Jill
50
73
  last_name: Schmoe
51
74
  likes: biking, watching Family Guy
52
75
 
53
- and a text file would look like (note it looks kind of like CSV, but not quite, because you can't quote fields):
76
+ A text file looks like this (note it looks kind of like CSV, but not quite, because you can't quote fields):
54
77
 
55
78
  # first name, last name, likes
56
79
  Joe Bloe eating spaghetti, watching The Godfather
57
80
  Jill Schmoe biking, watching Family Guy
58
81
 
59
- In addition, you can have environment-specific files. So if I have a directory structure that looks like:
82
+ One thing about the naming of the files. What if you need to run your seeds in a certain order? Well, the files in the `seeds/` directory are sorted before being run, so you can simply prepend numbers to the name of the file. For instance, if I am seeding two tables, `cars` and `people`, but I want to make sure `people` is seeded before `cars`, I could name them `seeds/01_people.yml` and `seeds/02_cars.rb`.
83
+
84
+ You can also keep seed files per environment. So if I have a directory structure that looks like:
60
85
 
61
86
  seeds/
62
- people.txt
87
+ 01_people.txt
63
88
  development/
64
- cars.rb
65
- venues.rb
89
+ 02_cars.rb
90
+ 03_venues.rb
66
91
  test/
67
- cars.rb
68
- car_types.yml
92
+ 02_cars.rb
93
+ 03_car_types.yml
69
94
 
70
- Then, when I run `rake kaplan:db:seed` in different environments, this is which tables get populated:
95
+ If I were to run `rake kaplan:db:seed` in all three environments, this is which tables would get populated:
71
96
 
72
97
  +-------------+-------------------------+
73
98
  | Environment | Tables |
@@ -77,9 +102,13 @@ Then, when I run `rake kaplan:db:seed` in different environments, this is which
77
102
  | test | people, cars, car_types |
78
103
  +-------------+-------------------------+
79
104
 
80
- Besides running seeds in a certain environment, you also have the option to run specific seed files, regardless of which environment they're stored under. So if for some reason we wanted to run the cars.rb seed in the development environment and the car_types.yml seed in the test environment, we could do that like this:
105
+ What if you need to run a certain seed file even if you're not in that environment? Well, you can do that too. So if for some reason we wanted to run the cars.rb seed in the development environment and the car_types.yml seed in the test environment, we could do that like this:
106
+
107
+ rake db:seed ONLY=seeds/development/02_cars.rb,seeds/test/03_car_types.yml
108
+
109
+ Note how we have to include the numbers before each file, because they are part of the file name. This would ordinarily be annoying, but the list you pass to `ONLY` (or, `SEED`, for compatibility) are actually patterns, so you can make them as short as you want provided the patterns do not yield ambiguous results. For instance, if we wanted to run the `venues` seed from the development environment and the `car_types` seed from the test environment, we could just say this:
81
110
 
82
- rake db:seed ONLY=seeds/development/cars.rb,seeds/test/car_types.yml
111
+ rake db:seed ONLY=venues,car_types
83
112
 
84
113
  ## Author/License
85
114
 
@@ -104,29 +104,45 @@ module Kaplan
104
104
  end
105
105
 
106
106
  def seeds(env, options={})
107
+ available_files = [
108
+ "#{project_root}/db/seeds.rb",
109
+ Dir["#{project_root}/seeds/*.{yml,yaml,rb,txt,csv}"],
110
+ Dir["#{project_root}/seeds/#{env}/*.{yml,yaml,rb,txt,csv}"]
111
+ ].flatten
112
+ available_files_with_shorter_path = available_files.map do |fn|
113
+ [ fn, fn.sub("#{project_root}/", "") ]
114
+ end
107
115
  if options[:only]
108
- files = options[:only].
109
- map {|path| Dir["#{project_root}/#{path}"] }.
110
- flatten.
111
- select {|file| File.file?(file) && file =~ /\.(yml|yaml|rb|txt|csv)/ }
112
-
113
- if files.empty?
114
- raise "Error in Kaplan.seeds: Couldn't find the seed files you specified."
116
+ files = options[:only].map do |pattern|
117
+ pattern = (pattern =~ %r{/}) ? Regexp.new(Regexp.escape(pattern)) : %r[/(?:\d+_)?#{pattern}]
118
+ possible_files = available_files_with_shorter_path.select {|_, sfn| sfn =~ pattern }
119
+ if possible_files.empty?
120
+ msg = "Error in Kaplan.seeds: <#{pattern}> doesn't match any files. Available files:\n"
121
+ available_files_with_shorter_path.map do |_, sfn|
122
+ msg << " - #{sfn}\n"
123
+ end
124
+ raise msg
125
+ end
126
+ if possible_files.size > 1
127
+ msg = "Error in Kaplan.seeds: <#{pattern}> matches more than one file. Available files:\n"
128
+ available_files_with_shorter_path.map do |_, sfn|
129
+ msg << " - #{sfn}\n"
130
+ end
131
+ raise msg
132
+ end
133
+ possible_files[0][0]
115
134
  end
116
135
  else
117
- files = [
118
- "#{project_root}/db/seeds.rb",
119
- Dir["#{project_root}/seeds/*.{yml,yaml,rb,txt,csv}"],
120
- Dir["#{project_root}/seeds/#{env}/*.{yml,yaml,rb,txt,csv}"]
121
- ].flatten
136
+ files = available_files
122
137
  end
138
+ raise "Error in Kaplan.seeds: Got an empty set of seeds." if files.empty?
123
139
  files.sort.enum_for(:each_with_index).map do |filename, i|
124
140
  basename = ::File.basename(filename)
125
141
  # Ignore numbers at the beginning of the filename, as they are used for sorting
126
142
  basename =~ /^(?:\d+_)?(.+?)\.([^.]+)$/
127
143
  extension = $2.downcase
128
144
  # collection_name and model only applies to files within seeds/
129
- if i > 0
145
+ if filename != "#{project_root}/db/seeds.rb"
130
146
  collection_name = $1
131
147
  model = collection_name.classify.constantize
132
148
  end
@@ -1,3 +1,3 @@
1
1
  module Kaplan
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kaplan
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 4
10
- version: 0.2.4
9
+ - 5
10
+ version: 0.2.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Elliot Winkler
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-04 00:00:00 -06:00
18
+ date: 2011-04-20 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies: []
21
21