mongoid-categorized_counter_cache 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1f08ef357971979a5c149c8fbe32b0f071c2696d
4
+ data.tar.gz: bb59afbff1cd61065874b471eeed7fadeb8c3d62
5
+ SHA512:
6
+ metadata.gz: 48032a8bafa0942c99b6c47ad32bc836cd11c988cf3942735de27f1298ef3ba54fda6988f295d9691ef8e5b498d8f7d289633babe9c5bdc69f56b2aaca3eff7f
7
+ data.tar.gz: 40d7255bec540e1d01a2071b1d5ba9924001d5b4f872be59416657b0312d11cab741d04492442c3ab8c608632d96bc53182c37bc02bac31ed5c361a7da801f54
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ tags
10
+ .byebug_history
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.7
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in mongoid-categorized_counter_cache.gemspec
6
+ gemspec
@@ -0,0 +1,59 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mongoid-categorized_counter_cache (0.1.0)
5
+ activemodel (~> 5.1)
6
+ mongoid (~> 6.2)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (5.2.1)
12
+ activesupport (= 5.2.1)
13
+ activesupport (5.2.1)
14
+ concurrent-ruby (~> 1.0, >= 1.0.2)
15
+ i18n (>= 0.7, < 2)
16
+ minitest (~> 5.1)
17
+ tzinfo (~> 1.1)
18
+ bson (4.3.0)
19
+ byebug (10.0.2)
20
+ concurrent-ruby (1.0.5)
21
+ diff-lcs (1.3)
22
+ i18n (1.1.0)
23
+ concurrent-ruby (~> 1.0)
24
+ minitest (5.11.3)
25
+ mongo (2.6.2)
26
+ bson (>= 4.3.0, < 5.0.0)
27
+ mongoid (6.2.1)
28
+ activemodel (~> 5.1)
29
+ mongo (>= 2.4.1, < 3.0.0)
30
+ rake (12.3.1)
31
+ rspec (3.7.0)
32
+ rspec-core (~> 3.7.0)
33
+ rspec-expectations (~> 3.7.0)
34
+ rspec-mocks (~> 3.7.0)
35
+ rspec-core (3.7.1)
36
+ rspec-support (~> 3.7.0)
37
+ rspec-expectations (3.7.0)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.7.0)
40
+ rspec-mocks (3.7.0)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.7.0)
43
+ rspec-support (3.7.1)
44
+ thread_safe (0.3.6)
45
+ tzinfo (1.2.5)
46
+ thread_safe (~> 0.1)
47
+
48
+ PLATFORMS
49
+ ruby
50
+
51
+ DEPENDENCIES
52
+ bundler (~> 1.16)
53
+ byebug
54
+ mongoid-categorized_counter_cache!
55
+ rake (~> 12.3)
56
+ rspec (~> 3.0)
57
+
58
+ BUNDLED WITH
59
+ 1.16.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 FENG Zhichao
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,39 @@
1
+ # Mongoid::CategorizedCounterCache
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mongoid/categorized_counter_cache`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'mongoid-categorized_counter_cache'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install mongoid-categorized_counter_cache
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mongoid-categorized_counter_cache.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'mongoid/categorized_counter_cache'
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(__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,19 @@
1
+ version: '3'
2
+
3
+ services:
4
+ dev:
5
+ image: ruby:2.5.1
6
+ links:
7
+ - mongo
8
+ command: echo 'dev environment started'
9
+
10
+ mongo:
11
+ image: mongo:3.4.16
12
+ entrypoint: mongod --smallfiles
13
+ ports:
14
+ - "27117:27017"
15
+ volumes:
16
+ - mongodb:/data/db
17
+
18
+ volumes:
19
+ mongodb:
@@ -0,0 +1,83 @@
1
+ require 'mongoid/categorized_counter_cache/version'
2
+
3
+ module Mongoid
4
+ module CategorizedCounterCache
5
+
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+
12
+ def categorized_counter_cache relation_name
13
+ relation = self.relations[relation_name.to_s]
14
+ cache_column_base = relation.inverse_class_name.demodulize.underscore.pluralize
15
+
16
+ after_create do
17
+ category = yield self
18
+ cache_column = "#{cache_column_base}_#{category}_count"
19
+
20
+ if record = __send__(relation_name)
21
+ record[cache_column] = (record[cache_column] || 0) + 1
22
+
23
+ if record.persisted?
24
+ record.class.with(record.persistence_context) do |_class|
25
+ _class.increment_counter(cache_column, record._id)
26
+ end
27
+ record.remove_change(cache_column)
28
+ end
29
+ end
30
+ end
31
+
32
+ after_update do
33
+ foreign_key = relation.foreign_key
34
+ category = yield self
35
+ cache_column = "#{cache_column_base}_#{category}_count"
36
+
37
+ if record = __send__(relation_name)
38
+ if attribute_changed?(foreign_key)
39
+ original, current = attribute_change(foreign_key)
40
+
41
+ unless original.nil?
42
+ record.class.with(persistence_context) do |_class|
43
+ _class.decrement_counter(cache_column, original)
44
+ end
45
+ end
46
+
47
+ unless current.nil?
48
+ record[cache_column] = (record[cache_column] || 0) + 1
49
+ record.class.with(record.persistence_context) do |_class|
50
+ _class.increment_counter(cache_column, current) if record.persisted?
51
+ end
52
+ end
53
+ end
54
+ elsif attribute_changed?(foreign_key)
55
+ original, current = attribute_change(foreign_key)
56
+
57
+ unless original.nil?
58
+ relation.klass.with(persistence_context) do |_class|
59
+ _class.decrement_counter(cache_column, original)
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ before_destroy do
66
+ if record = __send__(relation_name)
67
+ category = yield self
68
+ cache_column = "#{cache_column_base}_#{category}_count"
69
+ record[cache_column] = (record[cache_column] || 0) - 1 unless record.frozen?
70
+
71
+ if record.persisted?
72
+ record.class.with(record.persistence_context) do |_class|
73
+ _class.decrement_counter(cache_column, record._id)
74
+ end
75
+ record.remove_change(cache_column)
76
+ end
77
+ end
78
+ end
79
+
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module CategorizedCounterCache
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'mongoid/categorized_counter_cache/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'mongoid-categorized_counter_cache'
7
+ spec.version = Mongoid::CategorizedCounterCache::VERSION
8
+ spec.authors = ['FENG Zhichao']
9
+ spec.email = ['flankerfc@gmail.com']
10
+
11
+ spec.summary = 'Mongoid Counter Cache extension: counter cache with categorized count'
12
+ spec.description = 'Mongoid Counter Cache extension: counter cache with categorized count'
13
+ spec.homepage = 'https://github.com/flanker/mongoid-categorized_counter_cache'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_runtime_dependency 'mongoid', '~> 6.2'
24
+ spec.add_runtime_dependency 'activemodel', '~> 5.1'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.16'
27
+ spec.add_development_dependency 'rake', '~> 12.3'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ spec.add_development_dependency 'byebug', '~> 0'
30
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-categorized_counter_cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - FENG Zhichao
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-10-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mongoid
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '6.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '6.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activemodel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '12.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '12.3'
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: byebug
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
+ description: 'Mongoid Counter Cache extension: counter cache with categorized count'
98
+ email:
99
+ - flankerfc@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - Gemfile.lock
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - docker-compose.yml
115
+ - lib/mongoid/categorized_counter_cache.rb
116
+ - lib/mongoid/categorized_counter_cache/version.rb
117
+ - mongoid-categorized_counter_cache.gemspec
118
+ homepage: https://github.com/flanker/mongoid-categorized_counter_cache
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.6.14
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: 'Mongoid Counter Cache extension: counter cache with categorized count'
142
+ test_files: []