mongoid-collection-separated 0.1.1

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: 2db1c0e56ef0f51a62a9cae1301527cc601207cdc3d29c88d0b0b62ee4434056
4
+ data.tar.gz: d483cfedb5024cfd7b1773309878fffb11273e17951f7a7a70f15eb3aed58e3e
5
+ SHA512:
6
+ metadata.gz: c2f90b4d09bf56814bf34c8918f943a219b84995daf2a4b21fab506d34850ee050c44f25caae72dd444d2d42290840e08bd11dfde16ec011021ebfa9de87dff7
7
+ data.tar.gz: e848e4bfc8cf1858e17b1bf3b4d157a92fd090a8c7e0f4cf8a88ee57c58295769855b32d630b88d4e4b1e2c253b3d189d0abcc9e0cbdcffc183fa971c4974460
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .idea
13
+ tags
14
+ .byebug_history
15
+ .DS_Store
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_collection_separated.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,66 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mongoid-collection-separated (0.1.1)
5
+ mongoid (~> 6.4.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.2.1)
11
+ activesupport (= 5.2.1)
12
+ activesupport (5.2.1)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 0.7, < 2)
15
+ minitest (~> 5.1)
16
+ tzinfo (~> 1.1)
17
+ bson (4.3.0)
18
+ byebug (10.0.0)
19
+ coderay (1.1.2)
20
+ concurrent-ruby (1.1.4)
21
+ diff-lcs (1.3)
22
+ i18n (1.1.1)
23
+ concurrent-ruby (~> 1.0)
24
+ method_source (0.9.0)
25
+ minitest (5.11.3)
26
+ mongo (2.6.2)
27
+ bson (>= 4.3.0, < 5.0.0)
28
+ mongoid (6.4.2)
29
+ activemodel (>= 5.1, < 6.0.0)
30
+ mongo (>= 2.5.1, < 3.0.0)
31
+ pry (0.11.3)
32
+ coderay (~> 1.1.0)
33
+ method_source (~> 0.9.0)
34
+ pry-byebug (3.6.0)
35
+ byebug (~> 10.0)
36
+ pry (~> 0.10)
37
+ rake (10.5.0)
38
+ rspec (3.8.0)
39
+ rspec-core (~> 3.8.0)
40
+ rspec-expectations (~> 3.8.0)
41
+ rspec-mocks (~> 3.8.0)
42
+ rspec-core (3.8.0)
43
+ rspec-support (~> 3.8.0)
44
+ rspec-expectations (3.8.2)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.8.0)
47
+ rspec-mocks (3.8.0)
48
+ diff-lcs (>= 1.2.0, < 2.0)
49
+ rspec-support (~> 3.8.0)
50
+ rspec-support (3.8.0)
51
+ thread_safe (0.3.6)
52
+ tzinfo (1.2.5)
53
+ thread_safe (~> 0.1)
54
+
55
+ PLATFORMS
56
+ ruby
57
+
58
+ DEPENDENCIES
59
+ bundler (~> 1.17)
60
+ mongoid-collection-separated!
61
+ pry-byebug (~> 3.4)
62
+ rake (~> 10.0)
63
+ rspec (~> 3.0)
64
+
65
+ BUNDLED WITH
66
+ 1.17.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Oscar Jiang
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,33 @@
1
+ # MongoidCollectionSeparatable
2
+
3
+ Support mongoid collections to be saved into and queried from separated collections with condition
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mongoid_collection_separated'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install mongoid_collection_separated
20
+
21
+ ## Usage
22
+ Add the following line into the model class that you want to split:
23
+ ```ruby
24
+ separated_by :form_id, :calc_collection_name
25
+ ```
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mongoid_collection_separatable.
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -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,2 @@
1
+ require "mongoid/collection_separated"
2
+
@@ -0,0 +1,44 @@
1
+ require 'mongoid'
2
+ require 'mongoid/collection_separated/monkey_patches'
3
+ require 'active_support'
4
+ require 'mongoid/collection_separated/version'
5
+
6
+
7
+ module Mongoid
8
+ module CollectionSeparated
9
+ extend ActiveSupport::Concern
10
+
11
+ included do
12
+ action_method_module = [Mongoid::Persistable.constants.map {|c| Mongoid::Persistable.const_get(c)} + [Mongoid::Reloadable]].flatten.select {|c| c.is_a?(Module)}
13
+ action_method_module.each {|action_module| alias_method_with_collection action_module}
14
+ end
15
+
16
+ class_methods do
17
+ attr_accessor :separated_field, :separated_parent_class, :separated_parent_field, :calc_collection_name_fun
18
+
19
+ def separated_by separated_field, calc_collection_name_fun, opts={}
20
+ @separated_parent_class = opts[:parent_class].constantize
21
+ @separated_parent_field = opts[:parent_field] || :id
22
+ @separated_field = separated_field
23
+ @calc_collection_name_fun = calc_collection_name_fun
24
+ end
25
+
26
+ def alias_method_with_collection action_module
27
+ action_module.instance_methods(false).each do |action_method|
28
+ define_method "#{action_method}_with_context" do |*args|
29
+ klass = self.class
30
+ self.send "#{action_method}_without_context", *args unless klass.respond_to?(:separated_field)
31
+
32
+ collection_name = klass.where(klass.separated_field => self.send(klass.separated_field)).ensured_collection.name
33
+ self.with(collection: collection_name) {self.send "#{action_method}_without_context", *args}
34
+ end
35
+
36
+ alias_method "#{action_method}_without_context".to_sym, action_method
37
+ alias_method action_method.to_sym, "#{action_method}_with_context".to_sym
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,130 @@
1
+ module Mongoid
2
+ module Contextual
3
+ private
4
+
5
+ # Changes:
6
+ # 1. Get form_id from selector
7
+ # 2. If collection is entries, not matter from context or current collection object, and form has entries_separated flag set, set collection name instance variable to entries plus form_id as new collection name
8
+
9
+ def create_context_with_separated_entries
10
+ context = create_context_without_separated_entries
11
+ query_class = instance_variable_get :@klass
12
+ new_collection_name = calc_new_collection_name query_class
13
+ unless new_collection_name.blank?
14
+ context.collection.instance_variable_set :@name, new_collection_name
15
+ collection.instance_variable_set :@name, new_collection_name
16
+ end
17
+ instance_variable_set :@context, context
18
+ context
19
+ end
20
+
21
+ def calc_new_collection_name query_class
22
+ return unless query_class.respond_to?(:separated_field) && query_class.send(:separated_field).present?
23
+ return unless query_class.respond_to?(:calc_collection_name_fun) || query_class.respond_to?(query_class.calc_collection_name_fun)
24
+ query_class.send(query_class.calc_collection_name_fun, (separated_value(query_class)))
25
+ end
26
+
27
+ def separated_value query_class
28
+ value = separated_selector(query_class)
29
+ value = separated_value_from_query_in_with_only_one_value(query_class) if value.is_a?(Hash)
30
+ value
31
+ end
32
+
33
+ def separated_value_from_query_in_with_only_one_value query_class
34
+ separated_selector(query_class)['$in']&.first
35
+ end
36
+
37
+ def separated_selector query_class
38
+ selector[query_class.separated_field.to_s]
39
+ end
40
+
41
+ alias_method :create_context_without_separated_entries, :create_context
42
+ alias_method :create_context, :create_context_with_separated_entries
43
+ end
44
+ end
45
+
46
+ module Mongoid
47
+ module Relations
48
+ module Referenced
49
+ class Many < Relations::Many
50
+ private
51
+
52
+ # Changes:
53
+ # 1. 'base' should be an instance of Form
54
+ # 2. If form has entries_separated flat and collection name is entries, clone a new context because it is build each time when called and set to context. Then remove form_id from selector because all the entries inside the new collection has the same form_id
55
+
56
+ def criteria_with_separated_entries
57
+ cri = criteria_without_separated_entries
58
+ query_class = cri.instance_variable_get :@klass
59
+ if should_query_from_separated_collection? query_class
60
+ context = cri.context.clone
61
+ new_collection_name = calc_new_collection_name query_class
62
+ unless new_collection_name.blank?
63
+ context.collection.instance_variable_set :@name, new_collection_name
64
+ cri.instance_variable_set :'@collection', @collection
65
+ end
66
+ end
67
+ cri
68
+ end
69
+
70
+ def should_query_from_separated_collection?(query_class)
71
+ query_class.respond_to?(:separated_field) && query_class.send(:separated_field) && base.is_a?(query_class.separated_parent_class) && base.respond_to?(query_class.separated_parent_field)
72
+ end
73
+
74
+ def calc_new_collection_name query_class
75
+ return unless query_class.respond_to?(:calc_collection_name_fun) or query_class.respond_to?(query_class.calc_collection_name_fun)
76
+ query_class.send(query_class.calc_collection_name_fun, base.send(query_class.separated_parent_field))
77
+ end
78
+
79
+ alias_method :criteria_without_separated_entries, :criteria
80
+ alias_method :criteria, :criteria_with_separated_entries
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ module Mongoid
87
+ # The +Criteria+ class is the core object needed in Mongoid to retrieve
88
+ # objects from the database. It is a DSL that essentially sets up the
89
+ # selector and options arguments that get passed on to a Mongo::Collection
90
+ # in the Ruby driver. Each method on the +Criteria+ returns self to they
91
+ # can be chained in order to create a readable criterion to be executed
92
+ # against the database.
93
+ class Criteria
94
+ def ensured_collection
95
+ context.collection
96
+ end
97
+ end
98
+ end
99
+
100
+ module Mongoid
101
+ module Clients
102
+ module Options
103
+ def collection_with_separated(parent = nil)
104
+ klass = self.class
105
+ origin_collection = collection_without_separated(parent)
106
+ return origin_collection unless self.class.respond_to?(:separated_field)
107
+ origin_collection.instance_variable_set :@name, separated_collection_name(klass)
108
+ origin_collection
109
+ end
110
+
111
+ alias_method :collection_without_separated, :collection
112
+ alias_method :collection, :collection_with_separated
113
+
114
+ def collection_name_with_separated
115
+ klass = self.class
116
+ origin_collection_name = collection_name_without_separated
117
+ return origin_collection_name unless self.class.respond_to?(:separated_field)
118
+ separated_collection_name klass
119
+ end
120
+
121
+ alias_method :collection_name_without_separated, :collection_name
122
+ alias_method :collection_name, :collection_name_with_separated
123
+
124
+ def separated_collection_name(klass)
125
+ klass.where(klass.separated_field => self.send(klass.separated_field)).ensured_collection.name
126
+ end
127
+ end
128
+ end
129
+ end
130
+
@@ -0,0 +1,6 @@
1
+ module Mongoid
2
+ module CollectionSeparated
3
+ VERSION = "0.1.1"
4
+ end
5
+ end
6
+
@@ -0,0 +1,2 @@
1
+ require "mongoid/collection_separated"
2
+
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "mongoid/collection_separated/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "mongoid-collection-separated"
7
+ spec.version = Mongoid::CollectionSeparated::VERSION
8
+ spec.authors = ["aibotyu"]
9
+ spec.email = ["284894567@qq.com"]
10
+
11
+ spec.summary = %q{Save the mongoid model into different collections by condition}
12
+ spec.description = %q{Mongoid models are saved in one collection by default. However, when collections after too large , it could be extracted into a separated one and query form that, to make it query faster}
13
+ spec.homepage = "https://github.com/WeiGangqiang/mongoid_collection_separated"
14
+ spec.license = "MIT"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency 'mongoid', ' ~> 6.4.0'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.17"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ spec.add_development_dependency "pry-byebug", "~> 3.4"
29
+
30
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-collection-separated
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - aibotyu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-03-04 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.4.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 6.4.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.4'
83
+ description: Mongoid models are saved in one collection by default. However, when
84
+ collections after too large , it could be extracted into a separated one and query
85
+ form that, to make it query faster
86
+ email:
87
+ - 284894567@qq.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - lib/mongoid-collection-separated.rb
99
+ - lib/mongoid/collection_separated.rb
100
+ - lib/mongoid/collection_separated/monkey_patches.rb
101
+ - lib/mongoid/collection_separated/version.rb
102
+ - lib/mongoid_collection_separated.rb
103
+ - mongoid_collection_separated.gemspec
104
+ homepage: https://github.com/WeiGangqiang/mongoid_collection_separated
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.7.8
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Save the mongoid model into different collections by condition
128
+ test_files: []