planter 0.0.11 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +31 -8
- data/lib/generators/planter/initializer_generator.rb +4 -0
- data/lib/planter/config.rb +9 -0
- data/lib/planter/seeder.rb +18 -5
- 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: 37c19399e25198fe0545690d5b5568c5bd91b4736258af3d8664c36abe882889
|
4
|
+
data.tar.gz: a399b54a606d7c0e98d49f0bad5abff1fc4726d96eab3573c82d1dc649b83b1e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
139
|
-
|
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
|
-
|
145
|
-
|
146
|
-
test2@example.com
|
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
|
data/lib/planter/config.rb
CHANGED
@@ -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
|
data/lib/planter/seeder.rb
CHANGED
@@ -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:
|
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:
|
325
|
+
contents = ERB.new(contents, trim_mode: erb_trim_mode).result(binding)
|
313
326
|
end
|
314
327
|
|
315
328
|
@data ||= ::CSV.parse(
|
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.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-
|
11
|
+
date: 2021-11-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|