mongoid_embed_finder 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/lib/mongoid_embed_finder/nested_query.rb +23 -0
- data/lib/mongoid_embed_finder/projectors/base.rb +7 -2
- data/lib/mongoid_embed_finder/runner.rb +13 -17
- data/lib/mongoid_embed_finder/version.rb +1 -1
- data/spec/mongoid_embed_finder/projectors/base_spec.rb +7 -2
- data/spec/mongoid_embed_finder/projectors/single_spec.rb +28 -4
- data/spec/mongoid_embed_finder/relation_discovery_spec.rb +6 -6
- metadata +3 -4
- data/spec/mongoid_embed_finder/projector_spec.rb +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60b96e46d3a830bd20085827c827774bc342fb0f
|
4
|
+
data.tar.gz: 2d9232aeb1e78e8d4710369f08ebe2acd5937ad8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2bd177968c36476571030f5fd90e35389e9000957c957266962adfe4b6b876c83bb2ebcd991bc84ff681d976906ac2e025525e0ac370db29d9ebea700a1945b3
|
7
|
+
data.tar.gz: 52269e3d0f7a87c1541e7e9c47ae3ce07893e58eb16978b97a188a2b81eba92b697d25d22decc6e15d6bb17a0159628b612b81920bac319d403a378c5e51e066
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module MongoidEmbedFinder
|
2
|
+
class NestedQuery
|
3
|
+
attr_reader :parent_criteria, :child_criteria
|
4
|
+
|
5
|
+
def initialize(parent_criteria, child_criteria)
|
6
|
+
@parent_criteria = parent_criteria
|
7
|
+
@child_criteria = child_criteria
|
8
|
+
end
|
9
|
+
|
10
|
+
def scope_parent(conditions = {})
|
11
|
+
@parent_criteria = @parent_criteria.where(conditions)
|
12
|
+
end
|
13
|
+
|
14
|
+
def scope_child(conditions = {})
|
15
|
+
@child_criteria = @child_criteria.where(conditions)
|
16
|
+
end
|
17
|
+
|
18
|
+
def execute
|
19
|
+
parent_criteria.collection
|
20
|
+
.find(parent_criteria.selector)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -1,8 +1,13 @@
|
|
1
1
|
module MongoidEmbedFinder
|
2
2
|
module Projectors
|
3
|
-
class Base < Struct.new(:
|
3
|
+
class Base < Struct.new(:query, :relation)
|
4
4
|
def projection
|
5
|
-
{ relation.key => { operator =>
|
5
|
+
{ relation.key => { operator => query.child_criteria.selector }}
|
6
|
+
end
|
7
|
+
|
8
|
+
def project
|
9
|
+
query.scope_parent(projection)
|
10
|
+
query.execute.select(projection)
|
6
11
|
end
|
7
12
|
|
8
13
|
def operator
|
@@ -1,6 +1,6 @@
|
|
1
|
-
require "mongoid_embed_finder/
|
2
|
-
require "mongoid_embed_finder/projectors/single"
|
1
|
+
require "mongoid_embed_finder/nested_query"
|
3
2
|
require "mongoid_embed_finder/nested_builder"
|
3
|
+
require "mongoid_embed_finder/projectors/single"
|
4
4
|
require "mongoid_embed_finder/relation_discovery"
|
5
5
|
|
6
6
|
module MongoidEmbedFinder
|
@@ -19,26 +19,22 @@ module MongoidEmbedFinder
|
|
19
19
|
private
|
20
20
|
|
21
21
|
def find_first(attrs = {}, parent: {})
|
22
|
-
|
23
|
-
|
24
|
-
execute_query.first
|
22
|
+
query = build_nested_query(attrs, parent: parent)
|
23
|
+
project_query(query, Projectors::Single).first
|
25
24
|
end
|
26
25
|
|
27
|
-
def
|
28
|
-
|
29
|
-
|
30
|
-
end
|
26
|
+
def build_nested_query(attrs = {}, parent: {})
|
27
|
+
parent_criteria = relations.parent_class.criteria
|
28
|
+
child_criteria = relations.child_class.criteria
|
31
29
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
30
|
+
NestedQuery.new(parent_criteria, child_criteria).tap do |query|
|
31
|
+
query.scope_parent(parent)
|
32
|
+
query.scope_child(attrs)
|
33
|
+
end
|
36
34
|
end
|
37
35
|
|
38
|
-
def
|
39
|
-
relations.
|
40
|
-
.find(@parent_criteria.selector)
|
41
|
-
.select(@projector.projection)
|
36
|
+
def project_query(query, projector_class)
|
37
|
+
projector_class.new(query, relations.children).project
|
42
38
|
end
|
43
39
|
|
44
40
|
def build_child_with_parent(nested_attrs)
|
@@ -3,10 +3,15 @@ require "mongoid_embed_finder/projectors/base"
|
|
3
3
|
|
4
4
|
describe MongoidEmbedFinder::Projectors::Base do
|
5
5
|
describe "#projection" do
|
6
|
+
let(:query) do
|
7
|
+
double(
|
8
|
+
scope_parent: nil,
|
9
|
+
execute: double(select: []),
|
10
|
+
child_criteria: double(selector: {}))
|
11
|
+
end
|
6
12
|
let(:relation) { double(key: "children") }
|
7
|
-
let(:criteria) { double(selector: { "name" => "Child Name 0" }) }
|
8
13
|
|
9
|
-
subject { described_class.new(
|
14
|
+
subject { described_class.new(query, relation) }
|
10
15
|
|
11
16
|
it "relies on methods to be implemented" do
|
12
17
|
expect { subject.projection }.to raise_error(NotImplementedError)
|
@@ -2,12 +2,36 @@ require "spec_helper"
|
|
2
2
|
require "mongoid_embed_finder/projectors/single"
|
3
3
|
|
4
4
|
describe MongoidEmbedFinder::Projectors::Single do
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
let(:relation) { double(key: "children") }
|
6
|
+
let(:criteria) { double(selector: { "name" => "Child Name 0" }) }
|
7
|
+
let(:projection_result) { [] }
|
8
|
+
let(:query) do
|
9
|
+
double(
|
10
|
+
scope_parent: nil,
|
11
|
+
execute: double(select: projection_result),
|
12
|
+
child_criteria: criteria)
|
13
|
+
end
|
8
14
|
|
9
|
-
|
15
|
+
subject { described_class.new(query, relation) }
|
10
16
|
|
17
|
+
describe "#projection" do
|
11
18
|
its(:projection) { should eq(relation.key => { "$elemMatch" => criteria.selector }) }
|
12
19
|
end
|
20
|
+
|
21
|
+
describe "#project" do
|
22
|
+
it "extend parent's criteria" do
|
23
|
+
subject.project
|
24
|
+
expect(query).to have_received(:scope_parent).with(subject.projection)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "projects query" do
|
28
|
+
subject.project
|
29
|
+
expect(query).to have_received(:execute)
|
30
|
+
expect(query.execute).to have_received(:select).with(subject.projection)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "returns projection result" do
|
34
|
+
expect(subject.project).to eq projection_result
|
35
|
+
end
|
36
|
+
end
|
13
37
|
end
|
@@ -3,14 +3,14 @@ require "mongoid_embed_finder/relation_discovery"
|
|
3
3
|
|
4
4
|
describe MongoidEmbedFinder::RelationDiscovery do
|
5
5
|
describe "#relations" do
|
6
|
-
subject { described_class.new(
|
6
|
+
subject { described_class.new(Door, :car).relations }
|
7
7
|
|
8
|
-
its(:child_class) { should eq
|
9
|
-
its(:parent_class) { should eq
|
8
|
+
its(:child_class) { should eq Door }
|
9
|
+
its(:parent_class) { should eq Car }
|
10
10
|
|
11
|
-
its('children.key') { should eq "
|
12
|
-
its('children.class_name') { should eq "
|
11
|
+
its('children.key') { should eq "doors" }
|
12
|
+
its('children.class_name') { should eq "Door" }
|
13
13
|
|
14
|
-
its('parent.setter') { should eq "
|
14
|
+
its('parent.setter') { should eq "car=" }
|
15
15
|
end
|
16
16
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_embed_finder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Artur Hebda
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
@@ -112,6 +112,7 @@ files:
|
|
112
112
|
- Rakefile
|
113
113
|
- lib/mongoid_embed_finder.rb
|
114
114
|
- lib/mongoid_embed_finder/nested_builder.rb
|
115
|
+
- lib/mongoid_embed_finder/nested_query.rb
|
115
116
|
- lib/mongoid_embed_finder/projectors/base.rb
|
116
117
|
- lib/mongoid_embed_finder/projectors/single.rb
|
117
118
|
- lib/mongoid_embed_finder/relation_discovery.rb
|
@@ -119,7 +120,6 @@ files:
|
|
119
120
|
- lib/mongoid_embed_finder/version.rb
|
120
121
|
- mongoid_embed_finder.gemspec
|
121
122
|
- spec/mongoid_embed_finder/nested_builder_spec.rb
|
122
|
-
- spec/mongoid_embed_finder/projector_spec.rb
|
123
123
|
- spec/mongoid_embed_finder/projectors/base_spec.rb
|
124
124
|
- spec/mongoid_embed_finder/projectors/single_spec.rb
|
125
125
|
- spec/mongoid_embed_finder/relation_discovery_spec.rb
|
@@ -151,7 +151,6 @@ specification_version: 4
|
|
151
151
|
summary: Find mongoid embedded documents easily.
|
152
152
|
test_files:
|
153
153
|
- spec/mongoid_embed_finder/nested_builder_spec.rb
|
154
|
-
- spec/mongoid_embed_finder/projector_spec.rb
|
155
154
|
- spec/mongoid_embed_finder/projectors/base_spec.rb
|
156
155
|
- spec/mongoid_embed_finder/projectors/single_spec.rb
|
157
156
|
- spec/mongoid_embed_finder/relation_discovery_spec.rb
|
@@ -1,15 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
require "mongoid_embed_finder/projector"
|
3
|
-
|
4
|
-
describe MongoidEmbedFinder::Projector do
|
5
|
-
describe "#to_projection" do
|
6
|
-
let(:relation) { double(key: "children") }
|
7
|
-
let(:criteria) { double(selector: { "name" => "Child Name 0" }) }
|
8
|
-
|
9
|
-
subject { described_class.new(relation, criteria) }
|
10
|
-
|
11
|
-
it "needs to be implemented in derived classes" do
|
12
|
-
expect { subject.to_projection }.to raise_error(NotImplementedError)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|