planter 0.0.6 → 0.0.7

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: 95ab85b49c50ea3b7ee077d4225083884558ae89c6fa5b2ea4e9410284f890eb
4
- data.tar.gz: 2fd36b19ab5064d681323eb5f7e73a95cf710b7af8a587031c84b7175610cfbe
3
+ metadata.gz: 42b61a78b5034eadac54f7e5a570073851b4a9a6fc20c4fae71a1e49fb8d3df1
4
+ data.tar.gz: c02538d3a776a89b221b135c4aa2d355a278df35e514d53872f200556bc720dd
5
5
  SHA512:
6
- metadata.gz: fa74216b4529bcb3d21ccd697574c175ad7dd641d477921108ff9949e1ab9a9537cf6f4847b6e2aaadeb91e8aecedc6afec243c72250d9b77cce1da0e4926580
7
- data.tar.gz: 9b9cb5dd5a29b8a66c8cd9a6c49d5192c1395a5d935a98628928f65babcd709c2a6480ed356bf2f575e0710856a214025efdf91942acf3fe034a39a1369f302a
6
+ metadata.gz: 119f4f0a790efd00eb4e537734ab51e9416b5017f0891a07698d291b44604b2a81e7ea73d1a8893da1e3f4c24b4790a4a18a352388240d9ed9de32a1f19206b5
7
+ data.tar.gz: 3c066efbe8b1325bec3218de0840bf91688c6ed1da29e06446756603f370620d5a57af4afc57313fa42171ba977f1dab9cbe73a9578fcb3379e2433935c609b3
data/README.md CHANGED
@@ -72,7 +72,7 @@ To seed from CSV, you simply need to add the following to your seeder class.
72
72
 
73
73
  ```ruby
74
74
  class UsersSeeder < Planter::Seeder
75
- seeding_method :standard_csv
75
+ seeding_method :csv
76
76
  end
77
77
  ```
78
78
 
@@ -86,15 +86,27 @@ test1@example.com,test1
86
86
  test2@example.com,test2
87
87
  ```
88
88
 
89
- If the CSV files are not located in the project, you can specify a `:csv_file`
90
- option. Note that the value must be a full path.
89
+ If the CSV file is named differently than the seeder, you can specify the
90
+ `:csv_name` option. Note that the value should not include the file extension.
91
91
 
92
92
  ```ruby
93
93
  class UsersSeeder < Planter::Seeder
94
- seeding_method :standard_csv, csv_file: '/home/me/users.csv'
94
+ seeding_method :csv, csv_name: :people
95
95
  end
96
96
  ```
97
97
 
98
+ `ERB` can be used in the CSV files if you name it with `.erb` at the end of the
99
+ file name. For example, `users.csv.erb`. Note that lines starting with `<%` and
100
+ ending with `%>` will not be considered rows, so you can use `ERB` rows to set
101
+ values. For example:
102
+
103
+ ```csv.erb
104
+ email,login_attempts
105
+ <% count = 1 %>
106
+ test2@example.com,<%= count += 1 %>
107
+ test2@example.com,<%= count += 1 %>
108
+ ```
109
+
98
110
  Running `rails db:seed` will now seed your `users` table.
99
111
 
100
112
  ## Seeding from a data array
@@ -148,7 +160,7 @@ end
148
160
 
149
161
  Note that specifying `number_of_records` in this instance will create that many
150
162
  records *for each record of the parent model*. You can also specify the
151
- association if it's different from the table name, using the `assocation:`
163
+ association if it's different from the table name, using the `:assocation`
152
164
  option.
153
165
 
154
166
  ### Custom seeds
@@ -169,7 +181,7 @@ end
169
181
  ```
170
182
 
171
183
  ## Customization
172
- You can change the directories of both the seeder files and the csv files. In
184
+ You can change the directories of both the seeder files and the CSV files. In
173
185
  your `configure` block in `db/seeds.rb`, you can add the following. Note that,
174
186
  in both instances, the path should be relative to `Rails.root`.
175
187
 
data/lib/planter.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'csv'
4
+ require 'erb'
4
5
  require 'planter/version'
5
6
  require 'planter/railtie'
6
7
  require 'planter/config'
@@ -16,13 +17,13 @@ require 'planter/seeder'
16
17
  # The most basic way to seed is to have a CSV file with the same name as the
17
18
  # table in +db/seed_files/+. So, +users.csv+. This CSV should have the table's
18
19
  # column names as header. To seed using this method, your class should look
19
- # like the following. Note that +:csv_file+ is not required; it defaults to the
20
+ # like the following. Note that +:csv_name+ is not required; it defaults to the
20
21
  # table name with a +csv+ file extension. The directory where the seed files
21
22
  # are kept can be changed via an initializer.
22
23
  # # db/seeds/users_seeder.rb
23
24
  # require 'planter'
24
25
  # class UsersSeeder < Planter::Seeder
25
- # seeding_method :standard_csv, csv_file: '/home/me/users.csv'
26
+ # seeding_method :csv, csv_name: '/home/me/users.csv'
26
27
  # end
27
28
  #
28
29
  # Another way to seed is to create records from a data array. To do this, your
@@ -8,14 +8,14 @@ module Planter
8
8
  # The allowed seeding methods.
9
9
  #
10
10
  # @return [Array]
11
- SEEDING_METHODS = %i[standard_csv data_array].freeze
11
+ SEEDING_METHODS = %i[csv data_array].freeze
12
12
 
13
13
  ##
14
14
  # Array of hashes used to create records. Your class must set this
15
15
  # attribute when using +data_hash+ seeding method, although it's probably
16
16
  # more likely that you'll want to define a method that returns a new set of
17
17
  # data each time (via +Faker+, +Array#sample+, etc.). When using
18
- # +standard_csv+, +data+ will be set to the data within the csv. You can
18
+ # +csv+, +data+ will be set to the data within the csv. You can
19
19
  # override this.
20
20
  #
21
21
  # @return [Array]
@@ -28,40 +28,49 @@ module Planter
28
28
  #
29
29
  # @param [Symbol] seeding_method
30
30
  #
31
- # @param [Hash] options
31
+ # @kwarg [Integer] number_of_records
32
+ #
33
+ # @kwarg [String] model
34
+ #
35
+ # @kwarg [String] parent_model
36
+ #
37
+ # @kwarg [Symbol, String] association
38
+ #
39
+ # @kwarg [Symbol, String] csv_name
32
40
  #
33
41
  # @example
34
42
  # require 'planter'
35
43
  # class UsersSeeder < Planter::Seeder
36
- # seeding_method :data_array,
44
+ # seeding_method :csv,
45
+ # number_of_records: 2,
37
46
  # model: 'User'
38
47
  # parent_model: 'Person',
39
48
  # association: :users,
40
- # number_of_records: 2
49
+ # csv_name: :awesome_users
41
50
  # end
42
- def self.seeding_method(method, **options)
51
+ def self.seeding_method(
52
+ method,
53
+ number_of_records: 1,
54
+ model: to_s.delete_suffix('Seeder').singularize,
55
+ parent_model: nil,
56
+ association: nil,
57
+ csv_name: nil
58
+ )
43
59
  if !SEEDING_METHODS.include?(method.intern)
44
60
  raise ArgumentError, "Method must be one of #{SEEDING_METHODS.join(', ')}"
45
- elsif options[:association] && !options[:parent_model]
61
+ elsif association && !parent_model
46
62
  raise ArgumentError, "Must specify :parent_model with :association"
47
63
  end
48
64
 
49
65
  @seeding_method = method
50
- @number_of_records = options.fetch(:number_of_records, 1)
51
- @model = options.fetch(:model, to_s.delete_suffix('Seeder').singularize)
52
- @parent_model = options[:parent_model]
53
- @association = @parent_model && options.fetch(:association) do
54
- determine_association(options)
55
- end
56
- return unless @seeding_method == :standard_csv
57
-
58
- @csv_file = options.fetch(:csv_file, Rails.root.join(
59
- Planter.config.csv_files_directory,
60
- "#{to_s.delete_suffix('Seeder').underscore}.csv"
61
- ).to_s)
66
+ @number_of_records = number_of_records
67
+ @model = model
68
+ @parent_model = parent_model
69
+ @association = @parent_model && (association || determine_association)
70
+ @csv_file = determine_csv_filename(csv_name) if @seeding_method == :csv
62
71
  end
63
72
 
64
- def self.determine_association(options) # :nodoc:
73
+ def self.determine_association # :nodoc:
65
74
  associations =
66
75
  @parent_model.constantize.reflect_on_all_associations.map(&:name)
67
76
  table = to_s.delete_suffix('Seeder').underscore.split('/').last
@@ -70,10 +79,23 @@ module Planter
70
79
  return t if associations.include?(t)
71
80
  end
72
81
 
73
- raise ArgumentError, 'Could not determine association name'
82
+ raise ArgumentError, "Couldn't determine association name"
74
83
  end
75
84
  private_class_method :determine_association
76
85
 
86
+ def self.determine_csv_filename(csv_name) # :nodoc:
87
+ file = (
88
+ csv_name || "#{to_s.delete_suffix('Seeder').underscore}"
89
+ ).to_s + '.csv'
90
+ [file, "#{file}.erb"].each do |f|
91
+ fname = Rails.root.join(Planter.config.csv_files_directory, f).to_s
92
+ return fname if File.file?(fname)
93
+ end
94
+
95
+ raise ArgumentError, "Couldn't find csv for #{@model}"
96
+ end
97
+ private_class_method :determine_csv_filename
98
+
77
99
  ##
78
100
  # The default seed method. To use this method, your class must provide a
79
101
  # valid +seeding_method+, and not implement its own +seed+ method.
@@ -181,10 +203,13 @@ module Planter
181
203
 
182
204
  def validate_attributes # :nodoc:
183
205
  case seeding_method.intern
184
- when :standard_csv
185
- raise "#{csv_file} does not exist" unless ::File.file?(csv_file)
206
+ when :csv
207
+ contents = File.read(csv_file)
208
+ if csv_file.end_with?('.erb')
209
+ contents = ERB.new(contents, trim_mode: '<>').result(binding)
210
+ end
186
211
 
187
- @data ||= ::CSV.table(csv_file).map(&:to_hash)
212
+ @data ||= ::CSV.parse(contents, headers: true).map(&:to_hash)
188
213
  when :data_array
189
214
  raise "Must define '@data'" if public_send(:data).nil?
190
215
  else
@@ -21,7 +21,7 @@ module Planter
21
21
  # Patch version.
22
22
  #
23
23
  # @return [Integer]
24
- PATCH = 6
24
+ PATCH = 7
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.6
4
+ version: 0.0.7
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-05-23 00:00:00.000000000 Z
11
+ date: 2021-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails