consyncful-tree 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8f9dfd640ea20ac184288e3af655a3def80755429ca2d0880d3d05af4848bd3f
4
+ data.tar.gz: e523aa66dcc6f005f661eb4bba23644f52b83b0a48d6fb0b6774ec631b760fc9
5
+ SHA512:
6
+ metadata.gz: 3f68b3fef751d66d2e3ae02d4a21819fda521a1bbd3ae49bbc5a5c78eb81826ed37d5bc83a11c094ce232995d465443000fdca03585303be7dfcf6a7d40735ce
7
+ data.tar.gz: 98a49750eb6a42057da56e97e92b36cce1ac9fb9d8618428b62fcc95232b3f4f52150cca265fde4bbf5376f88f8f0bffb4f58ca28ae829b6001faf43c010e3b6
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,17 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.7
3
+ NewCops: enable
4
+
5
+ Style/StringLiterals:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Layout/LineLength:
14
+ Max: 120
15
+
16
+ Style/Documentation:
17
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [1.0.0] - 2022-06-14
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in consyncful-tree.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,134 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ consyncful-tree (1.0.0)
5
+ consyncful (~> 0.7.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (7.0.3)
11
+ activesupport (= 7.0.3)
12
+ activesupport (7.0.3)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 1.6, < 2)
15
+ minitest (>= 5.1)
16
+ tzinfo (~> 2.0)
17
+ addressable (2.8.0)
18
+ public_suffix (>= 2.0.2, < 5.0)
19
+ ast (2.4.2)
20
+ bson (4.15.0)
21
+ byebug (11.1.3)
22
+ coderay (1.1.3)
23
+ concurrent-ruby (1.1.10)
24
+ consyncful (0.7.0)
25
+ contentful (>= 2.11.1, < 3.0.0)
26
+ hooks (>= 0.4.1)
27
+ mongoid (>= 7.0.2, < 8.0.0)
28
+ rainbow
29
+ contentful (2.16.3)
30
+ http (> 0.8, < 5.0)
31
+ multi_json (~> 1)
32
+ database_cleaner-core (2.0.1)
33
+ database_cleaner-mongoid (2.0.1)
34
+ database_cleaner-core (~> 2.0.0)
35
+ mongoid
36
+ diff-lcs (1.5.0)
37
+ domain_name (0.5.20190701)
38
+ unf (>= 0.0.5, < 1.0.0)
39
+ ffi (1.15.5)
40
+ ffi-compiler (1.0.1)
41
+ ffi (>= 1.0.0)
42
+ rake
43
+ hooks (0.4.1)
44
+ uber (~> 0.0.14)
45
+ http (4.4.1)
46
+ addressable (~> 2.3)
47
+ http-cookie (~> 1.0)
48
+ http-form_data (~> 2.2)
49
+ http-parser (~> 1.2.0)
50
+ http-cookie (1.0.5)
51
+ domain_name (~> 0.5)
52
+ http-form_data (2.3.0)
53
+ http-parser (1.2.3)
54
+ ffi-compiler (>= 1.0, < 2.0)
55
+ i18n (1.10.0)
56
+ concurrent-ruby (~> 1.0)
57
+ method_source (1.0.0)
58
+ minitest (5.15.0)
59
+ mongo (2.17.1)
60
+ bson (>= 4.8.2, < 5.0.0)
61
+ mongoid (7.4.0)
62
+ activemodel (>= 5.1, < 7.1, != 7.0.0)
63
+ mongo (>= 2.10.5, < 3.0.0)
64
+ ruby2_keywords (~> 0.0.5)
65
+ multi_json (1.15.0)
66
+ parallel (1.22.1)
67
+ parser (3.1.2.0)
68
+ ast (~> 2.4.1)
69
+ pry (0.13.1)
70
+ coderay (~> 1.1)
71
+ method_source (~> 1.0)
72
+ pry-byebug (3.9.0)
73
+ byebug (~> 11.0)
74
+ pry (~> 0.13.0)
75
+ public_suffix (4.0.7)
76
+ rainbow (3.1.1)
77
+ rake (13.0.6)
78
+ regexp_parser (2.5.0)
79
+ rexml (3.2.5)
80
+ rspec (3.11.0)
81
+ rspec-core (~> 3.11.0)
82
+ rspec-expectations (~> 3.11.0)
83
+ rspec-mocks (~> 3.11.0)
84
+ rspec-core (3.11.0)
85
+ rspec-support (~> 3.11.0)
86
+ rspec-expectations (3.11.0)
87
+ diff-lcs (>= 1.2.0, < 2.0)
88
+ rspec-support (~> 3.11.0)
89
+ rspec-mocks (3.11.1)
90
+ diff-lcs (>= 1.2.0, < 2.0)
91
+ rspec-support (~> 3.11.0)
92
+ rspec-support (3.11.0)
93
+ rubocop (1.30.0)
94
+ parallel (~> 1.10)
95
+ parser (>= 3.1.0.0)
96
+ rainbow (>= 2.2.2, < 4.0)
97
+ regexp_parser (>= 1.8, < 3.0)
98
+ rexml (>= 3.2.5, < 4.0)
99
+ rubocop-ast (>= 1.18.0, < 2.0)
100
+ ruby-progressbar (~> 1.7)
101
+ unicode-display_width (>= 1.4.0, < 3.0)
102
+ rubocop-ast (1.18.0)
103
+ parser (>= 3.1.1.0)
104
+ rubocop-rake (0.6.0)
105
+ rubocop (~> 1.0)
106
+ rubocop-rspec (2.11.1)
107
+ rubocop (~> 1.19)
108
+ ruby-progressbar (1.11.0)
109
+ ruby2_keywords (0.0.5)
110
+ tzinfo (2.0.4)
111
+ concurrent-ruby (~> 1.0)
112
+ uber (0.0.15)
113
+ unf (0.1.4)
114
+ unf_ext
115
+ unf_ext (0.0.8.2)
116
+ unicode-display_width (2.1.0)
117
+
118
+ PLATFORMS
119
+ x86_64-darwin-19
120
+ x86_64-linux
121
+
122
+ DEPENDENCIES
123
+ bundler (~> 2)
124
+ consyncful-tree!
125
+ database_cleaner-mongoid (~> 2.0)
126
+ pry-byebug
127
+ rake (~> 13.0)
128
+ rspec (~> 3.0)
129
+ rubocop
130
+ rubocop-rake
131
+ rubocop-rspec
132
+
133
+ BUNDLED WITH
134
+ 2.3.7
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # Consyncful::Tree
2
+
3
+ For those already using the `consyncful` gem, `consyncful-tree` provides a few additional concerns to help with Consyncful models nested in a tree structure.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'consyncful-tree'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install consyncful-tree
20
+
21
+ ## Usage
22
+
23
+ ### Parent
24
+ Include the Consyncful::Tree::Parent concern in a Consyncful model that you want to keep track of the dependencies of.
25
+
26
+ ```
27
+ class ModelName < Consyncful::Base
28
+ include Consyncful::Tree::Parent
29
+
30
+ ...
31
+ end
32
+ ```
33
+
34
+ This will give you the following three methods that recursively traverse the `references_one` + `references_many` relationships:
35
+ ```
36
+ lookup_child_model_ids
37
+
38
+ lookup_child_models
39
+
40
+ with_child_class_of?
41
+ ```
42
+
43
+ ### Child
44
+
45
+ Include the Consyncful::Tree::Child concern in any Consyncful model that is a dependency of a Consyncful::Tree::Parent and that should touch that parent when it is updated.
46
+
47
+ ```
48
+ class ModelName < Consyncful::Base
49
+ include Consyncful::Tree::Child
50
+
51
+ ...
52
+ end
53
+ ```
54
+
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org). You'll need an account on [rubygems.org](https://rubygems.org), you'll also need a current owner of the gem to add you as an owner.
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/DigitalNZ/consyncful-tree.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/consyncful/tree/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "consyncful-tree"
7
+ spec.version = Consyncful::Tree::VERSION
8
+ spec.authors = ["DigitalNZ"]
9
+ spec.email = ["info@digitalnz.org"]
10
+
11
+ spec.summary = "Code to help with adding a tree structure to Contentful models."
12
+ spec.description = "Code to help with adding a tree structure to Contentful models."
13
+ spec.homepage = "https://github.com/DigitalNZ/consyncful-tree"
14
+ spec.required_ruby_version = ">= 2.7.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/DigitalNZ/consyncful-tree"
18
+ spec.metadata["changelog_uri"] = "https://github.com/DigitalNZ/consyncful-tree/blob/main/CHANGELOG.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 2"
32
+ spec.add_development_dependency "database_cleaner-mongoid", "~> 2.0"
33
+ spec.add_development_dependency "pry-byebug"
34
+ spec.add_development_dependency "rake", "~> 13.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+ spec.add_development_dependency "rubocop"
37
+ spec.add_development_dependency "rubocop-rake"
38
+ spec.add_development_dependency "rubocop-rspec"
39
+
40
+ spec.add_dependency "consyncful", "~> 0.7.0"
41
+
42
+ # For more information and examples about making a new gem, check out our
43
+ # guide at: https://bundler.io/guides/creating_gem.html
44
+ spec.metadata["rubygems_mfa_required"] = "true"
45
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+
5
+ module Consyncful
6
+ module Tree
7
+ module Child
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ after_save do
12
+ parents.map(&:touch)
13
+ end
14
+
15
+ def parents
16
+ Consyncful::Base.where(child_model_ids: id)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+
5
+ # Parent module is included to parent Consyncful models
6
+ # This module allows the current parent class to lookup its children no matter where they are.
7
+ module Consyncful
8
+ module Tree
9
+ module Parent
10
+ extend ActiveSupport::Concern
11
+
12
+ included do
13
+ references_many :child_models
14
+
15
+ before_save do
16
+ self.child_model_ids = lookup_child_model_ids
17
+ end
18
+ end
19
+
20
+ def lookup_child_model_ids(context: self)
21
+ context.relations.map do |key, val|
22
+ child_ids = context[val.foreign_key]
23
+ child_ids = Array.wrap(child_ids)
24
+
25
+ next if key == "child_models" || child_ids.empty?
26
+
27
+ child_ids + lookup_child_objects(child_ids)
28
+ end.flatten.compact
29
+ end
30
+
31
+ def lookup_child_objects(child_ids)
32
+ return [] if child_ids.empty?
33
+
34
+ child_ids.map do |id|
35
+ obj = Consyncful::Base.where(id: id).first
36
+
37
+ next if obj.nil?
38
+
39
+ lookup_child_model_ids(context: obj) if classes_with_parent_concern.exclude?(obj.class.to_s)
40
+ end.flatten.compact
41
+ end
42
+
43
+ # Returns true/false if the parent has a child model with a specific class.
44
+ # This is useful if you want to trace a child's class on a specific parent.
45
+ def with_child_class_of?(klass)
46
+ return false if klass.nil?
47
+
48
+ klasses = child_models.map(&:class).uniq.map(&:to_s)
49
+
50
+ klasses.include?(klass.to_s)
51
+ end
52
+
53
+ private
54
+
55
+ def classes_with_parent_concern
56
+ # ObjectSpace lets you interact with garbage collection and traverse alive objects.
57
+ # This will iterate through all the classes for objects that are alive.
58
+ #
59
+ # NOTE: when a Rails app is running, it will have all the classes loaded, but in tests or the console
60
+ # for this Gem, it will just be the ones used in the test.
61
+ ObjectSpace.each_object(Class).select { |c| c.included_modules.include?(Consyncful::Tree::Parent) }
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Consyncful
4
+ module Tree
5
+ VERSION = "1.0.0"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "consyncful/tree/version"
4
+ require "consyncful/tree/concerns/child"
5
+ require "consyncful/tree/concerns/parent"
6
+
7
+ module Consyncful
8
+ module Tree
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ module Consyncful::Tree
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,185 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: consyncful-tree
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - DigitalNZ
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-06-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: database_cleaner-mongoid
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '13.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '13.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: consyncful
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 0.7.0
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 0.7.0
139
+ description: Code to help with adding a tree structure to Contentful models.
140
+ email:
141
+ - info@digitalnz.org
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".rspec"
147
+ - ".rubocop.yml"
148
+ - CHANGELOG.md
149
+ - Gemfile
150
+ - Gemfile.lock
151
+ - README.md
152
+ - Rakefile
153
+ - consyncful-tree.gemspec
154
+ - lib/consyncful/tree.rb
155
+ - lib/consyncful/tree/concerns/child.rb
156
+ - lib/consyncful/tree/concerns/parent.rb
157
+ - lib/consyncful/tree/version.rb
158
+ - sig/consyncful_tree.rbs
159
+ homepage: https://github.com/DigitalNZ/consyncful-tree
160
+ licenses: []
161
+ metadata:
162
+ homepage_uri: https://github.com/DigitalNZ/consyncful-tree
163
+ source_code_uri: https://github.com/DigitalNZ/consyncful-tree
164
+ changelog_uri: https://github.com/DigitalNZ/consyncful-tree/blob/main/CHANGELOG.md
165
+ rubygems_mfa_required: 'true'
166
+ post_install_message:
167
+ rdoc_options: []
168
+ require_paths:
169
+ - lib
170
+ required_ruby_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: 2.7.0
175
+ required_rubygems_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ requirements: []
181
+ rubygems_version: 3.2.32
182
+ signing_key:
183
+ specification_version: 4
184
+ summary: Code to help with adding a tree structure to Contentful models.
185
+ test_files: []