seed_reaper 0.0.0 → 0.0.2
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 +4 -4
- data/lib/Rakefile +6 -0
- data/lib/seed_reaper/config_evaluator.rb +35 -0
- data/lib/seed_reaper/railtie.rb +14 -0
- data/lib/seed_reaper/seed_writer.rb +27 -0
- data/lib/seed_reaper/seedifier.rb +105 -0
- data/lib/seed_reaper/tasks/seed_reaper.rake +15 -0
- data/lib/seed_reaper.rb +4 -23
- metadata +8 -4
- data/lib/config_evaluator.rb +0 -33
- data/lib/seedifier.rb +0 -103
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0347bce9f9ed607a63163bd7e25b290b3a961e9d18b6f9e95a07c917a216a892
|
4
|
+
data.tar.gz: 539a54c298af39892b4944d2dd0b18db062029170cf914958f89d5db2a1102ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7c8b1f2f7a6b2c21de8ab5c87f329a20d4a21cf622c9816d68cbf69b2d218a63cac468f3176986ffe6ac8b6ecdcd1c9329b1ab61448bb6714407cb9cb516c056
|
7
|
+
data.tar.gz: 2dbfe636a01fc0d62bbd54edee05a4daf50db7779687ccb2b288b015d4d26bb55796ac0b94c7de88b850abf1068371c89d5af254198d8baca91efb1051946cbd
|
data/lib/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SeedReaper
|
4
|
+
class ConfigEvaluator
|
5
|
+
def initialize(config)
|
6
|
+
@config = config
|
7
|
+
end
|
8
|
+
|
9
|
+
def schema
|
10
|
+
return nil unless @config
|
11
|
+
return @config.reject { |k| k == :meta } if @config.is_a?(Hash)
|
12
|
+
return @config.reject { |c| c.is_a?(Hash) && c.has_key?(:meta) } if @config.is_a?(Array)
|
13
|
+
|
14
|
+
@config
|
15
|
+
end
|
16
|
+
|
17
|
+
%i[count joins].each do |meta_field|
|
18
|
+
define_method meta_field do
|
19
|
+
meta(meta_field)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def meta(field)
|
26
|
+
return nil unless @config
|
27
|
+
return @config.dig(:meta, field) if @config.is_a?(Hash)
|
28
|
+
return nil unless @config.is_a?(Array)
|
29
|
+
|
30
|
+
@config.select do |c|
|
31
|
+
c.is_a?(Hash) && c.has_key?(:meta)
|
32
|
+
end.first&.dig(:meta, field)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'seed_reaper'
|
4
|
+
|
5
|
+
module SeedReaper
|
6
|
+
class Railtie < Rails::Railtie
|
7
|
+
railtie_name :seed_reaper
|
8
|
+
|
9
|
+
rake_tasks do
|
10
|
+
path = File.expand_path(__dir__)
|
11
|
+
Dir.glob("#{path}/tasks/**/*.rake").each { |f| load f }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SeedReaper
|
4
|
+
class SeedWriter
|
5
|
+
def initialize(config)
|
6
|
+
@config = config
|
7
|
+
end
|
8
|
+
|
9
|
+
def write!
|
10
|
+
FileUtils.rm_rf('db/seeds/.', secure: true)
|
11
|
+
|
12
|
+
@config.each_with_index do |element, i|
|
13
|
+
File.open("db/seeds/#{i.to_s.rjust(@config.count.digits.count, '0')}_#{file_name(element)}.seeds.rb", 'w') do |file|
|
14
|
+
file.write(Seedifier.new(element).seedify)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def file_name(element)
|
22
|
+
return element if element.is_a?(Symbol)
|
23
|
+
|
24
|
+
element.first[0]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/inflector'
|
4
|
+
require_relative 'config_evaluator'
|
5
|
+
|
6
|
+
module SeedReaper
|
7
|
+
class Seedifier
|
8
|
+
def initialize(config)
|
9
|
+
@config = config
|
10
|
+
end
|
11
|
+
|
12
|
+
def seedify
|
13
|
+
seedify_collection(base_model, base_config)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def base_model
|
19
|
+
return @config.to_s.camelize.constantize if @config.is_a?(Symbol)
|
20
|
+
|
21
|
+
@config.first[0].to_s.camelize.constantize
|
22
|
+
end
|
23
|
+
|
24
|
+
def base_config
|
25
|
+
return nil if @config.is_a?(Symbol)
|
26
|
+
|
27
|
+
@config.first[1]
|
28
|
+
end
|
29
|
+
|
30
|
+
def seedify_collection(collection, config)
|
31
|
+
ce = ConfigEvaluator.new(config)
|
32
|
+
evaluated_collection(collection, ce).reduce('') do |str, instance|
|
33
|
+
str += serialize(instance) + seedify_associations(instance, ce.schema)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def seedify_associations(instance, config)
|
38
|
+
return '' if config.nil?
|
39
|
+
|
40
|
+
if config.is_a?(Symbol)
|
41
|
+
instance.send(config).tap do |association|
|
42
|
+
return '' if association.blank?
|
43
|
+
|
44
|
+
association = [association] unless association.respond_to?(:each)
|
45
|
+
return seedify_collection(association, nil)
|
46
|
+
end
|
47
|
+
elsif config.is_a?(Array)
|
48
|
+
config.map do |sub_config|
|
49
|
+
seedify_associations(instance, sub_config)
|
50
|
+
end.join
|
51
|
+
elsif config.is_a?(Hash)
|
52
|
+
config.map do |(association_name, sub_config)|
|
53
|
+
association = instance.send(association_name)
|
54
|
+
next '' if association.blank?
|
55
|
+
|
56
|
+
association = [association] unless association.respond_to?(:each)
|
57
|
+
seedify_collection(association, sub_config)
|
58
|
+
end.join
|
59
|
+
else
|
60
|
+
fail "Your input config contains a #{config.class_name}. You may only use symbols, arrays and hashes."
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def evaluated_collection(collection, config_evaluator)
|
65
|
+
limited_collection(
|
66
|
+
joined_collection(collection, config_evaluator.joins),
|
67
|
+
config_evaluator.count
|
68
|
+
)
|
69
|
+
end
|
70
|
+
|
71
|
+
def limited_collection(collection, count)
|
72
|
+
return collection unless collection.respond_to?(:limit)
|
73
|
+
return collection.limit(count) if count
|
74
|
+
|
75
|
+
collection.all
|
76
|
+
end
|
77
|
+
|
78
|
+
def joined_collection(collection, joins)
|
79
|
+
return collection unless collection.respond_to?(:joins)
|
80
|
+
return collection.joins(joins) if joins
|
81
|
+
|
82
|
+
collection
|
83
|
+
end
|
84
|
+
|
85
|
+
def serialize(instance)
|
86
|
+
"#{instance.class}.new(\n#{serialize_attrs(instance)}\n).save!(validate: false)\n\n"
|
87
|
+
end
|
88
|
+
|
89
|
+
def serialize_attrs(instance)
|
90
|
+
instance.attributes.to_h.reduce('') do |attr_str, (k, v)|
|
91
|
+
attr_str += "\s\s#{k}: #{serialize_value(v)},\n"
|
92
|
+
end[0...-2]
|
93
|
+
end
|
94
|
+
|
95
|
+
def serialize_value(value)
|
96
|
+
if value.nil?
|
97
|
+
"nil"
|
98
|
+
elsif value.is_a?(Integer)
|
99
|
+
value
|
100
|
+
else
|
101
|
+
"\"#{value}\""
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'yaml'
|
4
|
+
require 'seed_reaper'
|
5
|
+
|
6
|
+
namespace :seed_reaper do
|
7
|
+
desc 'Write seeds to db/seeds/ based on config/seed_reaper.yml.'
|
8
|
+
task :write do
|
9
|
+
SeedReaper::SeedWriter.new(
|
10
|
+
YAML.load(
|
11
|
+
File.read('config/seed_reaper.yml')
|
12
|
+
)
|
13
|
+
).write!
|
14
|
+
end
|
15
|
+
end
|
data/lib/seed_reaper.rb
CHANGED
@@ -1,27 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require 'seed_reaper/seedifier'
|
4
|
+
require 'seed_reaper/seed_writer'
|
5
|
+
require 'seed_reaper/railtie' if defined?(Rails)
|
4
6
|
|
5
|
-
|
6
|
-
def initialize(config)
|
7
|
-
@config = config
|
8
|
-
end
|
9
|
-
|
10
|
-
def write!
|
11
|
-
FileUtils.rm_rf('db/seeds/.', secure: true)
|
12
|
-
|
13
|
-
@config.each_with_index do |element, i|
|
14
|
-
File.open("db/seeds/#{i.to_s.rjust(@config.count.digits.count, '0')}_#{file_name(element)}.seeds.rb", 'w') do |file|
|
15
|
-
file.write(Seedifier.new(element).seedify)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
private
|
21
|
-
|
22
|
-
def file_name(element)
|
23
|
-
return element if element.is_a?(Symbol)
|
24
|
-
|
25
|
-
element.first[0]
|
26
|
-
end
|
7
|
+
module SeedReaper
|
27
8
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: seed_reaper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Butts
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-02-
|
11
|
+
date: 2022-02-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -143,9 +143,13 @@ executables: []
|
|
143
143
|
extensions: []
|
144
144
|
extra_rdoc_files: []
|
145
145
|
files:
|
146
|
-
- lib/
|
146
|
+
- lib/Rakefile
|
147
147
|
- lib/seed_reaper.rb
|
148
|
-
- lib/
|
148
|
+
- lib/seed_reaper/config_evaluator.rb
|
149
|
+
- lib/seed_reaper/railtie.rb
|
150
|
+
- lib/seed_reaper/seed_writer.rb
|
151
|
+
- lib/seed_reaper/seedifier.rb
|
152
|
+
- lib/seed_reaper/tasks/seed_reaper.rake
|
149
153
|
homepage: https://github.com/dbutts2/seed_reaper
|
150
154
|
licenses:
|
151
155
|
- MIT
|
data/lib/config_evaluator.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class ConfigEvaluator
|
4
|
-
def initialize(config)
|
5
|
-
@config = config
|
6
|
-
end
|
7
|
-
|
8
|
-
def schema
|
9
|
-
return nil unless @config
|
10
|
-
return @config.reject { |k| k == :meta } if @config.is_a?(Hash)
|
11
|
-
return @config.reject { |c| c.is_a?(Hash) && c.has_key?(:meta) } if @config.is_a?(Array)
|
12
|
-
|
13
|
-
@config
|
14
|
-
end
|
15
|
-
|
16
|
-
%i[count joins].each do |meta_field|
|
17
|
-
define_method meta_field do
|
18
|
-
meta(meta_field)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
|
-
|
24
|
-
def meta(field)
|
25
|
-
return nil unless @config
|
26
|
-
return @config.dig(:meta, field) if @config.is_a?(Hash)
|
27
|
-
return nil unless @config.is_a?(Array)
|
28
|
-
|
29
|
-
@config.select do |c|
|
30
|
-
c.is_a?(Hash) && c.has_key?(:meta)
|
31
|
-
end.first&.dig(:meta, field)
|
32
|
-
end
|
33
|
-
end
|
data/lib/seedifier.rb
DELETED
@@ -1,103 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'active_support/inflector'
|
4
|
-
require 'config_evaluator'
|
5
|
-
|
6
|
-
class Seedifier
|
7
|
-
def initialize(config)
|
8
|
-
@config = config
|
9
|
-
end
|
10
|
-
|
11
|
-
def seedify
|
12
|
-
seedify_collection(base_model, base_config)
|
13
|
-
end
|
14
|
-
|
15
|
-
private
|
16
|
-
|
17
|
-
def base_model
|
18
|
-
return @config.to_s.camelize.constantize if @config.is_a?(Symbol)
|
19
|
-
|
20
|
-
@config.first[0].to_s.camelize.constantize
|
21
|
-
end
|
22
|
-
|
23
|
-
def base_config
|
24
|
-
return nil if @config.is_a?(Symbol)
|
25
|
-
|
26
|
-
@config.first[1]
|
27
|
-
end
|
28
|
-
|
29
|
-
def seedify_collection(collection, config)
|
30
|
-
ce = ConfigEvaluator.new(config)
|
31
|
-
evaluated_collection(collection, ce).reduce('') do |str, instance|
|
32
|
-
str += serialize(instance) + seedify_associations(instance, ce.schema)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def seedify_associations(instance, config)
|
37
|
-
return '' if config.nil?
|
38
|
-
|
39
|
-
if config.is_a?(Symbol)
|
40
|
-
instance.send(config).tap do |association|
|
41
|
-
return '' if association.blank?
|
42
|
-
|
43
|
-
association = [association] unless association.respond_to?(:each)
|
44
|
-
return seedify_collection(association, nil)
|
45
|
-
end
|
46
|
-
elsif config.is_a?(Array)
|
47
|
-
config.map do |sub_config|
|
48
|
-
seedify_associations(instance, sub_config)
|
49
|
-
end.join
|
50
|
-
elsif config.is_a?(Hash)
|
51
|
-
config.map do |(association_name, sub_config)|
|
52
|
-
association = instance.send(association_name)
|
53
|
-
next '' if association.blank?
|
54
|
-
|
55
|
-
association = [association] unless association.respond_to?(:each)
|
56
|
-
seedify_collection(association, sub_config)
|
57
|
-
end.join
|
58
|
-
else
|
59
|
-
fail "Your input config contains a #{config.class_name}. You may only use symbols, arrays and hashes."
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
def evaluated_collection(collection, config_evaluator)
|
64
|
-
limited_collection(
|
65
|
-
joined_collection(collection, config_evaluator.joins),
|
66
|
-
config_evaluator.count
|
67
|
-
)
|
68
|
-
end
|
69
|
-
|
70
|
-
def limited_collection(collection, count)
|
71
|
-
return collection unless collection.respond_to?(:limit)
|
72
|
-
return collection.limit(count) if count
|
73
|
-
|
74
|
-
collection.all
|
75
|
-
end
|
76
|
-
|
77
|
-
def joined_collection(collection, joins)
|
78
|
-
return collection unless collection.respond_to?(:joins)
|
79
|
-
return collection.joins(joins) if joins
|
80
|
-
|
81
|
-
collection
|
82
|
-
end
|
83
|
-
|
84
|
-
def serialize(instance)
|
85
|
-
"#{instance.class}.new(\n#{serialize_attrs(instance)}\n).save!(validate: false)\n\n"
|
86
|
-
end
|
87
|
-
|
88
|
-
def serialize_attrs(instance)
|
89
|
-
instance.attributes.to_h.reduce('') do |attr_str, (k, v)|
|
90
|
-
attr_str += "\s\s#{k}: #{serialize_value(v)},\n"
|
91
|
-
end[0...-2]
|
92
|
-
end
|
93
|
-
|
94
|
-
def serialize_value(value)
|
95
|
-
if value.nil?
|
96
|
-
"nil"
|
97
|
-
elsif value.is_a?(Integer)
|
98
|
-
value
|
99
|
-
else
|
100
|
-
"\"#{value}\""
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|