mongoid_embedded_helper 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +13 -0
- data/Gemfile +9 -0
- data/MITLICENSE +22 -0
- data/README.markdown +36 -0
- data/Rakefile +56 -0
- data/VERSION +1 -0
- data/autotest/discover.rb +1 -0
- data/lib/mongoid/embedded_helper.rb +36 -0
- data/lib/mongoid_embedded_helper.rb +1 -0
- data/spec/mongoid/embedded_helper_spec.rb +72 -0
- data/spec/rspec.options +1 -0
- data/spec/spec_helper.rb +10 -0
- metadata +122 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/MITLICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) <#{Time.new.year}> <#{ENV['USERNAME'] || ENV['USER']}>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Mongoid embedded helper #
|
2
|
+
|
3
|
+
The Mongoid EmbeddedHelper helps overcome the limitation that you can't perform direct queries on an embedded collection without accessing it from the root.
|
4
|
+
It simply introduces a generic function that performs the "local" query by accessing it from the nearest root ;)
|
5
|
+
|
6
|
+
## Installation ##
|
7
|
+
|
8
|
+
<code>gem install mongoid_embedded_helper</code>
|
9
|
+
|
10
|
+
## Usage ##
|
11
|
+
|
12
|
+
Mongoid doesn't allow you to perform direct queries on an embedded collection. You have to access the embedded collection from the root in order to do this.
|
13
|
+
To overcome this limitation, use the EmbeddedHelper module to add a method <code>query_class</code> which will find the closest non-embedded node in the hierarchy and
|
14
|
+
then traverse down the path back to the object, so that you can perform the query on the embedded collection.
|
15
|
+
|
16
|
+
<code>
|
17
|
+
require 'mongoid_embedded_helper'
|
18
|
+
|
19
|
+
class Item
|
20
|
+
include Mongoid::Document
|
21
|
+
include Mongoid::EmbeddedHelper
|
22
|
+
field :pos, :type => Integer
|
23
|
+
field :number, :type => Integer
|
24
|
+
|
25
|
+
field :assoc, :type => Symbol
|
26
|
+
embedded_in :list, :inverse_of => :items
|
27
|
+
end
|
28
|
+
|
29
|
+
item = @person.lists[0].items[0]
|
30
|
+
item.query_class.where(:number.gt => 1).to_a
|
31
|
+
|
32
|
+
</code>
|
33
|
+
|
34
|
+
## Copyright ##
|
35
|
+
|
36
|
+
Copyright (c) <2010> <Kristian Mandrup>
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "mongoid_embedded_helper"
|
8
|
+
gem.summary = %Q{Facilitates performing queries on collections in embedded Mongoid documents}
|
9
|
+
gem.description = %Q{Facilitates performing queries on collections in embedded Mongoid documents by performing query from the root node}
|
10
|
+
gem.email = "kmandrup@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/kristianmandrup/mongoid_embedded_helper"
|
12
|
+
gem.authors = ["Kristian Mandrup"]
|
13
|
+
gem.add_dependency("mongoid", "<= 2.0.0")
|
14
|
+
gem.add_dependency("bson", ">= 1.0.3")
|
15
|
+
|
16
|
+
gem.add_development_dependency "rspec", ">=2.0.0.beta.12"
|
17
|
+
end
|
18
|
+
Jeweler::GemcutterTasks.new
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
21
|
+
end
|
22
|
+
#
|
23
|
+
# require 'rake/testtask'
|
24
|
+
# Rake::TestTask.new(:test) do |test|
|
25
|
+
# test.libs << 'lib' << 'test'
|
26
|
+
# test.pattern = 'test/**/test_*.rb'
|
27
|
+
# test.verbose = true
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# begin
|
31
|
+
# require 'rcov/rcovtask'
|
32
|
+
# Rcov::RcovTask.new do |test|
|
33
|
+
# test.libs << 'test'
|
34
|
+
# test.pattern = 'test/**/test_*.rb'
|
35
|
+
# test.verbose = true
|
36
|
+
# end
|
37
|
+
# rescue LoadError
|
38
|
+
# task :rcov do
|
39
|
+
# abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
40
|
+
# end
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
# task :test => :check_dependencies
|
44
|
+
#
|
45
|
+
# task :default => :test
|
46
|
+
#
|
47
|
+
# require 'rake/rdoctask'
|
48
|
+
# Rake::RDocTask.new do |rdoc|
|
49
|
+
# version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
50
|
+
#
|
51
|
+
# rdoc.rdoc_dir = 'rdoc'
|
52
|
+
# rdoc.title = "mongoid_acts_as_tree #{version}"
|
53
|
+
# rdoc.rdoc_files.include('README*')
|
54
|
+
# rdoc.rdoc_files.include('lib/**/*.rb')
|
55
|
+
# end
|
56
|
+
#
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1 @@
|
|
1
|
+
Autotest.add_discovery { "rspec2" }
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
|
3
|
+
module Mongoid
|
4
|
+
module EmbeddedHelper
|
5
|
+
def in_collection stack = []
|
6
|
+
stack.extend(ArrayExt) if stack.empty?
|
7
|
+
if embedded?
|
8
|
+
stack.add_collection_name self
|
9
|
+
_parent.in_collection(stack)
|
10
|
+
else
|
11
|
+
return self.class if stack.empty?
|
12
|
+
iterate_collection_stack stack
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def iterate_collection_stack stack
|
19
|
+
collection = self
|
20
|
+
stack = stack.reverse
|
21
|
+
stack.each do |entry|
|
22
|
+
sub_collection = collection.send entry[:collection_name]
|
23
|
+
index = sub_collection.to_a.index(entry[:object]) if entry != stack.last
|
24
|
+
collection = sub_collection[index] if index
|
25
|
+
collection = sub_collection if !index
|
26
|
+
end
|
27
|
+
collection
|
28
|
+
end
|
29
|
+
|
30
|
+
module ArrayExt
|
31
|
+
def add_collection_name obj
|
32
|
+
self << {:collection_name => obj.collection_name, :object => obj}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'mongoid/embedded_helper'
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
Mongoid.configure.master = Mongo::Connection.new.db('acts_as_list-test')
|
4
|
+
|
5
|
+
class Person
|
6
|
+
include Mongoid::Document
|
7
|
+
include Mongoid::EmbeddedHelper
|
8
|
+
field :name, :type => String
|
9
|
+
embeds_many :lists
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
class List
|
14
|
+
include Mongoid::Document
|
15
|
+
include Mongoid::EmbeddedHelper
|
16
|
+
field :pos, :type => Integer
|
17
|
+
field :name, :type => String
|
18
|
+
embedded_in :person, :inverse_of => :lists
|
19
|
+
|
20
|
+
embeds_many :items
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
class Item
|
25
|
+
include Mongoid::Document
|
26
|
+
include Mongoid::EmbeddedHelper
|
27
|
+
field :pos, :type => Integer
|
28
|
+
field :number, :type => Integer
|
29
|
+
|
30
|
+
field :assoc, :type => Symbol
|
31
|
+
embedded_in :list, :inverse_of => :items
|
32
|
+
end
|
33
|
+
|
34
|
+
describe 'Mongoid Embedded Helper' do
|
35
|
+
|
36
|
+
before :each do
|
37
|
+
@person = Person.create! :name => 'Kristian'
|
38
|
+
# person.lists = []
|
39
|
+
list = List.new :pos => 1, :name => 'My list'
|
40
|
+
(1..3).each do |counter|
|
41
|
+
item = Item.new :pos => counter, :number => counter
|
42
|
+
list.items << item
|
43
|
+
end
|
44
|
+
@person.lists << list
|
45
|
+
@person.save!
|
46
|
+
end
|
47
|
+
|
48
|
+
after :each do
|
49
|
+
Mongoid.database.collections.each do |coll|
|
50
|
+
coll.remove
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe '#top level query' do
|
55
|
+
|
56
|
+
it "should handle query from top level node" do
|
57
|
+
result = @person.in_collection.where(:name => 'Kristian').to_a.first
|
58
|
+
result.name.should == 'Kristian'
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should handle query from mid level node" do
|
62
|
+
result = @person.lists[0].in_collection.where(:name => 'My list').to_a.first
|
63
|
+
result.name.should == 'My list'
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should handle query from mid level node" do
|
67
|
+
result = @person.lists[0].items[0].in_collection.where(:number.gt => 1).to_a
|
68
|
+
result.size.should == 2
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
data/spec/rspec.options
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--format nested --color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_embedded_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Kristian Mandrup
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-07-05 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: mongoid
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - <=
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 0
|
31
|
+
- 0
|
32
|
+
version: 2.0.0
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: bson
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
segments:
|
44
|
+
- 1
|
45
|
+
- 0
|
46
|
+
- 3
|
47
|
+
version: 1.0.3
|
48
|
+
type: :runtime
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: rspec
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 2
|
60
|
+
- 0
|
61
|
+
- 0
|
62
|
+
- beta
|
63
|
+
- 12
|
64
|
+
version: 2.0.0.beta.12
|
65
|
+
type: :development
|
66
|
+
version_requirements: *id003
|
67
|
+
description: Facilitates performing queries on collections in embedded Mongoid documents by performing query from the root node
|
68
|
+
email: kmandrup@gmail.com
|
69
|
+
executables: []
|
70
|
+
|
71
|
+
extensions: []
|
72
|
+
|
73
|
+
extra_rdoc_files:
|
74
|
+
- README.markdown
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- MITLICENSE
|
79
|
+
- README.markdown
|
80
|
+
- Rakefile
|
81
|
+
- VERSION
|
82
|
+
- autotest/discover.rb
|
83
|
+
- lib/mongoid/embedded_helper.rb
|
84
|
+
- lib/mongoid_embedded_helper.rb
|
85
|
+
- spec/mongoid/embedded_helper_spec.rb
|
86
|
+
- spec/rspec.options
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
has_rdoc: true
|
89
|
+
homepage: http://github.com/kristianmandrup/mongoid_embedded_helper
|
90
|
+
licenses: []
|
91
|
+
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options:
|
94
|
+
- --charset=UTF-8
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
version: "0"
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
segments:
|
111
|
+
- 0
|
112
|
+
version: "0"
|
113
|
+
requirements: []
|
114
|
+
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 1.3.7
|
117
|
+
signing_key:
|
118
|
+
specification_version: 3
|
119
|
+
summary: Facilitates performing queries on collections in embedded Mongoid documents
|
120
|
+
test_files:
|
121
|
+
- spec/mongoid/embedded_helper_spec.rb
|
122
|
+
- spec/spec_helper.rb
|