planter 0.0.11 → 0.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9eab293e0cec74131351f8e1ff29919165da5e7eeb9cea2d305802f8cf5e1c8f
4
- data.tar.gz: 6e06237482fb476e3a664fead3bfd64d15013f5570316964360161c9e05a25a4
3
+ metadata.gz: 76a26d2138c0fff92d6c99503e2f5b5c88aa5333d3151b4c4c27ed4b29667f7b
4
+ data.tar.gz: 322850357a4886588218e7a7800b5a0f23310506f650c284acb963d0a3a994ae
5
5
  SHA512:
6
- metadata.gz: 3d28919333f86c7e0d6be6e4a12a4dea558ec0d734ccb63457931af6c8070f71ff76a90c370d2e26a8500106dbb81585ca9685b829c2cf220cd770be229e274d
7
- data.tar.gz: a40e5e9ae771224e486c8263d2194fc5de1256b6d6a0d9d880ef44aea01294160fbbe1f139b74a375f4adb3e6372e155700968de0d862918ad0662ce3a2fc80c
6
+ metadata.gz: 9e7fb3cdefd3dd6ca635c11a9f5725dbc83299d2e3b5df59dbdd9f3eff0dfc718f2d6ab00fcd6b8de56c7a37abaca8c5af73b697e31ef406ae7cf3f75380191e
7
+ data.tar.gz: 51ffeee246719300c2474aa86fa36b306f8996515da57dc308c4a8aa78f14bbed59b80596575d1f82534a809447b283ceed2c28e11daf92851ecfefd5b61a2cd
data/README.md CHANGED
@@ -60,12 +60,21 @@ Planter.configure do |config|
60
60
  ]
61
61
 
62
62
  ##
63
- # The directory where the seeder files are kept.
63
+ # The directory where the seeders are kept.
64
64
  # config.seeders_directory = 'db/seeds'
65
65
 
66
66
  ##
67
67
  # The directory where CSVs are kept.
68
68
  # config.csv_files_directory = 'db/seed_files'
69
+
70
+ ##
71
+ # The default trim mode for ERB. Valid modes are:
72
+ # '%' enables Ruby code processing for lines beginning with %
73
+ # '<>' omit newline for lines starting with <% and ending in %>
74
+ # '>' omit newline for lines ending in %>
75
+ # '-' omit blank lines ending in -%>
76
+ # I recommend reading the help documentation for ERB::new()
77
+ # config.erb_trim_mode = nil
69
78
  end
70
79
  ```
71
80
 
@@ -135,18 +144,37 @@ class UsersSeeder < Planter::Seeder
135
144
  end
136
145
  ```
137
146
 
138
- `ERB` can be used in the CSV files if you name it with `.erb` at the end of the
139
- file name. For example, `users.csv.erb`. Note that lines starting with `<%` and
140
- ending with `%>` will not be considered rows, so you can use `ERB` rows to set
141
- values. For example:
147
+ `ERB` can be used in the CSV files if you end the file name with `.csv.erb`.
148
+ For example, `users.csv.erb`.
142
149
 
143
150
  ```
144
- email,login_attempts
145
- <% count = 1 %>
146
- test2@example.com,<%= count += 1 %>
147
- test2@example.com,<%= count += 1 %>
151
+ participant_id,name
152
+ <%= Participant.find_by(email: 'test1@example.com').id %>,"Test User1"
153
+ <%= Participant.find_by(email: 'test2@example.com').id %>,"Test User2"
148
154
  ```
149
155
 
156
+ Note that, if you need to change the trim mode for ERB, you can set a default in
157
+ the initializer.
158
+
159
+ ```ruby
160
+ Planter.configure do |config|
161
+ config.seeders = %i[
162
+ users
163
+ ]
164
+ config.erb_trim_mode = '<>'
165
+ end
166
+ ```
167
+
168
+ ...or, for individual seeders, via `seeding_method`.
169
+
170
+ ```ruby
171
+ class UsersSeeder < Planter::Seeder
172
+ seeding_method :csv, erb_trim_mode: '<>'
173
+ end
174
+ ```
175
+
176
+ For help with `erb_trim_mode`, see the help documentation for `ERB::new`.
177
+
150
178
  Running `rails planter:seed` will now seed your `users` table.
151
179
 
152
180
  ## Seeding from a data array
@@ -234,4 +262,4 @@ Request.
234
262
  I do these projects for fun, and I enjoy knowing that they're helpful to people.
235
263
  Consider starring [the repository](https://github.com/evanthegrayt/planter)
236
264
  if you like it! If you love it, follow me [on
237
- Github](https://github.com/evanthegrayt)!
265
+ GitHub](https://github.com/evanthegrayt)!
@@ -19,12 +19,21 @@ module Planter
19
19
  ]
20
20
 
21
21
  ##
22
- # The directory where the seeder files are kept.
22
+ # The directory where the seeders are kept.
23
23
  # config.seeders_directory = 'db/seeds'
24
24
 
25
25
  ##
26
26
  # The directory where CSVs are kept.
27
27
  # config.csv_files_directory = 'db/seed_files'
28
+
29
+ ##
30
+ # The default trim mode for ERB. Valid modes are:
31
+ # '%' enables Ruby code processing for lines beginning with %
32
+ # '<>' omit newline for lines starting with <% and ending in %>
33
+ # '>' omit newline for lines ending in %>
34
+ # '-' omit blank lines ending in -%>
35
+ # I recommend reading the help documentation for ERB::new()
36
+ # config.erb_trim_mode = nil
28
37
  end
29
38
  EOF
30
39
  end
@@ -3,7 +3,7 @@ module Planter
3
3
  class SeederGenerator < Rails::Generators::Base
4
4
  argument :seeder, required: true
5
5
 
6
- desc "This generator creates a seeder file at #{::Planter.config.seeders_directory}"
6
+ desc "Creates a seeder file at #{::Planter.config.seeders_directory}"
7
7
 
8
8
  def generate_seeders
9
9
  seeder == 'ALL' ? tables.each { |t| generate(t) } : generate(seeder)
@@ -42,6 +42,15 @@ module Planter
42
42
  # @return [Boolean]
43
43
  attr_accessor :quiet
44
44
 
45
+ ##
46
+ # The default trim mode for ERB. Must be "%", "<>", ">", or "-".
47
+ # For more information, see documentation for +ERB::new+.
48
+ #
49
+ # @param [String] erb_trim_mode
50
+ #
51
+ # @return [String]
52
+ attr_accessor :erb_trim_mode
53
+
45
54
  ##
46
55
  # Create a new instance of the config.
47
56
  def initialize
@@ -2,13 +2,13 @@
2
2
 
3
3
  module Planter
4
4
  ##
5
- # Class that seeders should inherit from. Seeder files should be in
6
- # +db/seeds+, and named +TABLE_seeder.rb+, where +TABLE+ is the name of the
7
- # table being seeded (I.E. +users_seeder.rb+). If your seeder is named
8
- # differently than the table, you'll need to specify the table with the
9
- # +model+ option. The seeder's class name should be the same as the file
10
- # name, but camelized. So, +UsersSeeder+. The directory where the seeder
11
- # files are located can be changed via an initializer.
5
+ # Class that seeders should inherit from. Seeders should be in +db/seeds+,
6
+ # and named +TABLE_seeder.rb+, where +TABLE+ is the name of the table being
7
+ # seeded (I.E. +users_seeder.rb+). If your seeder is named differently than
8
+ # the table, you'll need to specify the table with the +model+ option. The
9
+ # seeder's class name should be the same as the file name, but camelized. So,
10
+ # +UsersSeeder+. The directory where the seeder files are located can be
11
+ # changed via an initializer.
12
12
  #
13
13
  # The most basic way to seed is to have a CSV file with the same name as the
14
14
  # table in +db/seed_files/+. So, +users.csv+. This CSV should have the
@@ -102,116 +102,24 @@ module Planter
102
102
  attr_reader :data
103
103
 
104
104
  ##
105
- # If your class is going to use the inherited +seed+ method, you must tell
106
- # it which +seeding_method+ to use. The argument to this method must be
107
- # included in the +SEEDING_METHODS+ array.
105
+ # What trim mode should ERB use?
108
106
  #
109
- # @param [Symbol] seeding_method
110
- #
111
- # @kwarg [Integer] number_of_records
112
- #
113
- # @kwarg [String] model
114
- #
115
- # @kwarg [String] parent_model
116
- #
117
- # @kwarg [Symbol, String] association
118
- #
119
- # @kwarg [Symbol, String] csv_name
120
- #
121
- # @kwarg [Symbol, String] unique_columns
122
- #
123
- # @example
124
- # require 'planter'
125
- # class UsersSeeder < Planter::Seeder
126
- # seeding_method :csv,
127
- # number_of_records: 2,
128
- # model: 'User'
129
- # parent_model: 'Person',
130
- # association: :users,
131
- # csv_name: :awesome_users,
132
- # unique_columns %i[username email]
133
- # end
134
- def self.seeding_method(
135
- method,
136
- number_of_records: 1,
137
- model: to_s.delete_suffix('Seeder').singularize,
138
- parent_model: nil,
139
- association: nil,
140
- csv_name: nil,
141
- unique_columns: nil
142
- )
143
- if !SEEDING_METHODS.include?(method.intern)
144
- raise ArgumentError, "Method must be one of #{SEEDING_METHODS.join(', ')}"
145
- elsif association && !parent_model
146
- raise ArgumentError, "Must specify :parent_model with :association"
147
- end
148
-
149
- @seeding_method = method
150
- @number_of_records = number_of_records
151
- @model = model
152
- @parent_model = parent_model
153
- @association = @parent_model && (association || determine_association)
154
- @csv_file = determine_csv_filename(csv_name) if @seeding_method == :csv
155
- @unique_columns =
156
- case unique_columns
157
- when String, Symbol then [unique_columns.intern]
158
- when Array then unique_columns.map(&:intern)
159
- end
160
- end
161
-
162
- def self.determine_association # :nodoc:
163
- associations =
164
- @parent_model.constantize.reflect_on_all_associations.map(&:name)
165
- table = to_s.delete_suffix('Seeder').underscore.split('/').last
166
-
167
- [table, table.singularize].map(&:intern).each do |t|
168
- return t if associations.include?(t)
169
- end
170
-
171
- raise ArgumentError, "Couldn't determine association name"
172
- end
173
- private_class_method :determine_association
174
-
175
- def self.determine_csv_filename(csv_name) # :nodoc:
176
- file = (
177
- csv_name || "#{to_s.delete_suffix('Seeder').underscore}"
178
- ).to_s + '.csv'
179
- [file, "#{file}.erb"].each do |f|
180
- fname = Rails.root.join(Planter.config.csv_files_directory, f).to_s
181
- return fname if ::File.file?(fname)
182
- end
183
-
184
- raise ArgumentError, "Couldn't find csv for #{@model}"
185
- end
186
- private_class_method :determine_csv_filename
187
-
188
- ##
189
- # The default seed method. To use this method, your class must provide a
190
- # valid +seeding_method+, and not implement its own +seed+ method.
191
- def seed
192
- validate_attributes
193
-
194
- parent_model ? create_records_from_parent : create_records
195
- end
196
-
197
- protected
107
+ # @return [String]
108
+ class_attribute :erb_trim_mode
198
109
 
199
110
  ##
200
- # The seeding method specified.
111
+ # When creating a record, the fields that will be used to look up the
112
+ # record. If it already exists, a new one will not be created.
201
113
  #
202
- # @return [Symbol]
203
- def seeding_method
204
- @seeding_method ||= self.class.instance_variable_get('@seeding_method')
205
- end
114
+ # @return [Array]
115
+ class_attribute :unique_columns
206
116
 
207
117
  ##
208
118
  # The model for the table being seeded. If the model name you need is
209
119
  # different, change via +seeding_method+.
210
120
  #
211
121
  # @return [String]
212
- def model
213
- @model ||= self.class.instance_variable_get('@model')
214
- end
122
+ class_attribute :model
215
123
 
216
124
  ##
217
125
  # The model of the parent. When provided with +association+, records in the
@@ -219,18 +127,7 @@ module Planter
219
127
  # class must set this attribute via +seeding_method+.
220
128
  #
221
129
  # @return [String]
222
- def parent_model
223
- @parent_model ||= self.class.instance_variable_get('@parent_model')
224
- end
225
-
226
- ##
227
- # When using +parent_model+, the association name. Your class can set this
228
- # attribute via +seeding_method+.
229
- #
230
- # @return [Symbol]
231
- def association
232
- @association ||= self.class.instance_variable_get('@association')
233
- end
130
+ class_attribute :parent_model
234
131
 
235
132
  ##
236
133
  # The number of records to create from each record in the +data+ array. If
@@ -238,28 +135,131 @@ module Planter
238
135
  # +seeding_method+.
239
136
  #
240
137
  # @return [Integer]
241
- def number_of_records
242
- @number_of_records ||=
243
- self.class.instance_variable_get('@number_of_records')
244
- end
138
+ class_attribute :number_of_records
139
+
140
+ ##
141
+ # When using +parent_model+, the association name. Your class can set this
142
+ # attribute via +seeding_method+.
143
+ #
144
+ # @return [Symbol]
145
+ class_attribute :association
245
146
 
246
147
  ##
247
148
  # The csv file corresponding to the model.
248
149
  #
249
150
  # @return [String]
250
- def csv_file
251
- @csv_file ||= self.class.instance_variable_get('@csv_file')
252
- end
151
+ class_attribute :csv_file
253
152
 
254
153
  ##
255
- # When creating a record, the fields that will be used to look up the
256
- # record. If it already exists, a new one will not be created.
154
+ # The seeding method specified.
257
155
  #
258
- # @return [Array]
259
- def unique_columns
260
- @unique_columns ||= self.class.instance_variable_get('@unique_columns')
156
+ # @return [Symbol]
157
+ class_attribute :seed_method
158
+
159
+ ##
160
+ # Access the metaclass so we can define public and private class methods.
161
+ class << self
162
+ ##
163
+ # If your class is going to use the inherited +seed+ method, you must tell
164
+ # it which +seeding_method+ to use. The argument to this method must be
165
+ # included in the +SEEDING_METHODS+ array.
166
+ #
167
+ # @param [Symbol] seeding_method
168
+ #
169
+ # @kwarg [Integer] number_of_records
170
+ #
171
+ # @kwarg [String] model
172
+ #
173
+ # @kwarg [String] parent_model
174
+ #
175
+ # @kwarg [Symbol, String] association
176
+ #
177
+ # @kwarg [Symbol, String] csv_name
178
+ #
179
+ # @kwarg [Symbol, String] unique_columns
180
+ #
181
+ # @kwarg [String] erb_trim_mode
182
+ #
183
+ # @example
184
+ # require 'planter'
185
+ # class UsersSeeder < Planter::Seeder
186
+ # seeding_method :csv,
187
+ # number_of_records: 2,
188
+ # model: 'User'
189
+ # parent_model: 'Person',
190
+ # association: :users,
191
+ # csv_name: :awesome_users,
192
+ # unique_columns %i[username email],
193
+ # erb_trim_mode: '<>'
194
+ # end
195
+ def seeding_method(
196
+ method,
197
+ number_of_records: 1,
198
+ model: nil,
199
+ parent_model: nil,
200
+ association: nil,
201
+ csv_name: nil,
202
+ unique_columns: nil,
203
+ erb_trim_mode: nil
204
+ )
205
+ if !SEEDING_METHODS.include?(method.intern)
206
+ raise ArgumentError, "Method must be: #{SEEDING_METHODS.join(', ')}"
207
+ elsif association && !parent_model
208
+ raise ArgumentError, "Must specify :parent_model with :association"
209
+ end
210
+
211
+ self.seed_method = method
212
+ self.number_of_records = number_of_records
213
+ self.model = model || to_s.delete_suffix('Seeder').singularize
214
+ self.parent_model = parent_model
215
+ self.association = parent_model && (association || determine_association)
216
+ self.csv_file = determine_csv_filename(csv_name) if self.seed_method == :csv
217
+ self.erb_trim_mode = erb_trim_mode || Planter.config.erb_trim_mode
218
+ self.unique_columns =
219
+ case unique_columns
220
+ when String, Symbol then [unique_columns.intern]
221
+ when Array then unique_columns.map(&:intern)
222
+ end
223
+ end
224
+
225
+ private
226
+
227
+ def determine_association # :nodoc:
228
+ associations =
229
+ parent_model.constantize.reflect_on_all_associations.map(&:name)
230
+ table = to_s.delete_suffix('Seeder').underscore.split('/').last
231
+
232
+ [table, table.singularize].map(&:intern).each do |t|
233
+ return t if associations.include?(t)
234
+ end
235
+
236
+ raise ArgumentError, "Couldn't determine association name"
237
+ end
238
+
239
+ def determine_csv_filename(csv_name) # :nodoc:
240
+ file = (
241
+ csv_name || "#{to_s.delete_suffix('Seeder').underscore}"
242
+ ).to_s + '.csv'
243
+ [file, "#{file}.erb"].each do |f|
244
+ fname = Rails.root.join(Planter.config.csv_files_directory, f).to_s
245
+ return fname if ::File.file?(fname)
246
+ end
247
+
248
+ raise ArgumentError, "Couldn't find csv for #{model}"
249
+ end
261
250
  end
262
251
 
252
+ ##
253
+ # The default seed method. To use this method, your class must provide a
254
+ # valid +seeding_method+, and not implement its own +seed+ method.
255
+ def seed
256
+ validate_attributes
257
+
258
+ parent_model ? create_records_from_parent : create_records
259
+ end
260
+
261
+ protected
262
+
263
263
  ##
264
264
  # Creates records from the +data+ attribute.
265
265
  def create_records
@@ -305,11 +305,11 @@ module Planter
305
305
  end
306
306
 
307
307
  def validate_attributes # :nodoc:
308
- case seeding_method.intern
308
+ case seed_method.intern
309
309
  when :csv
310
310
  contents = ::File.read(csv_file)
311
311
  if csv_file.end_with?('.erb')
312
- contents = ERB.new(contents, trim_mode: '<>').result(binding)
312
+ contents = ERB.new(contents, trim_mode: erb_trim_mode).result(binding)
313
313
  end
314
314
 
315
315
  @data ||= ::CSV.parse(
@@ -21,7 +21,7 @@ module Planter
21
21
  # Patch version.
22
22
  #
23
23
  # @return [Integer]
24
- PATCH = 11
24
+ PATCH = 15
25
25
 
26
26
  ##
27
27
  # Version as +[MAJOR, MINOR, PATCH]+
data/lib/planter.rb CHANGED
@@ -67,7 +67,7 @@ module Planter
67
67
  # Planter.seed
68
68
  def self.seed
69
69
  seeders = ENV['SEEDERS']&.split(',') || config.seeders&.map(&:to_s)
70
- raise RuntimeError, 'No seeders specified' unless seeders&.any?
70
+ raise RuntimeError, 'No seeders specified' if seeders.blank?
71
71
 
72
72
  seeders.each do |s|
73
73
  require Rails.root.join(config.seeders_directory, "#{s}_seeder.rb").to_s
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.11
4
+ version: 0.0.15
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-08-30 00:00:00.000000000 Z
11
+ date: 2021-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,20 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 6.1.3
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 6.1.3.1
19
+ version: 6.1.4.4
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
24
  - - "~>"
28
25
  - !ruby/object:Gem::Version
29
- version: 6.1.3
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: 6.1.3.1
26
+ version: 6.1.4.4
33
27
  description: Create a seeder for each table in your database, and easily seed from
34
28
  CSV or custom methods
35
29
  email: