seed-do 3.1.0 → 3.2.0

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: a144ca5bdde78bd698bd63b5b7ec5d4538e290d494d6d9feaf3b72c2fabc94bd
4
- data.tar.gz: 05156a89c384563da57269b0850fc21f89b6d3f87fdb1801d83b5743cc286d22
3
+ metadata.gz: f41a0a75dff7954f90a7ed6cafe60eef73f0c20490ff9e42952b34e2efe896bc
4
+ data.tar.gz: dd04a94f944a7a290d211b16cec2537a3fa34bc2816cee6c1b594d22a91a907c
5
5
  SHA512:
6
- metadata.gz: 22803ec0568fbfe2d89b20cd65f04c331da02673b901724e7fcd48d22c8b764eff73763c14cd197292e9408f7345fe562a4c1ee5b329f3c8e926b7a25413e166
7
- data.tar.gz: '006481304627db4be5b6eb2ebba80c043078b78c1b4e815a9323398a7aa9f7b395ca3971cb0069f942413a957637f8cf839f71c479ebb9feaa8cf6f7b915ba83'
6
+ metadata.gz: ee3af13c18e0354bc04a378db5d0fc6ef344e2af3c617cc5405153200dd37d316be5e31d07234e077719e875c0daa6098878907146c27924ccb2021c9ba36725
7
+ data.tar.gz: 36367e0c2457b3d18781824ef255095545b7c441c37468c28c2448413877656c5180edca15da8f8d7309cc2bda78d4d9e2e0e7066afdfe607ec0ad71dc89fd89
@@ -41,25 +41,25 @@ module SeedDo
41
41
  # Person.seed_once(:id, :id => 1, :name => "Harry") # => Name *not* changed
42
42
  def seed_once(*args, &block)
43
43
  constraints, data = parse_seed_do_args(args, block)
44
- SeedDo::Seeder.new(self, constraints, data, :insert_only => true).seed
44
+ SeedDo::Seeder.new(self, constraints, data, insert_only: true).seed
45
45
  end
46
46
 
47
47
  private
48
48
 
49
- def parse_seed_do_args(args, block)
50
- if block.nil?
51
- if args.last.is_a?(Array)
52
- # Last arg is an array of data, so assume the rest of the args are constraints
53
- data = args.pop
54
- [args, data]
55
- else
56
- # Partition the args, assuming the first hash is the start of the data
57
- args.partition { |arg| !arg.is_a?(Hash) }
58
- end
49
+ def parse_seed_do_args(args, block)
50
+ if block.nil?
51
+ if args.last.is_a?(Array)
52
+ # Last arg is an array of data, so assume the rest of the args are constraints
53
+ data = args.pop
54
+ [args, data]
59
55
  else
60
- # We have a block, so assume the args are all constraints
61
- [args, [SeedDo::BlockHash.new(block).to_hash]]
56
+ # Partition the args, assuming the first hash is the start of the data
57
+ args.partition { |arg| !arg.is_a?(Hash) }
62
58
  end
59
+ else
60
+ # We have a block, so assume the args are all constraints
61
+ [args, [SeedDo::BlockHash.new(block).to_hash]]
63
62
  end
63
+ end
64
64
  end
65
65
  end
@@ -12,7 +12,7 @@ module SeedDo
12
12
 
13
13
  def method_missing(method_name, *args, &block)
14
14
  if method_name.to_s =~ /^(.*)=$/ && args.length == 1 && block.nil?
15
- @hash[$1] = args.first
15
+ @hash[::Regexp.last_match(1)] = args.first
16
16
  else
17
17
  super
18
18
  end
@@ -1,7 +1,7 @@
1
1
  Capistrano::Configuration.instance.load do
2
2
  namespace :db do
3
- desc "Load seed data into database"
4
- task :seed_do, :roles => :db, :only => { :primary => true } do
3
+ desc 'Load seed data into database'
4
+ task :seed_do, roles: :db, only: { primary: true } do
5
5
  run "cd #{release_path} && bundle exec rake RAILS_ENV=#{rails_env} db:seed_do"
6
6
  end
7
7
  end
@@ -1 +1 @@
1
- load File.expand_path('../../tasks/seed_do_capistrano3.rake', __FILE__)
1
+ load File.expand_path('../tasks/seed_do_capistrano3.rake', __dir__)
@@ -1,7 +1,7 @@
1
1
  module SeedDo
2
2
  class Railtie < Rails::Railtie
3
3
  rake_tasks do
4
- load "tasks/seed_do.rake"
4
+ load 'tasks/seed_do.rake'
5
5
  end
6
6
 
7
7
  initializer 'seed_do.set_fixture_paths' do
@@ -29,45 +29,41 @@ module SeedDo
29
29
 
30
30
  private
31
31
 
32
- def run_file(filename)
33
- puts "\n== Seed from #{filename}" unless SeedDo.quiet
32
+ def run_file(filename)
33
+ puts "\n== Seed from #{filename}" unless SeedDo.quiet
34
34
 
35
- ActiveRecord::Base.transaction do
36
- open(filename) do |file|
37
- chunked_ruby = +''
38
- file.each_line do |line|
39
- if line == "# BREAK EVAL\n"
40
- eval(chunked_ruby)
41
- chunked_ruby = +''
42
- else
43
- chunked_ruby << line
44
- end
35
+ ActiveRecord::Base.transaction do
36
+ open(filename) do |file|
37
+ chunked_ruby = +''
38
+ file.each_line do |line|
39
+ if line == "# BREAK EVAL\n"
40
+ eval(chunked_ruby)
41
+ chunked_ruby = +''
42
+ else
43
+ chunked_ruby << line
45
44
  end
46
- eval(chunked_ruby) unless chunked_ruby == ''
47
45
  end
46
+ eval(chunked_ruby) unless chunked_ruby == ''
48
47
  end
49
48
  end
49
+ end
50
50
 
51
- def open(filename)
52
- if filename[-3..-1] == '.gz'
53
- Zlib::GzipReader.open(filename) do |file|
54
- yield file
55
- end
56
- else
57
- File.open(filename) do |file|
58
- yield file
59
- end
60
- end
51
+ def open(filename, &)
52
+ if filename[-3..-1] == '.gz'
53
+ Zlib::GzipReader.open(filename, &)
54
+ else
55
+ File.open(filename, &)
61
56
  end
57
+ end
62
58
 
63
- def filenames
64
- filenames = []
65
- @fixture_paths.each do |path|
66
- filenames += (Dir[File.join(path, '*.rb')] + Dir[File.join(path, '*.rb.gz')]).sort
67
- end
68
- filenames.uniq!
69
- filenames = filenames.find_all { |filename| filename =~ @filter } if @filter
70
- filenames
59
+ def filenames
60
+ filenames = []
61
+ @fixture_paths.each do |path|
62
+ filenames += (Dir[File.join(path, '*.rb')] + Dir[File.join(path, '*.rb.gz')]).sort
71
63
  end
64
+ filenames.uniq!
65
+ filenames = filenames.find_all { |filename| filename =~ @filter } if @filter
66
+ filenames
67
+ end
72
68
  end
73
69
  end
@@ -41,62 +41,61 @@ module SeedDo
41
41
 
42
42
  private
43
43
 
44
- def validate_constraints!
45
- unknown_columns = @constraints.map(&:to_s) - @model_class.column_names
46
- unless unknown_columns.empty?
47
- raise(ArgumentError,
44
+ def validate_constraints!
45
+ unknown_columns = @constraints.map(&:to_s) - @model_class.column_names
46
+ return if unknown_columns.empty?
47
+
48
+ raise(ArgumentError,
48
49
  "Your seed constraints contained unknown columns: #{column_list(unknown_columns)}. " +
49
50
  "Valid columns are: #{column_list(@model_class.column_names)}.")
50
- end
51
- end
51
+ end
52
52
 
53
- def validate_data!
54
- raise ArgumentError, "Seed data missing" if @data.empty?
55
- end
53
+ def validate_data!
54
+ raise ArgumentError, 'Seed data missing' if @data.empty?
55
+ end
56
56
 
57
- def column_list(columns)
58
- '`' + columns.join("`, `") + '`'
59
- end
57
+ def column_list(columns)
58
+ '`' + columns.join('`, `') + '`'
59
+ end
60
60
 
61
- def seed_record(data)
62
- record = find_or_initialize_record(data)
63
- return if @options[:insert_only] && !record.new_record?
61
+ def seed_record(data)
62
+ record = find_or_initialize_record(data)
63
+ return if @options[:insert_only] && !record.new_record?
64
64
 
65
- puts " - #{@model_class} #{data.inspect}" unless @options[:quiet]
65
+ puts " - #{@model_class} #{data.inspect}" unless @options[:quiet]
66
66
 
67
- record.assign_attributes(data)
68
- record.save(:validate => false) || raise(ActiveRecord::RecordNotSaved, 'Record not saved!')
69
- record
70
- end
67
+ record.assign_attributes(data)
68
+ record.save(validate: false) || raise(ActiveRecord::RecordNotSaved, 'Record not saved!')
69
+ record
70
+ end
71
71
 
72
- def find_or_initialize_record(data)
73
- @model_class.where(constraint_conditions(data)).take ||
72
+ def find_or_initialize_record(data)
73
+ @model_class.where(constraint_conditions(data)).take ||
74
74
  @model_class.new
75
- end
75
+ end
76
76
 
77
- def constraint_conditions(data)
78
- Hash[@constraints.map { |c| [c, data[c.to_sym]] }]
79
- end
77
+ def constraint_conditions(data)
78
+ @constraints.to_h { |c| [c, data[c.to_sym]] }
79
+ end
80
80
 
81
- def update_id_sequence
82
- if @model_class.connection.adapter_name == "PostgreSQL" or @model_class.connection.adapter_name == "PostGIS"
83
- return if @model_class.primary_key.nil? || @model_class.sequence_name.nil?
81
+ def update_id_sequence
82
+ return unless %w[PostgreSQL PostGIS].include?(@model_class.connection.adapter_name)
83
+ return if @model_class.primary_key.nil? || @model_class.sequence_name.nil?
84
84
 
85
- quoted_id = @model_class.connection.quote_column_name(@model_class.primary_key)
86
- sequence = @model_class.sequence_name
85
+ quoted_id = @model_class.connection.quote_column_name(@model_class.primary_key)
86
+ sequence = @model_class.sequence_name
87
87
 
88
- if @model_class.connection.postgresql_version >= 100000
89
- sql =<<-EOS
88
+ if @model_class.connection.postgresql_version >= 100_000
89
+ sql = <<-EOS
90
90
  SELECT setval('#{sequence}', (SELECT GREATEST(MAX(#{quoted_id})+(SELECT seqincrement FROM pg_sequence WHERE seqrelid = '#{sequence}'::regclass), (SELECT seqmin FROM pg_sequence WHERE seqrelid = '#{sequence}'::regclass)) FROM #{@model_class.quoted_table_name}), false)
91
- EOS
92
- else
93
- sql =<<-EOS
91
+ EOS
92
+ else
93
+ sql = <<-EOS
94
94
  SELECT setval('#{sequence}', (SELECT GREATEST(MAX(#{quoted_id})+(SELECT increment_by FROM #{sequence}), (SELECT min_value FROM #{sequence})) FROM #{@model_class.quoted_table_name}), false)
95
- EOS
96
- end
97
-
98
- @model_class.connection.execute sql
99
- end
95
+ EOS
100
96
  end
97
+
98
+ @model_class.connection.execute sql
99
+ end
101
100
  end
102
101
  end
@@ -1,3 +1,3 @@
1
1
  module SeedDo
2
- VERSION = '3.1.0'
2
+ VERSION = '3.2.0'
3
3
  end
@@ -18,9 +18,9 @@ module SeedDo
18
18
  class Writer
19
19
  cattr_accessor :default_options
20
20
  @@default_options = {
21
- :chunk_size => 100,
22
- :constraints => [:id],
23
- :seed_type => :seed
21
+ chunk_size: 100,
22
+ constraints: [:id],
23
+ seed_type: :seed
24
24
  }
25
25
 
26
26
  # @param [Hash] options
@@ -33,13 +33,13 @@ module SeedDo
33
33
  # @option options [Array<Symbol>] :constraints ([:id]) The constraining attributes for the seeds
34
34
  def initialize(options = {})
35
35
  @options = self.class.default_options.merge(options)
36
- raise ArgumentError, "missing option :class_name" unless @options[:class_name]
36
+ raise ArgumentError, 'missing option :class_name' unless @options[:class_name]
37
37
  end
38
38
 
39
39
  # Creates a new instance of {Writer} with the `options`, and then calls {#write} with the
40
40
  # `io_or_filename` and `block`
41
- def self.write(io_or_filename, options = {}, &block)
42
- new(options).write(io_or_filename, &block)
41
+ def self.write(io_or_filename, options = {}, &)
42
+ new(options).write(io_or_filename, &)
43
43
  end
44
44
 
45
45
  # Writes the necessary headers and footers, and yields to a block within which the actual
@@ -51,7 +51,7 @@ module SeedDo
51
51
  # closed automatically.)
52
52
  # @yield [self] make calls to `#<<` within the block
53
53
  def write(io_or_filename, &block)
54
- raise ArgumentError, "missing block" unless block_given?
54
+ raise ArgumentError, 'missing block' unless block_given?
55
55
 
56
56
  if io_or_filename.respond_to?(:write)
57
57
  write_to_io(io_or_filename, &block)
@@ -65,7 +65,7 @@ module SeedDo
65
65
  # Add a seed. Must be called within a block passed to {#write}.
66
66
  # @param [Hash] seed The attributes for the seed
67
67
  def <<(seed)
68
- raise "You must add seeds inside a SeedDo::Writer#write block" unless @io
68
+ raise 'You must add seeds inside a SeedDo::Writer#write block' unless @io
69
69
 
70
70
  buffer = +''
71
71
 
@@ -76,57 +76,59 @@ module SeedDo
76
76
  end
77
77
 
78
78
  buffer << ",\n"
79
- buffer << ' ' + seed.inspect
79
+ buffer << (' ' + seed.inspect)
80
80
 
81
81
  @io.write(buffer)
82
82
 
83
83
  @count += 1
84
84
  end
85
- alias_method :add, :<<
85
+ alias add <<
86
86
 
87
87
  private
88
88
 
89
- def write_to_io(io)
90
- @io, @count = io, 0
91
- @io.write(file_header)
92
- @io.write(seed_header)
93
- yield(self)
94
- @io.write(seed_footer)
95
- @io.write(file_footer)
96
- ensure
97
- @io, @count = nil, nil
98
- end
89
+ def write_to_io(io)
90
+ @io = io
91
+ @count = 0
92
+ @io.write(file_header)
93
+ @io.write(seed_header)
94
+ yield(self)
95
+ @io.write(seed_footer)
96
+ @io.write(file_footer)
97
+ ensure
98
+ @io = nil
99
+ @count = nil
100
+ end
99
101
 
100
- def file_header
101
- <<-END
102
- # DO NOT MODIFY THIS FILE, it was auto-generated.
103
- #
104
- # Date: #{Time.now}
105
- # Seeding #{@options[:class_name]}
106
- # Written with the command:
107
- #
108
- # #{$0} #{$*.join}
109
- #
110
- END
111
- end
102
+ def file_header
103
+ <<~END
104
+ # DO NOT MODIFY THIS FILE, it was auto-generated.
105
+ #
106
+ # Date: #{Time.now}
107
+ # Seeding #{@options[:class_name]}
108
+ # Written with the command:
109
+ #
110
+ # #{$0} #{$*.join}
111
+ #
112
+ END
113
+ end
112
114
 
113
- def file_footer
114
- <<-END
115
- # End auto-generated file.
116
- END
117
- end
115
+ def file_footer
116
+ <<~END
117
+ # End auto-generated file.
118
+ END
119
+ end
118
120
 
119
- def seed_header
120
- constraints = @options[:constraints] && @options[:constraints].map(&:inspect).join(', ')
121
- "#{@options[:class_name]}.#{@options[:seed_type]}(#{constraints}"
122
- end
121
+ def seed_header
122
+ constraints = @options[:constraints] && @options[:constraints].map(&:inspect).join(', ')
123
+ "#{@options[:class_name]}.#{@options[:seed_type]}(#{constraints}"
124
+ end
123
125
 
124
- def seed_footer
125
- "\n)\n"
126
- end
126
+ def seed_footer
127
+ "\n)\n"
128
+ end
127
129
 
128
- def chunk_this_seed?
129
- @count != 0 && (@count % @options[:chunk_size]) == 0
130
- end
130
+ def chunk_this_seed?
131
+ @count != 0 && (@count % @options[:chunk_size]) == 0
132
+ end
131
133
  end
132
134
  end
data/lib/seed-do.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'active_record'
2
2
  require 'active_support/core_ext/module/attribute_accessors'
3
- require 'seed-do/railtie' if defined?(Rails) && Rails.version >= "3"
3
+ require 'seed-do/railtie' if defined?(Rails)
4
4
 
5
5
  module SeedDo
6
6
  autoload :VERSION, 'seed-do/version'
@@ -10,17 +10,13 @@ module SeedDo
10
10
  autoload :Runner, 'seed-do/runner'
11
11
  autoload :Writer, 'seed-do/writer'
12
12
 
13
- mattr_accessor :quiet
14
-
15
13
  # Set `SeedDo.quiet = true` to silence all output
16
- @@quiet = false
17
-
18
- mattr_accessor :fixture_paths
14
+ mattr_accessor :quiet, default: false
19
15
 
20
16
  # Set this to be an array of paths to directories containing your seed files. If used as a Rails
21
- # plugin, SeedDo will set to to contain `Rails.root/db/fixtures` and
17
+ # plugin, SeedDo will set it to contain `Rails.root/db/fixtures` and
22
18
  # `Rails.root/db/fixtures/Rails.env`
23
- @@fixture_paths = ['db/fixtures']
19
+ mattr_accessor :fixture_paths, default: ['db/fixtures']
24
20
 
25
21
  # Load seed data from files
26
22
  # @param [Array] fixture_paths The paths to look for seed files in
@@ -24,14 +24,10 @@ namespace :db do
24
24
  # to load files from RAILS_ROOT/features/fixtures
25
25
  rake db:seed_do FIXTURE_PATH=features/fixtures
26
26
  EOS
27
- task :seed_do => :environment do
28
- if ENV["FILTER"]
29
- filter = /#{ENV["FILTER"].gsub(/,/, "|")}/
30
- end
31
-
32
- if ENV["FIXTURE_PATH"]
33
- fixture_paths = [ENV["FIXTURE_PATH"], ENV["FIXTURE_PATH"] + '/' + Rails.env]
34
- end
27
+ task seed_do: :environment do
28
+ filter = /#{ENV['FILTER'].tr(',', '|')}/ if ENV['FILTER']
29
+
30
+ fixture_paths = [ENV['FIXTURE_PATH'], ENV['FIXTURE_PATH'] + '/' + Rails.env] if ENV['FIXTURE_PATH']
35
31
 
36
32
  SeedDo.seed(fixture_paths, filter)
37
33
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seed-do
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shinichi Maeshima
@@ -78,7 +78,8 @@ files:
78
78
  homepage: http://github.com/willnet/seed-do
79
79
  licenses:
80
80
  - MIT
81
- metadata: {}
81
+ metadata:
82
+ rubygems_mfa_required: 'true'
82
83
  rdoc_options: []
83
84
  require_paths:
84
85
  - lib