planter 0.0.12 → 0.0.13

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: 37c19399e25198fe0545690d5b5568c5bd91b4736258af3d8664c36abe882889
4
- data.tar.gz: a399b54a606d7c0e98d49f0bad5abff1fc4726d96eab3573c82d1dc649b83b1e
3
+ metadata.gz: 23a4116a1fe8a8d499e8522e5d4da4dadd76cb95ba917d54855f36adeaec519f
4
+ data.tar.gz: 523fcef8dd453614e4c1045aafd1ae57b54e7b6043c4fac3b3638ef35c577862
5
5
  SHA512:
6
- metadata.gz: 6275a47f463227b86374e327781edd92bbd9cdf9399bafa68c6c4cb3316ae812cf081509a26e81e04101f9cd4af43396f2c008477741f00f475ea09436353ba8
7
- data.tar.gz: 1a5a27d9253b78ae0c1c0526c1f8dcb977a428ec5c62cb4239f5542413e1f5020c68d8ee0d356ded4c1f3094c7f449f03fefb72180250388de81f8c44c2c6a63
6
+ metadata.gz: eb5d69e3807c0595c4a149cc0eeb487712a18381d86cad82a9e459ca28a19286c77e35b92820932b9be3ced37563632f7d8a27d1a85bf15c55621895bc5f923b
7
+ data.tar.gz: 9d32d66773a92b393fdac8a90a67ca410251588d61270a35ba8a21f8d1afe74963c5ef81702fffc33ffb59674b7fb953029f19d887f17a26bd7a5b7ae3522df3
data/README.md CHANGED
@@ -60,7 +60,7 @@ 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
  ##
@@ -68,7 +68,12 @@ Planter.configure do |config|
68
68
  # config.csv_files_directory = 'db/seed_files'
69
69
 
70
70
  ##
71
- # The default trim mode for ERB.
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()
72
77
  # config.erb_trim_mode = nil
73
78
  end
74
79
  ```
@@ -257,4 +262,4 @@ Request.
257
262
  I do these projects for fun, and I enjoy knowing that they're helpful to people.
258
263
  Consider starring [the repository](https://github.com/evanthegrayt/planter)
259
264
  if you like it! If you love it, follow me [on
260
- Github](https://github.com/evanthegrayt)!
265
+ GitHub](https://github.com/evanthegrayt)!
@@ -19,7 +19,7 @@ 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
  ##
@@ -27,7 +27,12 @@ module Planter
27
27
  # config.csv_files_directory = 'db/seed_files'
28
28
 
29
29
  ##
30
- # The default trim mode for ERB.
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()
31
36
  # config.erb_trim_mode = nil
32
37
  end
33
38
  EOF
@@ -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)
@@ -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,121 +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.
108
- #
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
- # @kwarg [String] erb_trim_mode
105
+ # What trim mode should ERB use?
124
106
  #
125
- # @example
126
- # require 'planter'
127
- # class UsersSeeder < Planter::Seeder
128
- # seeding_method :csv,
129
- # number_of_records: 2,
130
- # model: 'User'
131
- # parent_model: 'Person',
132
- # association: :users,
133
- # csv_name: :awesome_users,
134
- # unique_columns %i[username email],
135
- # erb_trim_mode: '<>'
136
- # end
137
- def self.seeding_method(
138
- method,
139
- number_of_records: 1,
140
- model: nil,
141
- parent_model: nil,
142
- association: nil,
143
- csv_name: nil,
144
- unique_columns: nil,
145
- erb_trim_mode: nil
146
- )
147
- if !SEEDING_METHODS.include?(method.intern)
148
- raise ArgumentError, "Method must be one of #{SEEDING_METHODS.join(', ')}"
149
- elsif association && !parent_model
150
- raise ArgumentError, "Must specify :parent_model with :association"
151
- end
152
-
153
- @seeding_method = method
154
- @number_of_records = number_of_records
155
- @model = model || to_s.delete_suffix('Seeder').singularize
156
- @parent_model = parent_model
157
- @association = @parent_model && (association || determine_association)
158
- @csv_file = determine_csv_filename(csv_name) if @seeding_method == :csv
159
- @erb_trim_mode = erb_trim_mode || Planter.config.erb_trim_mode
160
- @unique_columns =
161
- case unique_columns
162
- when String, Symbol then [unique_columns.intern]
163
- when Array then unique_columns.map(&:intern)
164
- end
165
- end
166
-
167
- def self.determine_association # :nodoc:
168
- associations =
169
- @parent_model.constantize.reflect_on_all_associations.map(&:name)
170
- table = to_s.delete_suffix('Seeder').underscore.split('/').last
171
-
172
- [table, table.singularize].map(&:intern).each do |t|
173
- return t if associations.include?(t)
174
- end
175
-
176
- raise ArgumentError, "Couldn't determine association name"
177
- end
178
- private_class_method :determine_association
179
-
180
- def self.determine_csv_filename(csv_name) # :nodoc:
181
- file = (
182
- csv_name || "#{to_s.delete_suffix('Seeder').underscore}"
183
- ).to_s + '.csv'
184
- [file, "#{file}.erb"].each do |f|
185
- fname = Rails.root.join(Planter.config.csv_files_directory, f).to_s
186
- return fname if ::File.file?(fname)
187
- end
188
-
189
- raise ArgumentError, "Couldn't find csv for #{@model}"
190
- end
191
- private_class_method :determine_csv_filename
192
-
193
- ##
194
- # The default seed method. To use this method, your class must provide a
195
- # valid +seeding_method+, and not implement its own +seed+ method.
196
- def seed
197
- validate_attributes
198
-
199
- parent_model ? create_records_from_parent : create_records
200
- end
201
-
202
- protected
107
+ # @return [String]
108
+ class_attribute :erb_trim_mode
203
109
 
204
110
  ##
205
- # 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.
206
113
  #
207
- # @return [Symbol]
208
- def seeding_method
209
- @seeding_method ||= self.class.instance_variable_get('@seeding_method')
210
- end
114
+ # @return [Array]
115
+ class_attribute :unique_columns
211
116
 
212
117
  ##
213
118
  # The model for the table being seeded. If the model name you need is
214
119
  # different, change via +seeding_method+.
215
120
  #
216
121
  # @return [String]
217
- def model
218
- @model ||= self.class.instance_variable_get('@model')
219
- end
122
+ class_attribute :model
220
123
 
221
124
  ##
222
125
  # The model of the parent. When provided with +association+, records in the
@@ -224,18 +127,7 @@ module Planter
224
127
  # class must set this attribute via +seeding_method+.
225
128
  #
226
129
  # @return [String]
227
- def parent_model
228
- @parent_model ||= self.class.instance_variable_get('@parent_model')
229
- end
230
-
231
- ##
232
- # When using +parent_model+, the association name. Your class can set this
233
- # attribute via +seeding_method+.
234
- #
235
- # @return [Symbol]
236
- def association
237
- @association ||= self.class.instance_variable_get('@association')
238
- end
130
+ class_attribute :parent_model
239
131
 
240
132
  ##
241
133
  # The number of records to create from each record in the +data+ array. If
@@ -243,36 +135,131 @@ module Planter
243
135
  # +seeding_method+.
244
136
  #
245
137
  # @return [Integer]
246
- def number_of_records
247
- @number_of_records ||=
248
- self.class.instance_variable_get('@number_of_records')
249
- 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
250
146
 
251
147
  ##
252
148
  # The csv file corresponding to the model.
253
149
  #
254
150
  # @return [String]
255
- def csv_file
256
- @csv_file ||= self.class.instance_variable_get('@csv_file')
257
- end
151
+ class_attribute :csv_file
258
152
 
259
153
  ##
260
- # When creating a record, the fields that will be used to look up the
261
- # record. If it already exists, a new one will not be created.
154
+ # The seeding method specified.
262
155
  #
263
- # @return [Array]
264
- def unique_columns
265
- @unique_columns ||= self.class.instance_variable_get('@unique_columns')
156
+ # @return [Symbol]
157
+ class_attribute :seeding_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.seeding_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 seeding_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
266
250
  end
267
251
 
268
252
  ##
269
- # What trim mode should ERB use?
270
- #
271
- # @return [String]
272
- def erb_trim_mode
273
- @erb_trim_mode ||= self.class.instance_variable_get('@erb_trim_mode')
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
274
259
  end
275
260
 
261
+ protected
262
+
276
263
  ##
277
264
  # Creates records from the +data+ attribute.
278
265
  def create_records
@@ -21,7 +21,7 @@ module Planter
21
21
  # Patch version.
22
22
  #
23
23
  # @return [Integer]
24
- PATCH = 12
24
+ PATCH = 13
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.12
4
+ version: 0.0.13
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-11-15 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