planter 0.0.11 → 0.0.12

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: 37c19399e25198fe0545690d5b5568c5bd91b4736258af3d8664c36abe882889
4
+ data.tar.gz: a399b54a606d7c0e98d49f0bad5abff1fc4726d96eab3573c82d1dc649b83b1e
5
5
  SHA512:
6
- metadata.gz: 3d28919333f86c7e0d6be6e4a12a4dea558ec0d734ccb63457931af6c8070f71ff76a90c370d2e26a8500106dbb81585ca9685b829c2cf220cd770be229e274d
7
- data.tar.gz: a40e5e9ae771224e486c8263d2194fc5de1256b6d6a0d9d880ef44aea01294160fbbe1f139b74a375f4adb3e6372e155700968de0d862918ad0662ce3a2fc80c
6
+ metadata.gz: 6275a47f463227b86374e327781edd92bbd9cdf9399bafa68c6c4cb3316ae812cf081509a26e81e04101f9cd4af43396f2c008477741f00f475ea09436353ba8
7
+ data.tar.gz: 1a5a27d9253b78ae0c1c0526c1f8dcb977a428ec5c62cb4239f5542413e1f5020c68d8ee0d356ded4c1f3094c7f449f03fefb72180250388de81f8c44c2c6a63
data/README.md CHANGED
@@ -66,6 +66,10 @@ Planter.configure do |config|
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.
72
+ # config.erb_trim_mode = nil
69
73
  end
70
74
  ```
71
75
 
@@ -135,18 +139,37 @@ class UsersSeeder < Planter::Seeder
135
139
  end
136
140
  ```
137
141
 
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:
142
+ `ERB` can be used in the CSV files if you end the file name with `.csv.erb`.
143
+ For example, `users.csv.erb`.
142
144
 
143
145
  ```
144
- email,login_attempts
145
- <% count = 1 %>
146
- test2@example.com,<%= count += 1 %>
147
- test2@example.com,<%= count += 1 %>
146
+ participant_id,name
147
+ <%= Participant.find_by(email: 'test1@example.com').id %>,"Test User1"
148
+ <%= Participant.find_by(email: 'test2@example.com').id %>,"Test User2"
148
149
  ```
149
150
 
151
+ Note that, if you need to change the trim mode for ERB, you can set a default in
152
+ the initializer.
153
+
154
+ ```ruby
155
+ Planter.configure do |config|
156
+ config.seeders = %i[
157
+ users
158
+ ]
159
+ config.erb_trim_mode = '<>'
160
+ end
161
+ ```
162
+
163
+ ...or, for individual seeders, via `seeding_method`.
164
+
165
+ ```ruby
166
+ class UsersSeeder < Planter::Seeder
167
+ seeding_method :csv, erb_trim_mode: '<>'
168
+ end
169
+ ```
170
+
171
+ For help with `erb_trim_mode`, see the help documentation for `ERB::new`.
172
+
150
173
  Running `rails planter:seed` will now seed your `users` table.
151
174
 
152
175
  ## Seeding from a data array
@@ -25,6 +25,10 @@ module Planter
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.
31
+ # config.erb_trim_mode = nil
28
32
  end
29
33
  EOF
30
34
  end
@@ -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
@@ -120,6 +120,8 @@ module Planter
120
120
  #
121
121
  # @kwarg [Symbol, String] unique_columns
122
122
  #
123
+ # @kwarg [String] erb_trim_mode
124
+ #
123
125
  # @example
124
126
  # require 'planter'
125
127
  # class UsersSeeder < Planter::Seeder
@@ -129,16 +131,18 @@ module Planter
129
131
  # parent_model: 'Person',
130
132
  # association: :users,
131
133
  # csv_name: :awesome_users,
132
- # unique_columns %i[username email]
134
+ # unique_columns %i[username email],
135
+ # erb_trim_mode: '<>'
133
136
  # end
134
137
  def self.seeding_method(
135
138
  method,
136
139
  number_of_records: 1,
137
- model: to_s.delete_suffix('Seeder').singularize,
140
+ model: nil,
138
141
  parent_model: nil,
139
142
  association: nil,
140
143
  csv_name: nil,
141
- unique_columns: nil
144
+ unique_columns: nil,
145
+ erb_trim_mode: nil
142
146
  )
143
147
  if !SEEDING_METHODS.include?(method.intern)
144
148
  raise ArgumentError, "Method must be one of #{SEEDING_METHODS.join(', ')}"
@@ -148,10 +152,11 @@ module Planter
148
152
 
149
153
  @seeding_method = method
150
154
  @number_of_records = number_of_records
151
- @model = model
155
+ @model = model || to_s.delete_suffix('Seeder').singularize
152
156
  @parent_model = parent_model
153
157
  @association = @parent_model && (association || determine_association)
154
158
  @csv_file = determine_csv_filename(csv_name) if @seeding_method == :csv
159
+ @erb_trim_mode = erb_trim_mode || Planter.config.erb_trim_mode
155
160
  @unique_columns =
156
161
  case unique_columns
157
162
  when String, Symbol then [unique_columns.intern]
@@ -260,6 +265,14 @@ module Planter
260
265
  @unique_columns ||= self.class.instance_variable_get('@unique_columns')
261
266
  end
262
267
 
268
+ ##
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')
274
+ end
275
+
263
276
  ##
264
277
  # Creates records from the +data+ attribute.
265
278
  def create_records
@@ -309,7 +322,7 @@ module Planter
309
322
  when :csv
310
323
  contents = ::File.read(csv_file)
311
324
  if csv_file.end_with?('.erb')
312
- contents = ERB.new(contents, trim_mode: '<>').result(binding)
325
+ contents = ERB.new(contents, trim_mode: erb_trim_mode).result(binding)
313
326
  end
314
327
 
315
328
  @data ||= ::CSV.parse(
@@ -21,7 +21,7 @@ module Planter
21
21
  # Patch version.
22
22
  #
23
23
  # @return [Integer]
24
- PATCH = 11
24
+ PATCH = 12
25
25
 
26
26
  ##
27
27
  # Version as +[MAJOR, MINOR, PATCH]+
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.12
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-11-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails