planter 0.0.9 → 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: 61c2952a2b44b89c48245554cd6be4b19598891aa958b3f2f0dd3e3d2548a079
4
- data.tar.gz: 91e099c652ce4bfb0bf87320f9efa3d1e0e0826ae031e17e967563beb8561f19
3
+ metadata.gz: 23a4116a1fe8a8d499e8522e5d4da4dadd76cb95ba917d54855f36adeaec519f
4
+ data.tar.gz: 523fcef8dd453614e4c1045aafd1ae57b54e7b6043c4fac3b3638ef35c577862
5
5
  SHA512:
6
- metadata.gz: d70a187f4064ab1081a5a4b3df180f4ced5436601151029fca74b985cc0658e0422b56f338693b08a885a5dfc7d241357ef86144373825d8553117c3e7f6f30e
7
- data.tar.gz: 923979e971e3cbc573a9eaffbc39481d8fc8a4930894de1ea8d8f4ef79c3f6d7bbaba0ab111d0099ee67298b336373a0836802126c5d5bba4c237eb41ab0c5d8
6
+ metadata.gz: eb5d69e3807c0595c4a149cc0eeb487712a18381d86cad82a9e459ca28a19286c77e35b92820932b9be3ced37563632f7d8a27d1a85bf15c55621895bc5f923b
7
+ data.tar.gz: 9d32d66773a92b393fdac8a90a67ca410251588d61270a35ba8a21f8d1afe74963c5ef81702fffc33ffb59674b7fb953029f19d887f17a26bd7a5b7ae3522df3
data/README.md CHANGED
@@ -19,10 +19,12 @@ Features include:
19
19
  You can view the documentation [here](https://evanthegrayt.github.io/planter/).
20
20
 
21
21
  ## Installation
22
- Add this line to your application's Gemfile:
22
+ Add the following line to your application's Gemfile. Because this plugin is
23
+ currently a pre-release version, it's recommended to lock it to a specific
24
+ version, as breaking changes may occur, even at the patch level.
23
25
 
24
26
  ```ruby
25
- gem 'planter'
27
+ gem 'planter', '0.0.10'
26
28
  ```
27
29
 
28
30
  And then execute:
@@ -51,19 +53,28 @@ Planter.configure do |config|
51
53
  # The list of seeders. These files are stored in the
52
54
  # config.seeders_directory, which can be changed below. When a new
53
55
  # seeder is generated, it will be appended to the bottom of this
54
- # list. If the order is incorrect, you'll need to adjust the it.
56
+ # list. If the order is incorrect, you'll need to adjust it.
55
57
  # Just be sure to keep the ending bracket on its own line, or the
56
58
  # generator won't know where to put new elements.
57
59
  config.seeders = %i[
58
60
  ]
59
61
 
60
62
  ##
61
- # The directory where the seeder files are kept.
63
+ # The directory where the seeders are kept.
62
64
  # config.seeders_directory = 'db/seeds'
63
65
 
64
66
  ##
65
67
  # The directory where CSVs are kept.
66
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
67
78
  end
68
79
  ```
69
80
 
@@ -133,18 +144,37 @@ class UsersSeeder < Planter::Seeder
133
144
  end
134
145
  ```
135
146
 
136
- `ERB` can be used in the CSV files if you name it with `.erb` at the end of the
137
- file name. For example, `users.csv.erb`. Note that lines starting with `<%` and
138
- ending with `%>` will not be considered rows, so you can use `ERB` rows to set
139
- 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`.
149
+
150
+ ```
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"
154
+ ```
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`.
140
169
 
141
- ```csv.erb
142
- email,login_attempts
143
- <% count = 1 %>
144
- test2@example.com,<%= count += 1 %>
145
- test2@example.com,<%= count += 1 %>
170
+ ```ruby
171
+ class UsersSeeder < Planter::Seeder
172
+ seeding_method :csv, erb_trim_mode: '<>'
173
+ end
146
174
  ```
147
175
 
176
+ For help with `erb_trim_mode`, see the help documentation for `ERB::new`.
177
+
148
178
  Running `rails planter:seed` will now seed your `users` table.
149
179
 
150
180
  ## Seeding from a data array
@@ -232,4 +262,4 @@ Request.
232
262
  I do these projects for fun, and I enjoy knowing that they're helpful to people.
233
263
  Consider starring [the repository](https://github.com/evanthegrayt/planter)
234
264
  if you like it! If you love it, follow me [on
235
- Github](https://github.com/evanthegrayt)!
265
+ GitHub](https://github.com/evanthegrayt)!
@@ -12,19 +12,28 @@ module Planter
12
12
  # The list of seeders. These files are stored in the
13
13
  # config.seeders_directory, which can be changed below. When a new
14
14
  # seeder is generated, it will be appended to the bottom of this
15
- # list. If the order is incorrect, you'll need to adjust the it.
15
+ # list. If the order is incorrect, you'll need to adjust it.
16
16
  # Just be sure to keep the ending bracket on its own line, or the
17
17
  # generator won't know where to put new elements.
18
18
  config.seeders = %i[
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 :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
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
@@ -309,7 +309,7 @@ module Planter
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 = 9
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.9
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Gray
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-11 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
@@ -57,7 +57,7 @@ metadata:
57
57
  homepage_uri: https://github.com/evanthegrayt/planter
58
58
  source_code_uri: https://github.com/evanthegrayt/planter
59
59
  documentation_uri: https://evanthegrayt.github.io/planter/
60
- post_install_message:
60
+ post_install_message:
61
61
  rdoc_options: []
62
62
  require_paths:
63
63
  - lib
@@ -72,8 +72,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
72
  - !ruby/object:Gem::Version
73
73
  version: '0'
74
74
  requirements: []
75
- rubygems_version: 3.2.15
76
- signing_key:
75
+ rubygems_version: 3.2.22
76
+ signing_key:
77
77
  specification_version: 4
78
78
  summary: Framework for seeding rails applications.
79
79
  test_files: []