planter 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 39fe6b05679379fdcf8c619b1db28fdac6a6f89eec521b3e8cf3f3667a471c49
4
- data.tar.gz: 42693cdccc9239a069996c74455edc658b339a894eff5b984ec00bba47a88797
3
+ metadata.gz: a542b8919dda44137996b078e0ac3f773fc8b85bed2225193de3f2a36bdfe099
4
+ data.tar.gz: '058271874209001fd8de03630c2e3f162959621769225bb354060f1ed8848354'
5
5
  SHA512:
6
- metadata.gz: c3ffd760187e67c1f57b7daf7a1bba7f019470f1324ddaf72d1412c34930ea520eea449aebd0cea43b6b48de948f93ba80f47bafe6de782f550c35daf0bb8508
7
- data.tar.gz: cfd8daff8e7d45a9d72411c195b31be7d1ddde9b562a03397b43f0848360c701d569289ab32e3260bf03f765752cd81fa123f3b597d9b08cc69ac9a6cab7bb6f
6
+ metadata.gz: c9728b99a23877a7f96f4267b963dd7f4e15f4c385303759476937f7f3f33bd6335ec0e6b89fde733ecfd7f5380386ba86ca26bfbdb9ac70f587b8a05efc8812
7
+ data.tar.gz: 1af8f9af55ca77f91f2ef7c01b8cb2c1919166be12f8785d779137d68237050b1ab259726da9546dea7dd2184f85431930e9ec8785d269c16ee1ba6add884b92
data/README.md CHANGED
@@ -21,10 +21,10 @@ You can view the documentation [here](https://evanthegrayt.github.io/planter/).
21
21
  ## Installation
22
22
  Add the following line to your application's Gemfile. Because this plugin is
23
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.
24
+ version, as breaking changes may occur, even at the minor level.
25
25
 
26
26
  ```ruby
27
- gem 'planter', '0.0.10'
27
+ gem 'planter', '0.1.3'
28
28
  ```
29
29
 
30
30
  And then execute:
@@ -83,6 +83,7 @@ allows you to use `db:seed` for other purposes. If you want Planter to hook
83
83
  into the existing `db:seed` task, simply add the following to `db/seeds.rb`.
84
84
 
85
85
  ```ruby
86
+ # db/seeds.rb
86
87
  Planter.seed
87
88
  ```
88
89
 
@@ -175,6 +176,38 @@ end
175
176
 
176
177
  For help with `erb_trim_mode`, see the help documentation for `ERB::new`.
177
178
 
179
+ Lastly, it's worth mentioning `transformations` under the CSV section, as that's
180
+ usually the pace where they're needed most, but it will work with any method.
181
+
182
+ If you're seeding with a CSV, and it contains values that need to have code
183
+ executed on them before it's imported into the database, you can define an
184
+ instance variable called `@transformations`, or a method called
185
+ `transformations`, that returns a Hash of field names, and Procs to run on the
186
+ value. For example, if you have an `admin` column, and the CSV contains "true",
187
+ it will come through as a String, but you probably want it to be a Boolean. This
188
+ can be solved with the following.
189
+
190
+ ```ruby
191
+ class UsersSeeder < Planter::Seeder
192
+ seeding_method :csv
193
+
194
+ def transformations
195
+ {
196
+ admin: ->(value) { value == 'true' },
197
+ last_name: ->(value, row) { "#{value} #{row[:suffix]}".squish }
198
+ }
199
+ end
200
+ end
201
+ ```
202
+
203
+ When defining a Proc/Lambda, you can make it accept 0, 1, or 2 arguments.
204
+ - When `0`, the value is replaced by the result of the Lambda
205
+ - When `1`, the value is passed to the Lambda, and is subsequently replaced by
206
+ the result of the Lambda
207
+ - When `2`, the value is the first argument, and the entire row, as a Hash, is
208
+ the second argument. This allows for more complicated transformations that can
209
+ be dependent on other fields and values in the record.
210
+
178
211
  Running `rails planter:seed` will now seed your `users` table.
179
212
 
180
213
  ## Seeding from a data array
@@ -102,6 +102,35 @@ module Planter
102
102
  # @return [Array]
103
103
  attr_reader :data
104
104
 
105
+ ##
106
+ # A hash of user-defined column names and procs to be run on values. This
107
+ # is most useful for when seeding from csv, and you need to transform, say,
108
+ # 'true' (String) into true (Boolean). The user may define this as an
109
+ # instance variable, or define a method that returns the hash.
110
+ #
111
+ # When defining a Proc/Lambda, you can make it accept 0, 1, or 2 arguments.
112
+ # - When 0, the value is replaced by the result of the Lambda.
113
+ # - When 1, the value is passed to the Lambda, and is subsequently
114
+ # replaced by the result of the Lambda.
115
+ # - When 2, the value is the first argument, and the entire row, as a
116
+ # Hash, is the second argument. This allows for more complicated
117
+ # transformations that can be dependent on other fields and values in the
118
+ # record.
119
+ #
120
+ # @return [Hash, nil]
121
+ #
122
+ # @example
123
+ # class UsersSeeder < Planter::Seeder
124
+ # seeding_method :csv
125
+ # def transformations
126
+ # {
127
+ # admin: ->(v) { v == 'true' },
128
+ # last_name: ->(value, row) { "#{value} #{row[:suffix]}".squish }
129
+ # }
130
+ # end
131
+ # end
132
+ attr_reader :transformations
133
+
105
134
  ##
106
135
  # What trim mode should ERB use?
107
136
  #
@@ -189,7 +218,7 @@ module Planter
189
218
  unique_columns: nil,
190
219
  erb_trim_mode: nil
191
220
  )
192
- if !SEEDING_METHODS.include?(seed_method.intern)
221
+ unless SEEDING_METHODS.include?(seed_method.intern)
193
222
  raise ArgumentError, "Method must be: #{SEEDING_METHODS.join(', ')}"
194
223
  end
195
224
 
@@ -216,7 +245,7 @@ module Planter
216
245
  parent ? create_records_from_parent : create_records
217
246
  end
218
247
 
219
- protected
248
+ private
220
249
 
221
250
  ##
222
251
  # Creates records from the +data+ attribute.
@@ -234,7 +263,7 @@ module Planter
234
263
 
235
264
  def create_record(record, parent_id: nil)
236
265
  number_of_records.times do
237
- unique, attrs = split_record(record)
266
+ unique, attrs = split_record(apply_transformations(record))
238
267
  model.constantize.where(
239
268
  unique.tap { |u| u[foreign_key] = parent_id if parent_id }
240
269
  ).first_or_create!(attrs)
@@ -252,6 +281,27 @@ module Planter
252
281
  end
253
282
  end
254
283
 
284
+ def apply_transformations(record)
285
+ return record if public_send(:transformations).nil?
286
+
287
+ Hash[record.map { |field, value| map_record(field, value, record) }]
288
+ end
289
+
290
+ def map_record(field, value, record)
291
+ [
292
+ field,
293
+ transformations.key?(field) ? transform(field, value, record) : value
294
+ ]
295
+ end
296
+
297
+ def transform(field, value, record)
298
+ case transformations[field].arity
299
+ when 0 then transformations[field].call
300
+ when 1 then transformations[field].call(value)
301
+ when 2 then transformations[field].call(value, record)
302
+ end
303
+ end
304
+
255
305
  def split_record(rec) # :nodoc:
256
306
  return [rec, {}] unless unique_columns
257
307
 
@@ -21,7 +21,7 @@ module Planter
21
21
  # Patch version.
22
22
  #
23
23
  # @return [Integer]
24
- PATCH = 2
24
+ PATCH = 3
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.1.2
4
+ version: 0.1.3
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-12-29 00:00:00.000000000 Z
11
+ date: 2022-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails