cache_depends_on 0.1.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
+ SHA1:
3
+ metadata.gz: dc9cd539c0b5d27ae04edbc2858505f176fdf130
4
+ data.tar.gz: 627fcf125320c3827fbc1be01063b87215e53119
5
+ SHA512:
6
+ metadata.gz: 5f9261d4eebf2386de8a00d59053c099d2f2900c0bb7a6289aadf6bb25447c64f7c000573a60799d98845264a1bf004c021ac626d357721b4f957452d463716a
7
+ data.tar.gz: e4eedbe151de73a1d9834f0427e24b009e4bbc75b29a2f169f0014d6a70380e1767b2f8b9883f3ae9d6952c78ac85e718732fd78a275b5e4d2551e8aced99c9e
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /eproject.cfg
11
+ *.gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ cache_depends_on
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.1
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
6
+
7
+ gemfile:
8
+ - gemfiles/activerecord_4.gemfile
9
+ - gemfiles/activerecord_5.gemfile
data/Appraisals ADDED
@@ -0,0 +1,7 @@
1
+ appraise 'activerecord-4' do
2
+ gem 'activerecord', '~> 4'
3
+ end
4
+
5
+ appraise 'activerecord-5' do
6
+ gem 'activerecord', '~> 5'
7
+ end
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Daniel Vartanov
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.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ cache_depends_on
2
+ ================
3
+
4
+ A better way of controlling cache dependencies than `belongs_to :product, touch: true`
5
+
6
+
7
+ Example
8
+ -------
9
+
10
+ ```ruby
11
+ class Author < ActiveRecord::Base
12
+ has_many :artciles
13
+
14
+ cache_depends_on :articles
15
+ end
16
+
17
+ class Artcile < ActiveRecord::Base
18
+ belongs_to :rating
19
+
20
+ cache_depends_on :rating
21
+ end
22
+
23
+ class Rating < ActiveRecord::Base
24
+ has_one :article
25
+ end
26
+ ```
27
+
28
+ `CacheDependsOn` can deal with any kinds of ActiveRecord associations.
29
+
30
+ ...console/SQL log...
31
+
32
+
33
+ Important notes
34
+ ---------------
35
+
36
+ ### Cache invalidation always happens outside transaction
37
+
38
+ ...why...
39
+
40
+ ### Each row will be updated only ones per transaction if properly configured
41
+
42
+ If an Author has five articles and all of them are updated within a transaction, the Author will be updated *only once*.
43
+
44
+ ...why it is important...
45
+ ...how to establish 'ideal relationships' between ActiveRecord models.
46
+
47
+
48
+ How to install
49
+ --------------
50
+
51
+ Add the following to your `Gemfile`:
52
+
53
+ ```ruby
54
+ gem 'cache_depends_on'
55
+ ```
56
+
57
+ Development
58
+ -----------
59
+
60
+ 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.
61
+
62
+
63
+ Contributing
64
+ ------------
65
+
66
+ Bug reports and pull requests are welcome on GitHub at https://github.com/veeqo/cache_depends_on.
67
+
68
+
69
+
70
+ Sponsored by [Veeqo](https://github.com/veeqo)
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+
4
+ require "bundler/gem_tasks"
5
+ require "rspec/core/rake_task"
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cache_depends_on"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -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,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cache_depends_on/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'cache_depends_on'
8
+ spec.version = CacheDependsOn::VERSION
9
+ spec.authors = ['Daniel Vartanov']
10
+ spec.email = ['dan@vartanov.net']
11
+
12
+ spec.summary = %q{A better way of controlling cache dependencies than 'belongs_to :product, touch: true'}
13
+ spec.description = %q{A better way of controlling cache dependencies than 'belongs_to :product, touch: true'}
14
+ spec.homepage = 'https://github.com/veeqo/cache_depends_on'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_dependency 'activerecord', '> 3', '< 6'
25
+
26
+ spec.add_development_dependency 'bundler'
27
+ spec.add_development_dependency 'rake'
28
+ spec.add_development_dependency 'rspec'
29
+ spec.add_development_dependency 'sqlite3'
30
+ spec.add_development_dependency 'database_cleaner'
31
+ spec.add_development_dependency 'appraisal'
32
+ end
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 4"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 5"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,27 @@
1
+ require 'active_record'
2
+ require 'active_support/concern'
3
+
4
+ module CacheDependsOn
5
+ module ActiveRecordModelExtension
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ cattr_accessor(:invalidates_cache_of) { [] }
10
+
11
+ after_commit { invalidate_dependent_caches }
12
+
13
+ define_model_callbacks :cache_invalidation
14
+ end
15
+
16
+ def invalidate_cache
17
+ touch
18
+ end
19
+
20
+ def invalidate_dependent_caches
21
+ run_callbacks(:cache_invalidation) do
22
+ invalidates_cache_of.select(&:collection?).each { |association| __send__(association.name).each(&:invalidate_cache) }
23
+ invalidates_cache_of.reject(&:collection?).each { |association| __send__(association.name).try(:invalidate_cache) }
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,48 @@
1
+ module CacheDependsOn
2
+ class AssociationCacheDependencyDefiner
3
+ def initialize(model_klass)
4
+ @model_klass = model_klass
5
+ end
6
+ attr_reader :model_klass
7
+
8
+ def define_cache_dependency_on(association_names)
9
+ model_klass.class_eval { include ActiveRecordModelExtension }
10
+
11
+ find_associations_by_names(association_names).each do |association|
12
+ association.klass.class_eval { include ActiveRecordModelExtension }
13
+ association.klass.invalidates_cache_of << find_inverse_of(association)
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def find_associations_by_names(association_names)
20
+ association_names.map do |association_name|
21
+ model_klass.reflect_on_association(association_name).tap do |association|
22
+ raise AssociationNotFound, "association #{association_name} was not found in #{model_klass.name}" if association.nil?
23
+ end
24
+ end
25
+ end
26
+
27
+ def find_inverse_of(association)
28
+ find_polymorphic_inverse_of(association) ||
29
+ find_singular_inverse_of(association) ||
30
+ find_plural_inverse_of(association) ||
31
+ raise(ReverseAssociationNotFound, "reverse association of #{model_klass} was not found in #{association.klass}")
32
+ end
33
+
34
+ def find_singular_inverse_of(association)
35
+ suggested_singular_reverse_association_name = model_klass.model_name.singular.to_sym
36
+ association.klass.reflect_on_association suggested_singular_reverse_association_name
37
+ end
38
+
39
+ def find_plural_inverse_of(association)
40
+ suggested_plural_reverse_association_name = model_klass.model_name.plural.to_sym
41
+ association.klass.reflect_on_association suggested_plural_reverse_association_name
42
+ end
43
+
44
+ def find_polymorphic_inverse_of(association)
45
+ association.klass.reflect_on_association association.options[:as]
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,4 @@
1
+ module CacheDependsOn
2
+ class AssociationNotFound < RuntimeError; end
3
+ class ReverseAssociationNotFound < RuntimeError; end
4
+ end
@@ -0,0 +1,3 @@
1
+ module CacheDependsOn
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,13 @@
1
+ require 'cache_depends_on/exceptions'
2
+ require 'cache_depends_on/association_cache_dependency_definer'
3
+ require 'cache_depends_on/active_record_model_extension'
4
+
5
+ module CacheDependsOn
6
+ module ClassMethods
7
+ def cache_depends_on(*association_names)
8
+ AssociationCacheDependencyDefiner.new(self).define_cache_dependency_on association_names
9
+ end
10
+ end
11
+ end
12
+
13
+ ActiveRecord::Base.extend CacheDependsOn::ClassMethods
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cache_depends_on
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Vartanov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-28 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: '3'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">"
28
+ - !ruby/object:Gem::Version
29
+ version: '3'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: sqlite3
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: database_cleaner
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: appraisal
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ description: 'A better way of controlling cache dependencies than ''belongs_to :product,
118
+ touch: true'''
119
+ email:
120
+ - dan@vartanov.net
121
+ executables: []
122
+ extensions: []
123
+ extra_rdoc_files: []
124
+ files:
125
+ - ".gitignore"
126
+ - ".rspec"
127
+ - ".ruby-gemset"
128
+ - ".ruby-version"
129
+ - ".travis.yml"
130
+ - Appraisals
131
+ - Gemfile
132
+ - LICENSE.txt
133
+ - README.md
134
+ - Rakefile
135
+ - bin/console
136
+ - bin/setup
137
+ - cache_depends_on.gemspec
138
+ - gemfiles/activerecord_4.gemfile
139
+ - gemfiles/activerecord_5.gemfile
140
+ - lib/cache_depends_on.rb
141
+ - lib/cache_depends_on/active_record_model_extension.rb
142
+ - lib/cache_depends_on/association_cache_dependency_definer.rb
143
+ - lib/cache_depends_on/exceptions.rb
144
+ - lib/cache_depends_on/version.rb
145
+ homepage: https://github.com/veeqo/cache_depends_on
146
+ licenses:
147
+ - MIT
148
+ metadata: {}
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubyforge_project:
165
+ rubygems_version: 2.6.8
166
+ signing_key:
167
+ specification_version: 4
168
+ summary: 'A better way of controlling cache dependencies than ''belongs_to :product,
169
+ touch: true'''
170
+ test_files: []