nexus_seed 0.1.0 → 0.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: 3fb70231c76e859262ea28255d625d4904b99a2e4fae96da3baeb5f9a2e30f36
4
- data.tar.gz: 53a9e3507714af99e8e826e11ebf55bae6e8f89ad0d5ba0c3e86a90f08562a76
3
+ metadata.gz: 358aa07ea013df46b776417ef479210e6ac818251615282ac2c2ba27a8242bed
4
+ data.tar.gz: 374ee49187d7b254bb6497f54272ea6428b3a887fff5b2eaf43564a92e351833
5
5
  SHA512:
6
- metadata.gz: 877f35fa660e49088ce1686eafc40cdc9d69a543c60910998764aaa6daaa7e58dea0b979c5bd02f7a8cd7fd199cd1d1695f0aa09fcec01f1169b7a6b020cda46
7
- data.tar.gz: 837e9a61883bb53c460e4b3d57e1e8c0c53ccc547d3f20411b0a92f5608d3ae0eaa6823c4025e838e6ddf6a45b125881c3653213ff384cfe57bb31529700bee7
6
+ metadata.gz: 2e305882915b41878297d35bb7e7f99376d0d97bdb85e5524caf17da4930fcc60ec1abc9780a1b7f4156917eb5db5000e5e8e04f6929d8526f3a3411f7d461af
7
+ data.tar.gz: fa1f505b5e10edbbc5f9b9b471eb03e6cfbfa02dd748ee87371f7bd0c8836c22f5ca26d6369857fda36b9ce27e13fc10b0768c6e7396872be36f485af33bf54c
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+ module NexusSeed
3
+ module Builder
4
+ class Base
5
+ @options = {}
6
+
7
+ def initialize(options = {})
8
+ @options = options
9
+ @find_by_params_field = options[:find_by_params_field].nil? ? :find_by_params : options[:find_by_params_field]
10
+ end
11
+
12
+ def defaults(params = {}); end
13
+
14
+ def model_class; end
15
+
16
+ def find_by_params(instance = nil, params = nil); end
17
+
18
+ def build(params)
19
+ # merge defaults with params
20
+ merged_params = if defaults(params).nil?
21
+ params
22
+ else
23
+ defaults(params).merge(params)
24
+ end
25
+
26
+ instance = klass.new(merged_params)
27
+
28
+ # set the custom find_by_params if provided
29
+ @find_by_params = if @options.key?(:find_by_params)
30
+ @options[:find_by_params]
31
+ else
32
+ find_by_params(instance, params)
33
+ end
34
+
35
+ raise StandardError, "Error: find_by_params must not be nil" if @find_by_params.nil?
36
+
37
+ before_save(instance, params)
38
+
39
+ find_by_query = if @find_by_params.is_a?(Hash)
40
+ @find_by_params
41
+ else
42
+ @find_by_params = @find_by_params.is_a?(Array) ? @find_by_params : [@find_by_params]
43
+
44
+ query_hash = {}
45
+
46
+ @find_by_params.each do |e|
47
+ query_hash[e] = instance[e]
48
+ end
49
+
50
+ query_hash
51
+ end
52
+
53
+ existing = klass.find_by(find_by_query)
54
+
55
+ result = if existing.nil?
56
+ instance.save!
57
+ instance
58
+ else
59
+ existing
60
+ end
61
+
62
+ NexusSeed::Builder.add_seed(result) if ENV['destroy_seeds']
63
+
64
+ after_save(result, params)
65
+ end
66
+
67
+ def before_save(instance, params = nil)
68
+ instance
69
+ end
70
+
71
+ def after_save(instance, params = nil)
72
+ instance
73
+ end
74
+
75
+ private
76
+
77
+ def klass
78
+ model_class.nil? ? self.class.name[0...-7].demodulize.constantize : model_class
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+ module NexusSeed
3
+ module Builder
4
+ @build_called = false
5
+ @counts = {}
6
+ @start = Time.now
7
+ @seeds = []
8
+
9
+ def self.build_called
10
+ @build_called
11
+ end
12
+
13
+ def self.build(class_name, params = {}, options = {})
14
+ if ENV['seed_report']
15
+ pre_seed_counts unless @build_called
16
+ end
17
+
18
+ @build_called = true
19
+ "NexusSeed::Builders::#{class_name.to_s.camelcase}Builder".constantize.new(options).build(params)
20
+ end
21
+
22
+ def self.seed_report
23
+ if ENV['seed_report']
24
+ total = 0
25
+
26
+ pp('SEED REPORT:')
27
+ NexusSeed::Builders.constants.select do |c|
28
+ NexusSeed::Builders.const_get(c).is_a?(Class)
29
+ k = NexusSeed::Builders.const_get(c).new.send(:klass)
30
+ if @counts[k.name.demodulize] != k.count
31
+ total += (k.count - @counts[k.name.demodulize])
32
+ pp("Generated #{k.count - @counts[k.name.demodulize]} new #{k.name.demodulize}s")
33
+ end
34
+ end
35
+
36
+ pp("Generated total of #{total} new seeds.")
37
+ else
38
+ pp('Seeding completed.')
39
+ end
40
+
41
+ pp("Time: #{(Time.now-@start).round(2)}s")
42
+ end
43
+
44
+ def self.add_seed(seed)
45
+ @seeds << seed
46
+ end
47
+
48
+ def self.destroy_seeds
49
+ count = 0
50
+ @seeds.reverse.each do |seed|
51
+ count += 1
52
+ seed.destroy
53
+ end
54
+ pp("Destroyed #{count} seeds (including dependants). All seeds deleted.")
55
+ end
56
+
57
+ def self.pre_seed_counts
58
+ counts = {}
59
+
60
+ NexusSeed::Builders.constants.select do |c|
61
+ if NexusSeed::Builders.const_get(c).is_a?(Class)
62
+ k = NexusSeed::Builders.const_get(c).new.send(:klass)
63
+ counts[k.name.demodulize] = k.count
64
+ end
65
+ end
66
+
67
+ @counts = counts
68
+ end
69
+ end
70
+
71
+ module Builders
72
+
73
+ end
74
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
  module NexusSeed
3
- # Leave this as 0.1.0 in order for CI process to replace with the tagged version.
4
- VERSION = '0.1.0'
3
+ # Leave this as 0.2.0 in order for CI process to replace with the tagged version.
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/nexus_seed.rb CHANGED
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
  require 'nexus_seed'
3
+ require 'nexus_seed/builder/base'
4
+ Dir["/app/lib/seed_builder/builders/*.rb"].each { |file| require file }
5
+ require 'nexus_seed/builder'
3
6
 
4
7
  module NexusSeed
5
8
  # defaults
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nexus_seed
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johnathon Harris
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-03 00:00:00.000000000 Z
11
+ date: 2023-02-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nexus_semantic_logger
@@ -36,6 +36,8 @@ files:
36
36
  - Gemfile
37
37
  - README.md
38
38
  - lib/nexus_seed.rb
39
+ - lib/nexus_seed/builder.rb
40
+ - lib/nexus_seed/builder/base.rb
39
41
  - lib/nexus_seed/version.rb
40
42
  - nexus_seed.gemspec
41
43
  homepage: