planter 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/planter/seeder.rb +75 -81
- data/lib/planter/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 39fe6b05679379fdcf8c619b1db28fdac6a6f89eec521b3e8cf3f3667a471c49
|
4
|
+
data.tar.gz: 42693cdccc9239a069996c74455edc658b339a894eff5b984ec00bba47a88797
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c3ffd760187e67c1f57b7daf7a1bba7f019470f1324ddaf72d1412c34930ea520eea449aebd0cea43b6b48de948f93ba80f47bafe6de782f550c35daf0bb8508
|
7
|
+
data.tar.gz: cfd8daff8e7d45a9d72411c195b31be7d1ddde9b562a03397b43f0848360c701d569289ab32e3260bf03f765752cd81fa123f3b597d9b08cc69ac9a6cab7bb6f
|
data/README.md
CHANGED
@@ -144,8 +144,8 @@ class UsersSeeder < Planter::Seeder
|
|
144
144
|
end
|
145
145
|
```
|
146
146
|
|
147
|
-
`ERB` can be used in the CSV files if you end the file name with `.csv.erb
|
148
|
-
For example, `users.csv.erb`.
|
147
|
+
`ERB` can be used in the CSV files if you end the file name with `.csv.erb` or
|
148
|
+
`.erb.csv`. For example, `users.csv.erb`.
|
149
149
|
|
150
150
|
```
|
151
151
|
participant_id,name
|
data/lib/planter/seeder.rb
CHANGED
@@ -142,7 +142,7 @@ module Planter
|
|
142
142
|
# The csv file corresponding to the model.
|
143
143
|
#
|
144
144
|
# @return [String]
|
145
|
-
class_attribute :
|
145
|
+
class_attribute :csv_name
|
146
146
|
|
147
147
|
##
|
148
148
|
# The seeding method specified.
|
@@ -151,79 +151,59 @@ module Planter
|
|
151
151
|
class_attribute :seed_method
|
152
152
|
|
153
153
|
##
|
154
|
-
#
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
unique_columns: nil,
|
195
|
-
erb_trim_mode: nil
|
196
|
-
)
|
197
|
-
if !SEEDING_METHODS.include?(seed_method.intern)
|
198
|
-
raise ArgumentError, "Method must be: #{SEEDING_METHODS.join(', ')}"
|
199
|
-
end
|
200
|
-
|
201
|
-
self.seed_method = seed_method
|
202
|
-
self.number_of_records = number_of_records
|
203
|
-
self.model = model || to_s.delete_suffix('Seeder').singularize
|
204
|
-
self.parent = parent
|
205
|
-
self.csv_file = determine_csv_filename(csv_name) if seed_method == :csv
|
206
|
-
self.erb_trim_mode = erb_trim_mode || Planter.config.erb_trim_mode
|
207
|
-
self.unique_columns =
|
208
|
-
case unique_columns
|
209
|
-
when String, Symbol then [unique_columns.intern]
|
210
|
-
when Array then unique_columns.map(&:intern)
|
211
|
-
end
|
154
|
+
# If your class is going to use the inherited +seed+ method, you must tell
|
155
|
+
# it which +seeding_method+ to use. The argument to this method must be
|
156
|
+
# included in the +SEEDING_METHODS+ array.
|
157
|
+
#
|
158
|
+
# @param [Symbol] seed_method
|
159
|
+
#
|
160
|
+
# @kwarg [Integer] number_of_records
|
161
|
+
#
|
162
|
+
# @kwarg [String] model
|
163
|
+
#
|
164
|
+
# @kwarg [Symbol, String] parent
|
165
|
+
#
|
166
|
+
# @kwarg [Symbol, String] csv_name
|
167
|
+
#
|
168
|
+
# @kwarg [Symbol, String] unique_columns
|
169
|
+
#
|
170
|
+
# @kwarg [String] erb_trim_mode
|
171
|
+
#
|
172
|
+
# @example
|
173
|
+
# require 'planter'
|
174
|
+
# class UsersSeeder < Planter::Seeder
|
175
|
+
# seeding_method :csv,
|
176
|
+
# number_of_records: 2,
|
177
|
+
# model: 'User'
|
178
|
+
# parent: :person,
|
179
|
+
# csv_name: :awesome_users,
|
180
|
+
# unique_columns %i[username email],
|
181
|
+
# erb_trim_mode: '<>'
|
182
|
+
# end
|
183
|
+
def self.seeding_method(
|
184
|
+
seed_method,
|
185
|
+
number_of_records: 1,
|
186
|
+
model: nil,
|
187
|
+
parent: nil,
|
188
|
+
csv_name: nil,
|
189
|
+
unique_columns: nil,
|
190
|
+
erb_trim_mode: nil
|
191
|
+
)
|
192
|
+
if !SEEDING_METHODS.include?(seed_method.intern)
|
193
|
+
raise ArgumentError, "Method must be: #{SEEDING_METHODS.join(', ')}"
|
212
194
|
end
|
213
195
|
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
196
|
+
self.seed_method = seed_method
|
197
|
+
self.number_of_records = number_of_records
|
198
|
+
self.model = model || to_s.delete_suffix('Seeder').singularize
|
199
|
+
self.parent = parent
|
200
|
+
self.csv_name = csv_name || to_s.delete_suffix('Seeder').underscore
|
201
|
+
self.erb_trim_mode = erb_trim_mode || Planter.config.erb_trim_mode
|
202
|
+
self.unique_columns =
|
203
|
+
case unique_columns
|
204
|
+
when String, Symbol then [unique_columns.intern]
|
205
|
+
when Array then unique_columns.map(&:intern)
|
223
206
|
end
|
224
|
-
|
225
|
-
raise ArgumentError, "Couldn't find csv for #{model}"
|
226
|
-
end
|
227
207
|
end
|
228
208
|
|
229
209
|
##
|
@@ -231,6 +211,7 @@ module Planter
|
|
231
211
|
# valid +seeding_method+, and not implement its own +seed+ method.
|
232
212
|
def seed
|
233
213
|
validate_attributes
|
214
|
+
extract_data_from_csv if seed_method == :csv
|
234
215
|
|
235
216
|
parent ? create_records_from_parent : create_records
|
236
217
|
end
|
@@ -263,18 +244,11 @@ module Planter
|
|
263
244
|
def validate_attributes # :nodoc:
|
264
245
|
case seed_method.intern
|
265
246
|
when :csv
|
266
|
-
|
267
|
-
if csv_file.end_with?('.erb')
|
268
|
-
contents = ERB.new(contents, trim_mode: erb_trim_mode).result(binding)
|
269
|
-
end
|
270
|
-
|
271
|
-
@data ||= ::CSV.parse(
|
272
|
-
contents, headers: true, header_converters: :symbol
|
273
|
-
).map(&:to_hash)
|
247
|
+
raise "Couldn't find csv for #{model}" unless full_csv_name
|
274
248
|
when :data_array
|
275
|
-
raise
|
249
|
+
raise 'data is not defined in the seeder' if public_send(:data).nil?
|
276
250
|
else
|
277
|
-
raise
|
251
|
+
raise 'seeding_method not defined in the seeder'
|
278
252
|
end
|
279
253
|
end
|
280
254
|
|
@@ -304,5 +278,25 @@ module Planter
|
|
304
278
|
@parent_model ||=
|
305
279
|
association_options.fetch(:class_name, parent.to_s.classify)
|
306
280
|
end
|
281
|
+
|
282
|
+
def full_csv_name
|
283
|
+
@full_csv_name ||=
|
284
|
+
%W[#{csv_name}.csv #{csv_name}.csv.erb #{csv_name}.erb.csv]
|
285
|
+
.map { |f| Rails.root.join(Planter.config.csv_files_directory, f).to_s }
|
286
|
+
.find { |f| ::File.file?(f) }
|
287
|
+
end
|
288
|
+
|
289
|
+
def extract_data_from_csv
|
290
|
+
contents = ::File.read(full_csv_name)
|
291
|
+
if full_csv_name.include?('.erb')
|
292
|
+
contents = ERB.new(contents, trim_mode: erb_trim_mode).result(binding)
|
293
|
+
end
|
294
|
+
|
295
|
+
@data ||= ::CSV.parse(
|
296
|
+
contents,
|
297
|
+
headers: true,
|
298
|
+
header_converters: :symbol
|
299
|
+
).map(&:to_hash)
|
300
|
+
end
|
307
301
|
end
|
308
302
|
end
|
data/lib/planter/version.rb
CHANGED