document_hydrator 0.1.0 → 0.1.1
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/HISTORY.markdown +5 -0
- data/README.markdown +15 -0
- data/VERSION +1 -1
- data/document_hydrator.gemspec +3 -2
- data/lib/document_hydrator.rb +9 -5
- data/spec/document_hydrator_spec.rb +20 -1
- metadata +5 -4
data/HISTORY.markdown
ADDED
data/README.markdown
CHANGED
@@ -12,6 +12,21 @@ guaranteed to be called at most once during any given invocation of
|
|
12
12
|
DocumentHydrator, ensuring efficient hydration of multiple
|
13
13
|
subdocuments.
|
14
14
|
|
15
|
+
## Installation
|
16
|
+
Install the gem:
|
17
|
+
|
18
|
+
gem install document_hydrator
|
19
|
+
|
20
|
+
Require the library:
|
21
|
+
|
22
|
+
require 'document_hydrator'
|
23
|
+
|
24
|
+
If you want to use the optional MongoDB collection hydrator, make
|
25
|
+
sure you require the MongoDB driver first:
|
26
|
+
|
27
|
+
require 'mongo'
|
28
|
+
require 'document_hydrator'
|
29
|
+
|
15
30
|
## Hydration Procs
|
16
31
|
Hydration procs are responsible for transforming an array of document
|
17
32
|
references into a hash that maps those references to their
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/document_hydrator.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{document_hydrator}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Greg Spurrier"]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-10-03}
|
13
13
|
s.description = %q{DocumentHydrator takes a document, represented as a Ruby Hash, and efficiently updates it so that embedded references to other documents are replaced with their corresponding subdocuments.}
|
14
14
|
s.email = %q{greg.spurrier@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
|
|
20
20
|
".autotest",
|
21
21
|
".rspec",
|
22
22
|
"Gemfile",
|
23
|
+
"HISTORY.markdown",
|
23
24
|
"LICENSE.txt",
|
24
25
|
"README.markdown",
|
25
26
|
"Rakefile",
|
data/lib/document_hydrator.rb
CHANGED
@@ -64,11 +64,15 @@ module DocumentHydrator
|
|
64
64
|
step = step.sub(/_id(s?)$/, '')
|
65
65
|
step = Inflector.pluralize(step) if $1 == 's'
|
66
66
|
end
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
67
|
+
document[step] =
|
68
|
+
case subdocument
|
69
|
+
when Array
|
70
|
+
subdocument.map {|id| dehydrated_documents[id] }
|
71
|
+
when nil
|
72
|
+
nil
|
73
|
+
else
|
74
|
+
dehydrated_documents[subdocument]
|
75
|
+
end
|
72
76
|
else
|
73
77
|
# Keep on stepping
|
74
78
|
if subdocument.kind_of?(Array)
|
@@ -18,7 +18,7 @@ describe DocumentHydrator do
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
before(:each) do
|
23
23
|
Dummy.reset_invocation_count
|
24
24
|
@hydration_proc = lambda { |ids| Dummy.ids_to_document_hash(ids) }
|
@@ -51,6 +51,13 @@ describe DocumentHydrator do
|
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
|
+
context 'with a nil id' do
|
55
|
+
it 'leaves the id as nil' do
|
56
|
+
orig = { 'key1' => 37, 'user' => nil}
|
57
|
+
DocumentHydrator.hydrate_document(orig.dup, 'user', @hydration_proc).should == orig
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
54
61
|
context 'with a compound path to an array of ids' do
|
55
62
|
it 'replaces the array with an array of hydrated documents' do
|
56
63
|
orig = { 'key1' => 37, 'foo' => { 'users' => [27, 39] } }
|
@@ -123,6 +130,18 @@ describe DocumentHydrator do
|
|
123
130
|
}
|
124
131
|
DocumentHydrator.hydrate_document(orig.dup, 'user_id', @hydration_proc).should == expected
|
125
132
|
end
|
133
|
+
|
134
|
+
it "removes the '_id' suffix during hydration event when id is nil" do
|
135
|
+
orig = {
|
136
|
+
'key1' => 37,
|
137
|
+
'user_id' => nil
|
138
|
+
}
|
139
|
+
expected = {
|
140
|
+
'key1' => 37,
|
141
|
+
'user' => nil
|
142
|
+
}
|
143
|
+
DocumentHydrator.hydrate_document(orig.dup, 'user_id', @hydration_proc).should == expected
|
144
|
+
end
|
126
145
|
end
|
127
146
|
|
128
147
|
context "with a path whose terminal key ends with '_ids'" do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: document_hydrator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Greg Spurrier
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-10-03 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -179,6 +179,7 @@ files:
|
|
179
179
|
- .autotest
|
180
180
|
- .rspec
|
181
181
|
- Gemfile
|
182
|
+
- HISTORY.markdown
|
182
183
|
- LICENSE.txt
|
183
184
|
- README.markdown
|
184
185
|
- Rakefile
|