seed_reaper 0.0.4 → 0.0.5

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: 84b0a2cbec760f86053dbc8df84816f0fec6f55ec3598e52d8f8ddb3ed5b61e9
4
- data.tar.gz: 8a096dac0835f12535249231686b8ecd10a3b9bac6973adaee7c2c157541db0b
3
+ metadata.gz: 1fad4ebc284e6a5e6a30df7e9a3305c70d48060d4d12a4a48f69391149fc975a
4
+ data.tar.gz: a66337b1c8749eb67a42b1fb1bb9d601e8f1cc21cf2002cb22044e534007b81f
5
5
  SHA512:
6
- metadata.gz: 5f7cf51a355a87a65567d0ce8659e897ebc127dff5ebe46ec1fd86ff68e1686b9e45e0008bdbfb8b6f4c52502a9a00891fbd189063fa074bf89c1681b6bd8432
7
- data.tar.gz: f0b8bc62ed2b49937a6be284a6efb6bcfeaf1d049cd4b55f3c2274706ee61ef6e5c28d9dfe19dea5e21276244b4dbf38ac15ae5e2cbcfcf27891c02f8f183283
6
+ metadata.gz: 9f5756b19dd5aaac7567cfbe78669e96e0c936d9cad7f50f1ecddbc9272581018b4bf1ef27d88bbee83b713c40d5278f36e777956cf473f1b533f480cc49d884
7
+ data.tar.gz: f78cedba61282dfe2f36c25b80c637630a39a2051bf35471df3922d9efed6d9d31d4058919ceec443f6b5b66d36ef177c76e9ecf8156593649bf7a1082942bfb
@@ -14,6 +14,32 @@ module SeedReaper
14
14
  @config
15
15
  end
16
16
 
17
+ def belongs_to_schema(instance)
18
+ return nil unless schema
19
+ return schema.select { |k| instance._reflections[k.to_s].belongs_to? } if schema.is_a?(Hash)
20
+ return schema.select do |c|
21
+ if c.is_a?(Hash)
22
+ instance._reflections[c.first[0].to_s].belongs_to?
23
+ else
24
+ instance._reflections[c.to_s].belongs_to?
25
+ end
26
+ end if schema.is_a?(Array)
27
+ return schema if instance._reflections[schema.to_s].belongs_to?
28
+ end
29
+
30
+ def non_belongs_to_schema(instance)
31
+ return nil unless schema
32
+ return schema.reject { |k| instance._reflections[k.to_s].belongs_to? } if schema.is_a?(Hash)
33
+ return schema.reject do |c|
34
+ if c.is_a?(Hash)
35
+ instance._reflections[c.first[0].to_s].belongs_to?
36
+ else
37
+ instance._reflections[c.to_s].belongs_to?
38
+ end
39
+ end if schema.is_a?(Array)
40
+ return schema unless instance._reflections[schema.to_s].belongs_to?
41
+ end
42
+
17
43
  %i[count joins].each do |meta_field|
18
44
  define_method meta_field do
19
45
  meta(meta_field)
@@ -8,6 +8,7 @@ module SeedReaper
8
8
  class Seedifier
9
9
  def initialize(config)
10
10
  @config = config
11
+ @serialized = []
11
12
  end
12
13
 
13
14
  def seedify
@@ -31,7 +32,10 @@ module SeedReaper
31
32
  def seedify_collection(collection, config)
32
33
  ce = ConfigEvaluator.new(config)
33
34
  evaluated_collection(collection, ce).reduce('') do |str, instance|
34
- str += serialize(instance) + seedify_associations(instance, ce.schema)
35
+ str +=
36
+ seedify_associations(instance, ce.belongs_to_schema(instance)) +
37
+ serialize(instance) +
38
+ seedify_associations(instance, ce.non_belongs_to_schema(instance))
35
39
  end
36
40
  end
37
41
 
@@ -84,7 +88,12 @@ module SeedReaper
84
88
  end
85
89
 
86
90
  def serialize(instance)
87
- "#{instance.class}.new(\n#{serialize_attrs(instance)}\n).save!(validate: false)\n\n"
91
+ seed = "#{instance.class}.new(\n#{serialize_attrs(instance)}\n).save!(validate: false)\n\n"
92
+ hashed_seed = Digest::SHA2.hexdigest(seed)
93
+ return '' if @serialized.include?(hashed_seed)
94
+
95
+ @serialized << hashed_seed
96
+ seed
88
97
  end
89
98
 
90
99
  def serialize_attrs(instance)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SeedReaper
4
- VERSION = '0.0.4'
4
+ VERSION = '0.0.5'
5
5
  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
4
+ version: 0.0.5
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-17 00:00:00.000000000 Z
11
+ date: 2022-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport