kaplan 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/kaplan.rb CHANGED
@@ -81,19 +81,25 @@ module Kaplan
81
81
  end
82
82
 
83
83
  class << self
84
- def choose_database_adapter
84
+ attr_reader :database_orm, :web_framework
85
+
86
+ def detect_database_orm
85
87
  if defined?(::ActiveRecord)
86
- Kaplan.extend(Kaplan::DatabaseAdapters::ActiveRecord)
88
+ @database_orm = :activerecord
89
+ extend Kaplan::DatabaseAdapters::ActiveRecord
87
90
  elsif defined?(::Mongoid)
88
- Kaplan.extend(Kaplan::DatabaseAdapters::Mongoid)
91
+ @database_orm = :mongoid
92
+ extend Kaplan::DatabaseAdapters::Mongoid
89
93
  end
90
94
  end
91
95
 
92
- def choose_web_framework
96
+ def detect_web_framework
93
97
  if defined?(::Rails)
94
- Kaplan.extend(Kaplan::WebFrameworks::Rails)
98
+ @web_framework = :rails
99
+ extend Kaplan::WebFrameworks::Rails
95
100
  elsif defined?(::Padrino)
96
- Kaplan.extend(Kaplan::WebFrameworks::Padrino)
101
+ @web_framework = :padrino
102
+ extend Kaplan::WebFrameworks::Padrino
97
103
  end
98
104
  end
99
105
 
@@ -108,14 +114,21 @@ module Kaplan
108
114
  raise "Error in Kaplan.seeds: Couldn't find the seed files you specified."
109
115
  end
110
116
  else
111
- files = Dir["#{project_root}/seeds/*.{yml,yaml,rb,txt,csv}"] + Dir["#{project_root}/seeds/#{env}/*.{yml,yaml,rb,txt,csv}"]
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
112
122
  end
113
- files.map do |filename|
123
+ files.enum_for(:each_with_index).map do |filename, i|
114
124
  basename = ::File.basename(filename)
115
125
  basename =~ /^(.+?)\.([^.]+)$/
116
- collection_name = $1
117
126
  extension = $2.downcase
118
- model = collection_name.classify.constantize
127
+ # collection_name and model only applies to files within seeds/
128
+ if i > 0
129
+ collection_name = $1
130
+ model = collection_name.classify.constantize
131
+ end
119
132
  [filename, extension, collection_name, model]
120
133
  end
121
134
  end
@@ -129,18 +142,20 @@ module Kaplan
129
142
  establish_database(options[:env])
130
143
  seeds = seeds(options[:env], :only => options[:only])
131
144
  seeds.each do |filename, ext, collection_name, model|
132
- if ext == "rb"
133
- puts " - Adding data for #{collection_name}..." if level > 1
134
- load filename
135
- elsif ext == "yml" || ext == "yaml"
136
- data = ::YAML.load_file(filename)
137
- records = (Hash === data) ? data[data.keys.first] : data
138
- puts " - Adding data for #{collection_name}..." if level > 1
139
- insert_rows(records, model)
140
- else
141
- lines = ::File.read(filename).split(/\n/)
142
- puts " - Adding data for #{collection_name}..." if level > 1
143
- insert_rows_from_csv(lines, model)
145
+ seed_database_for(filename, ext, collection_name, model) do
146
+ puts " - Adding data for #{collection_name}..." if level > 1 && collection_name
147
+ end
148
+
149
+ # If the seed file sets an explicit id for a record, the next time you
150
+ # attempt to create a record, Postgres will bomb because it still thinks
151
+ # the sequence for the id starts at 1. So this will set the next value for
152
+ # the sequence to the last id + 1.
153
+ #
154
+ # See: <http://stackoverflow.com/questions/1709705/postgresql-nextval-generating-existing-values>
155
+ #
156
+ if @database_orm == :activerecord && model && model.connection.adapter_name == "PostgreSQL"
157
+ puts " - Resetting primary key sequence for #{collection_name}..."
158
+ ActiveRecord::Base.connection.reset_pk_sequence!(collection_name)
144
159
  end
145
160
  end
146
161
  end
@@ -159,10 +174,27 @@ module Kaplan
159
174
 
160
175
  def seedable_collections(env, options={})
161
176
  # Remove collections that don't exist
162
- seeds(env, options).map {|filename, ext, collection_name, model| collection_name } & all_collections
177
+ seeds(env, options).map {|filename, ext, collection_name, model| collection_name }.compact & all_collections
163
178
  end
164
179
 
165
180
  private
181
+ def seed_database_for(filename, ext, collection_name, model)
182
+ case ext
183
+ when "rb"
184
+ yield
185
+ load filename
186
+ when "yml", "yaml"
187
+ data = ::YAML.load_file(filename)
188
+ records = (Hash === data) ? data[data.keys.first] : data
189
+ yield
190
+ insert_rows(records, model)
191
+ else
192
+ lines = ::File.read(filename).split(/\n/)
193
+ yield
194
+ insert_rows_from_csv(lines, model)
195
+ end
196
+ end
197
+
166
198
  def insert_rows(rows, model)
167
199
  rows.each do |row|
168
200
  record = model.new
@@ -173,6 +205,7 @@ module Kaplan
173
205
  end
174
206
  end
175
207
 
208
+ # TODO: Replace this with a real CSV parser
176
209
  def insert_rows_from_csv(lines, model)
177
210
  columns = lines.shift.sub(/^#[ ]*/, "").split(/,[ ]*/)
178
211
  rows = lines.map do |line|
@@ -185,5 +218,5 @@ module Kaplan
185
218
  end
186
219
  end
187
220
 
188
- Kaplan.choose_database_adapter
189
- Kaplan.choose_web_framework
221
+ Kaplan.detect_database_orm
222
+ Kaplan.detect_web_framework
@@ -12,7 +12,7 @@ namespace :kaplan do
12
12
  desc "Seeds a database of your choice (default: development) with bootstrap data.\nThe relevant tables are truncated first so you don't have to."
13
13
  task :seed, [:env, :only] => :environment do |t, args|
14
14
  env = args[:env] || ENV["RAILS_ENV"] || "development"
15
- if only = args[:only] || ENV["ONLY"]
15
+ if only = args[:only] || ENV["ONLY"] || ENV["SEED"]
16
16
  only = only.split(",") unless Array === only
17
17
  end
18
18
  Rake::Task['kaplan:db:plow'].invoke(env, only)
@@ -1,3 +1,3 @@
1
1
  module Kaplan
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
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: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
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-02-02 00:00:00 -07:00
18
+ date: 2011-02-05 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies: []
21
21