planter 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +18 -6
- data/lib/planter.rb +3 -2
- data/lib/planter/seeder.rb +49 -24
- data/lib/planter/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 42b61a78b5034eadac54f7e5a570073851b4a9a6fc20c4fae71a1e49fb8d3df1
|
4
|
+
data.tar.gz: c02538d3a776a89b221b135c4aa2d355a278df35e514d53872f200556bc720dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 119f4f0a790efd00eb4e537734ab51e9416b5017f0891a07698d291b44604b2a81e7ea73d1a8893da1e3f4c24b4790a4a18a352388240d9ed9de32a1f19206b5
|
7
|
+
data.tar.gz: 3c066efbe8b1325bec3218de0840bf91688c6ed1da29e06446756603f370620d5a57af4afc57313fa42171ba977f1dab9cbe73a9578fcb3379e2433935c609b3
|
data/README.md
CHANGED
@@ -72,7 +72,7 @@ To seed from CSV, you simply need to add the following to your seeder class.
|
|
72
72
|
|
73
73
|
```ruby
|
74
74
|
class UsersSeeder < Planter::Seeder
|
75
|
-
seeding_method :
|
75
|
+
seeding_method :csv
|
76
76
|
end
|
77
77
|
```
|
78
78
|
|
@@ -86,15 +86,27 @@ test1@example.com,test1
|
|
86
86
|
test2@example.com,test2
|
87
87
|
```
|
88
88
|
|
89
|
-
If the CSV
|
90
|
-
option. Note that the value
|
89
|
+
If the CSV file is named differently than the seeder, you can specify the
|
90
|
+
`:csv_name` option. Note that the value should not include the file extension.
|
91
91
|
|
92
92
|
```ruby
|
93
93
|
class UsersSeeder < Planter::Seeder
|
94
|
-
seeding_method :
|
94
|
+
seeding_method :csv, csv_name: :people
|
95
95
|
end
|
96
96
|
```
|
97
97
|
|
98
|
+
`ERB` can be used in the CSV files if you name it with `.erb` at the end of the
|
99
|
+
file name. For example, `users.csv.erb`. Note that lines starting with `<%` and
|
100
|
+
ending with `%>` will not be considered rows, so you can use `ERB` rows to set
|
101
|
+
values. For example:
|
102
|
+
|
103
|
+
```csv.erb
|
104
|
+
email,login_attempts
|
105
|
+
<% count = 1 %>
|
106
|
+
test2@example.com,<%= count += 1 %>
|
107
|
+
test2@example.com,<%= count += 1 %>
|
108
|
+
```
|
109
|
+
|
98
110
|
Running `rails db:seed` will now seed your `users` table.
|
99
111
|
|
100
112
|
## Seeding from a data array
|
@@ -148,7 +160,7 @@ end
|
|
148
160
|
|
149
161
|
Note that specifying `number_of_records` in this instance will create that many
|
150
162
|
records *for each record of the parent model*. You can also specify the
|
151
|
-
association if it's different from the table name, using the `
|
163
|
+
association if it's different from the table name, using the `:assocation`
|
152
164
|
option.
|
153
165
|
|
154
166
|
### Custom seeds
|
@@ -169,7 +181,7 @@ end
|
|
169
181
|
```
|
170
182
|
|
171
183
|
## Customization
|
172
|
-
You can change the directories of both the seeder files and the
|
184
|
+
You can change the directories of both the seeder files and the CSV files. In
|
173
185
|
your `configure` block in `db/seeds.rb`, you can add the following. Note that,
|
174
186
|
in both instances, the path should be relative to `Rails.root`.
|
175
187
|
|
data/lib/planter.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'csv'
|
4
|
+
require 'erb'
|
4
5
|
require 'planter/version'
|
5
6
|
require 'planter/railtie'
|
6
7
|
require 'planter/config'
|
@@ -16,13 +17,13 @@ require 'planter/seeder'
|
|
16
17
|
# The most basic way to seed is to have a CSV file with the same name as the
|
17
18
|
# table in +db/seed_files/+. So, +users.csv+. This CSV should have the table's
|
18
19
|
# column names as header. To seed using this method, your class should look
|
19
|
-
# like the following. Note that +:
|
20
|
+
# like the following. Note that +:csv_name+ is not required; it defaults to the
|
20
21
|
# table name with a +csv+ file extension. The directory where the seed files
|
21
22
|
# are kept can be changed via an initializer.
|
22
23
|
# # db/seeds/users_seeder.rb
|
23
24
|
# require 'planter'
|
24
25
|
# class UsersSeeder < Planter::Seeder
|
25
|
-
# seeding_method :
|
26
|
+
# seeding_method :csv, csv_name: '/home/me/users.csv'
|
26
27
|
# end
|
27
28
|
#
|
28
29
|
# Another way to seed is to create records from a data array. To do this, your
|
data/lib/planter/seeder.rb
CHANGED
@@ -8,14 +8,14 @@ module Planter
|
|
8
8
|
# The allowed seeding methods.
|
9
9
|
#
|
10
10
|
# @return [Array]
|
11
|
-
SEEDING_METHODS = %i[
|
11
|
+
SEEDING_METHODS = %i[csv data_array].freeze
|
12
12
|
|
13
13
|
##
|
14
14
|
# Array of hashes used to create records. Your class must set this
|
15
15
|
# attribute when using +data_hash+ seeding method, although it's probably
|
16
16
|
# more likely that you'll want to define a method that returns a new set of
|
17
17
|
# data each time (via +Faker+, +Array#sample+, etc.). When using
|
18
|
-
# +
|
18
|
+
# +csv+, +data+ will be set to the data within the csv. You can
|
19
19
|
# override this.
|
20
20
|
#
|
21
21
|
# @return [Array]
|
@@ -28,40 +28,49 @@ module Planter
|
|
28
28
|
#
|
29
29
|
# @param [Symbol] seeding_method
|
30
30
|
#
|
31
|
-
# @
|
31
|
+
# @kwarg [Integer] number_of_records
|
32
|
+
#
|
33
|
+
# @kwarg [String] model
|
34
|
+
#
|
35
|
+
# @kwarg [String] parent_model
|
36
|
+
#
|
37
|
+
# @kwarg [Symbol, String] association
|
38
|
+
#
|
39
|
+
# @kwarg [Symbol, String] csv_name
|
32
40
|
#
|
33
41
|
# @example
|
34
42
|
# require 'planter'
|
35
43
|
# class UsersSeeder < Planter::Seeder
|
36
|
-
# seeding_method :
|
44
|
+
# seeding_method :csv,
|
45
|
+
# number_of_records: 2,
|
37
46
|
# model: 'User'
|
38
47
|
# parent_model: 'Person',
|
39
48
|
# association: :users,
|
40
|
-
#
|
49
|
+
# csv_name: :awesome_users
|
41
50
|
# end
|
42
|
-
def self.seeding_method(
|
51
|
+
def self.seeding_method(
|
52
|
+
method,
|
53
|
+
number_of_records: 1,
|
54
|
+
model: to_s.delete_suffix('Seeder').singularize,
|
55
|
+
parent_model: nil,
|
56
|
+
association: nil,
|
57
|
+
csv_name: nil
|
58
|
+
)
|
43
59
|
if !SEEDING_METHODS.include?(method.intern)
|
44
60
|
raise ArgumentError, "Method must be one of #{SEEDING_METHODS.join(', ')}"
|
45
|
-
elsif
|
61
|
+
elsif association && !parent_model
|
46
62
|
raise ArgumentError, "Must specify :parent_model with :association"
|
47
63
|
end
|
48
64
|
|
49
65
|
@seeding_method = method
|
50
|
-
@number_of_records =
|
51
|
-
@model =
|
52
|
-
@parent_model =
|
53
|
-
@association = @parent_model &&
|
54
|
-
|
55
|
-
end
|
56
|
-
return unless @seeding_method == :standard_csv
|
57
|
-
|
58
|
-
@csv_file = options.fetch(:csv_file, Rails.root.join(
|
59
|
-
Planter.config.csv_files_directory,
|
60
|
-
"#{to_s.delete_suffix('Seeder').underscore}.csv"
|
61
|
-
).to_s)
|
66
|
+
@number_of_records = number_of_records
|
67
|
+
@model = model
|
68
|
+
@parent_model = parent_model
|
69
|
+
@association = @parent_model && (association || determine_association)
|
70
|
+
@csv_file = determine_csv_filename(csv_name) if @seeding_method == :csv
|
62
71
|
end
|
63
72
|
|
64
|
-
def self.determine_association
|
73
|
+
def self.determine_association # :nodoc:
|
65
74
|
associations =
|
66
75
|
@parent_model.constantize.reflect_on_all_associations.map(&:name)
|
67
76
|
table = to_s.delete_suffix('Seeder').underscore.split('/').last
|
@@ -70,10 +79,23 @@ module Planter
|
|
70
79
|
return t if associations.include?(t)
|
71
80
|
end
|
72
81
|
|
73
|
-
raise ArgumentError, '
|
82
|
+
raise ArgumentError, "Couldn't determine association name"
|
74
83
|
end
|
75
84
|
private_class_method :determine_association
|
76
85
|
|
86
|
+
def self.determine_csv_filename(csv_name) # :nodoc:
|
87
|
+
file = (
|
88
|
+
csv_name || "#{to_s.delete_suffix('Seeder').underscore}"
|
89
|
+
).to_s + '.csv'
|
90
|
+
[file, "#{file}.erb"].each do |f|
|
91
|
+
fname = Rails.root.join(Planter.config.csv_files_directory, f).to_s
|
92
|
+
return fname if File.file?(fname)
|
93
|
+
end
|
94
|
+
|
95
|
+
raise ArgumentError, "Couldn't find csv for #{@model}"
|
96
|
+
end
|
97
|
+
private_class_method :determine_csv_filename
|
98
|
+
|
77
99
|
##
|
78
100
|
# The default seed method. To use this method, your class must provide a
|
79
101
|
# valid +seeding_method+, and not implement its own +seed+ method.
|
@@ -181,10 +203,13 @@ module Planter
|
|
181
203
|
|
182
204
|
def validate_attributes # :nodoc:
|
183
205
|
case seeding_method.intern
|
184
|
-
when :
|
185
|
-
|
206
|
+
when :csv
|
207
|
+
contents = File.read(csv_file)
|
208
|
+
if csv_file.end_with?('.erb')
|
209
|
+
contents = ERB.new(contents, trim_mode: '<>').result(binding)
|
210
|
+
end
|
186
211
|
|
187
|
-
@data ||= ::CSV.
|
212
|
+
@data ||= ::CSV.parse(contents, headers: true).map(&:to_hash)
|
188
213
|
when :data_array
|
189
214
|
raise "Must define '@data'" if public_send(:data).nil?
|
190
215
|
else
|
data/lib/planter/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: planter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evan Gray
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-05-
|
11
|
+
date: 2021-05-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|