denormalized 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 655ccef71078b61b1dc00eab72802c709cf270c658f8019ebfd9a9e1efd2db5e
4
+ data.tar.gz: e4a645f0f8eff7a8b230d7cff72fad6d726ad9f286a6f4f760df7dff258810a3
5
+ SHA512:
6
+ metadata.gz: d9fbf8af4d7b8a0a500e4a97e0394b03a0651b4828b8ae0d6209a863fef9f19c2dfe60b0953a2837393219d4d70c286060470c9cf60299a7cf305226109d4dbb
7
+ data.tar.gz: 8b478478b2d3d03b726d2cdd79f217290cdea8849789b15d2ae2bdaad219b973afbc5ef1f044cbf97474188e5389dd16fcd675d504d9c5cafade0561af0e76c5
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in denormalized.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 12.0'
@@ -0,0 +1,42 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ denormalized (0.1.2)
5
+ activerecord (>= 4.2, < 5.0)
6
+ activesupport (>= 4.2, < 5.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (4.2.11.1)
12
+ activesupport (= 4.2.11.1)
13
+ builder (~> 3.1)
14
+ activerecord (4.2.11.1)
15
+ activemodel (= 4.2.11.1)
16
+ activesupport (= 4.2.11.1)
17
+ arel (~> 6.0)
18
+ activesupport (4.2.11.1)
19
+ i18n (~> 0.7)
20
+ minitest (~> 5.1)
21
+ thread_safe (~> 0.3, >= 0.3.4)
22
+ tzinfo (~> 1.1)
23
+ arel (6.0.4)
24
+ builder (3.2.3)
25
+ concurrent-ruby (1.1.5)
26
+ i18n (0.9.5)
27
+ concurrent-ruby (~> 1.0)
28
+ minitest (5.11.3)
29
+ rake (12.3.3)
30
+ thread_safe (0.3.6)
31
+ tzinfo (1.2.5)
32
+ thread_safe (~> 0.1)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ denormalized!
39
+ rake (~> 12.0)
40
+
41
+ BUNDLED WITH
42
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 OneNeptune
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,78 @@
1
+ # Denormalized
2
+
3
+ Denormalized facilitates a simplistic guarding of denormalized followers on any write to the specified source of truth. Denormalized relies on monkey-patching the write methods in ActiveRecord.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'denormalized'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install denormalized
20
+
21
+ ## Usage
22
+
23
+ Currently only tested to work for ActiveRecord 4.2.x -- it should be fairly easy to adapt it to work with version 5 or 6, but at the time the author hasn't required such use.
24
+
25
+ For the "denormalized follower", you will need a column that has the same name as the column on the "source of truth". Here's an example:
26
+
27
+ ```ruby
28
+ follower = DenormalizedFollower.find(1)
29
+ follower.name
30
+ => "bingo"
31
+
32
+ source_of_truth = SourceOfTruth.find(1)
33
+ source_of_truth.name
34
+ => "bingo"
35
+ ```
36
+
37
+ Enabled Denormalized on the "source of truth" and tell it which columns it is responsible for, and which tables should be updated:
38
+
39
+ ```ruby
40
+ class SourceOfTruth
41
+ denormalized :name, tables: [:denormalized_followers]
42
+ end
43
+ ```
44
+
45
+ Then, anytime you utilize a write method that updates the specified columns on the "source_of_truth", any followers will be automatically updated as well, using the same method.
46
+
47
+ Example:
48
+
49
+ ```ruby
50
+ follower = DenormalizedFollower.find(1)
51
+ follower.name
52
+ => "bingo"
53
+
54
+ source_of_truth = SourceOfTruth.find(1)
55
+ source_of_truth.name
56
+ => "bingo"
57
+
58
+ source_of_truth.update_column(:name, "banjo")
59
+ follower.name
60
+ => "banjo"
61
+ ```
62
+
63
+ As you can see, even a write method that typically skips callbacks will still keep the specified tables up to date. That is because Denormalized monkey-patches the ActiveRecord writeable methods, so behind the scenes in the above example, `update_column` was also called on the follower. When Denormalized calls the same methods on followers, it will only pass along specified attributes, any additional attributes will be ignored, so you can safely update attributes that exist on both columns you don't want to sync as long as it's not specified in the source of truth.
64
+
65
+ ## Development
66
+
67
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
68
+
69
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
70
+
71
+ ## Contributing
72
+
73
+ Bug reports and pull requests are welcome on GitHub at https://github.com/RecruitiFi/denormalized.
74
+
75
+
76
+ ## License
77
+
78
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'denormalized'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/denormalized/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'denormalized'
7
+ spec.version = Denormalized::VERSION
8
+ spec.authors = ['OneNeptune']
9
+ spec.email = ['michael@michaelbennett.nyc']
10
+
11
+ spec.summary = 'Facilitate simplistic guarding of denormalized ' \
12
+ 'followers on update of source of truth.'
13
+ spec.homepage = 'https://github.com/RecruitiFi/denormalized'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = spec.homepage
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 { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = 'exe'
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ['lib']
28
+
29
+ spec.add_dependency 'activerecord', '>= 4.2', '< 5.0'
30
+ spec.add_dependency 'activesupport', '>= 4.2', '< 5.0'
31
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+ require 'denormalized/core'
5
+
6
+ module Denormalized
7
+ def denormalized?
8
+ included_modules.include?(Denormalized::Core)
9
+ end
10
+
11
+ def denormalized(*attributes)
12
+ options = attributes.extract_options!.dup
13
+
14
+ if attributes.empty?
15
+ raise ArgumentError, 'You need to supply at least one column'
16
+ end
17
+ if options.empty? || options[:tables]&.empty?
18
+ raise ArgumentError, 'You need to supply at least one table'
19
+ end
20
+
21
+ class_attribute :denormalized_configuration
22
+
23
+ self.denormalized_configuration = {
24
+ columns: attributes,
25
+ tables: options[:tables],
26
+ columns_hash: Hash[attributes.map { |column| [column, true] }]
27
+ }
28
+
29
+ include Denormalized::Core unless denormalized?
30
+ end
31
+ end
32
+
33
+ # Extend ActiveRecord's functionality
34
+ ActiveRecord::Base.extend Denormalized
@@ -0,0 +1,162 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Denormalized
4
+ module Core
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ def denormalized_subject(table)
10
+ table.to_s.classify.constantize
11
+ end
12
+
13
+ def denormalized_subjects(table:, where_attrs:)
14
+ denormalized_subject(
15
+ table
16
+ ).where(
17
+ where_attrs
18
+ ).uniq
19
+ end
20
+
21
+ def denormalized_tables
22
+ denormalized_configuration[:tables].uniq
23
+ end
24
+
25
+ def contains_denormalized_attributes(attributes)
26
+ attributes.keys.any? do |name|
27
+ self.class.denormalized_attribute?(name)
28
+ end
29
+ end
30
+
31
+ def extract_existing_denormalized_attributes(new_attributes)
32
+ {}.tap do |extracted_attributes|
33
+ (denormalized_configuration[:columns] & new_attributes.keys).each do |attribute|
34
+ extracted_attributes[attribute] = read_attribute(attribute)
35
+ end
36
+ end
37
+ end
38
+
39
+ def extract_denormalized_attributes(new_attributes)
40
+ {}.tap do |extracted_attributes|
41
+ (denormalized_configuration[:columns] & new_attributes.keys).each do |attribute|
42
+ extracted_attributes[attribute] = new_attributes[attribute]
43
+ end
44
+ end
45
+ end
46
+
47
+ def write_attribute(attr_name, value)
48
+ if self.class.denormalized_attribute?(attr_name)
49
+ denormalized_tables.each do |table|
50
+ denormalized_subjects(
51
+ table: table,
52
+ where_attrs: { attr_name => read_attribute(attr_name) }
53
+ ).each { |obj| obj.send(:write_attribute, attr_name, value) }
54
+ end
55
+ end
56
+
57
+ super
58
+ end
59
+
60
+ def update_attribute(name, value)
61
+ if self.class.denormalized_attribute?(attr_name)
62
+ denormalized_tables.each do |table|
63
+ denormalized_subjects(
64
+ table: table,
65
+ where_attrs: { name => read_attribute(name) }
66
+ ).each { |obj| obj.send(:update_attribute, name, value) }
67
+ end
68
+ end
69
+
70
+ super
71
+ end
72
+
73
+ def update(attributes)
74
+ if contains_denormalized_attributes(attributes)
75
+ gifted_attributes = extract_denormalized_attributes(attributes)
76
+
77
+ denormalized_tables.each do |table|
78
+ denormalized_subjects(
79
+ table: table,
80
+ where_attrs: extract_existing_denormalized_attributes(attributes)
81
+ ).each { |obj| obj.update(gifted_attributes) }
82
+ end
83
+ end
84
+
85
+ super
86
+ end
87
+
88
+ def assign_attributes(new_attributes)
89
+ if contains_denormalized_attributes(new_attributes)
90
+ gifted_attributes = extract_denormalized_attributes(new_attributes)
91
+
92
+ denormalized_tables.each do |table|
93
+ denormalized_subjects(
94
+ table: table,
95
+ where_attrs: extract_existing_denormalized_attributes(new_attributes)
96
+ ).each { |obj| obj.assign_attributes(gifted_attributes) }
97
+ end
98
+ end
99
+
100
+ super
101
+ end
102
+
103
+ def update_columns(attributes)
104
+ if contains_denormalized_attributes(attributes)
105
+ gifted_attributes = extract_denormalized_attributes(attributes)
106
+
107
+ denormalized_tables.each do |table|
108
+ denormalized_subjects(
109
+ table: table,
110
+ where_attrs: extract_existing_denormalized_attributes(attributes)
111
+ ).each { |obj| obj.send(:update_columns, gifted_attributes) }
112
+ end
113
+ end
114
+
115
+ super
116
+ end
117
+
118
+ module ClassMethods
119
+ def denormalized_attribute?(name)
120
+ name = name.to_sym if name.is_a?(String)
121
+
122
+ if name.is_a?(Symbol)
123
+ return denormalized_configuration[:columns_hash][name]
124
+ end
125
+
126
+ Rails.logger.warn '[DENORMALIZED]: Symbol expected, instead received ' + name.class.to_s
127
+ false
128
+ end
129
+
130
+ def update(id, attributes)
131
+ if attributes.keys.any? { |name| denormalized_attribute?(name) }
132
+ subjects = base_class.where(id: id) # we will let super handle errors if subject doesn't exist
133
+
134
+ if subjects.any?
135
+ subjects.each do |subject|
136
+ gifted_attributes = subject.extract_denormalized_attributes(attributes)
137
+
138
+ denormalized_configuration[:tables].each do |table|
139
+ subject.denormalized_action(
140
+ table: table,
141
+ where_attrs: subject.extract_existing_denormalized_attributes(attributes),
142
+ method: :update,
143
+ args: gifted_attributes
144
+ )
145
+ end
146
+ end
147
+ end
148
+ end
149
+
150
+ super
151
+ end
152
+
153
+ def update_all(updates)
154
+ if updates.keys.any? { |name| denormalized_attribute?(name) }
155
+ Rails.logger.warn '[DENORMALIZED]: Unable to update followers when using `update_all` -- orphans may have been created'
156
+ end
157
+
158
+ super
159
+ end
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Denormalized
4
+ VERSION = '0.1.2'
5
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: denormalized
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - OneNeptune
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '4.2'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: activesupport
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '4.2'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '5.0'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '4.2'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '5.0'
53
+ description:
54
+ email:
55
+ - michael@michaelbennett.nyc
56
+ executables: []
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - ".gitignore"
61
+ - Gemfile
62
+ - Gemfile.lock
63
+ - LICENSE.txt
64
+ - README.md
65
+ - Rakefile
66
+ - bin/console
67
+ - bin/setup
68
+ - denormalized.gemspec
69
+ - lib/denormalized.rb
70
+ - lib/denormalized/core.rb
71
+ - lib/denormalized/version.rb
72
+ homepage: https://github.com/RecruitiFi/denormalized
73
+ licenses:
74
+ - MIT
75
+ metadata:
76
+ homepage_uri: https://github.com/RecruitiFi/denormalized
77
+ source_code_uri: https://github.com/RecruitiFi/denormalized
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 2.3.0
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.7.9
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Facilitate simplistic guarding of denormalized followers on update of source
98
+ of truth.
99
+ test_files: []