mongoid-nested-serialization 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env bash
2
+
3
+ if ! rvm list | grep -q ruby-1.9.3-p194 ; then
4
+ rvm install 1.9.3-p194
5
+ fi
6
+
7
+ rvm 1.9.3-p194@ryantownsend_mongoid_nested_serialization --create
8
+
9
+ if ! gem list | grep -q bundler ; then
10
+ gem install --no-ri --no-rdoc bundler
11
+ bundle install --without production
12
+ fi
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ services:
5
+ - mongodb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid_nested_serialization.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Ryan Townsend
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Procfile ADDED
@@ -0,0 +1 @@
1
+ mongo: mongod --config /usr/local/etc/mongod.conf
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # MongoidNestedSerialization
2
+
3
+ Loads nested Mongoid documents using JSON serialization.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "mongoid-nested-serialization"
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install mongoid-nested-serialization
20
+
21
+ ## Usage
22
+
23
+ The module will be automatically loaded into your `Mongoid::Document` objects:
24
+
25
+ ```ruby
26
+ class User
27
+ include Mongoid::Document
28
+ # also performs:
29
+ # include Mongoid::NestedSerialization
30
+
31
+ # objects may be embedded within others
32
+ embedded_in :account, inverse_of: :users
33
+ end
34
+
35
+ # find the root object
36
+ account = Account.first
37
+ # create a persisted object within it
38
+ user = account.users.create # => <User id:123>
39
+ # load the JSON needed to find the object
40
+ json = user.finder_json
41
+ # => { class_name: "Account", id: 456, embedded: { collection: "users", id: 123 } }
42
+ User.find_by_json(json)
43
+ # => <User id:123>
44
+ ```
45
+
46
+ ## Contributing
47
+
48
+ 1. Fork it
49
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
50
+ 3. Add your code and tests
51
+ 4. Run the full test suite (`bundle exec foreman start; bundle exec rake; bundle exec foreman stop`) and ensure it passes 100%
52
+ 5. Commit your changes (`git commit -am 'Added some feature'`)
53
+ 6. Push to the branch (`git push origin my-new-feature`)
54
+ 7. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require "rspec/core/rake_task"
5
+
6
+ desc "Runs all the specs"
7
+ task default: %w(spec)
8
+
9
+ desc "Run specs"
10
+ RSpec::Core::RakeTask.new do |t|
11
+ t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
12
+ # Put spec opts in a file named .rspec in root
13
+ end
@@ -0,0 +1,8 @@
1
+ module Mongoid
2
+ module Document
3
+ def self.included(base)
4
+ super
5
+ base.send(:include, Mongoid::NestedSerialization)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,41 @@
1
+ require "multi_json"
2
+
3
+ module Mongoid
4
+ module NestedSerialization
5
+ class Finder
6
+ def initialize(klass)
7
+ @klass = klass
8
+ end
9
+
10
+ # parse the raw JSON data into a hash
11
+ def parse_input(json)
12
+ MultiJson.load(json)
13
+ end
14
+
15
+ # load the top level object directly with the collection
16
+ def top_level_object(data)
17
+ data["class_name"].constantize.find(data["id"])
18
+ end
19
+
20
+ # load an object nested within another, using the data
21
+ def nested_object(object, data)
22
+ object.send(data["association"]).find(data["id"])
23
+ end
24
+
25
+ def find(json)
26
+ data = parse_input(json)
27
+ # load the top level object
28
+ object = top_level_object(data)
29
+ # if we have embedded stuff
30
+ while data["embedded"]
31
+ # work on the next level down
32
+ data = data["embedded"]
33
+ # find the nested object
34
+ object = nested_object(object, data)
35
+ end
36
+ # once at the bottom, return the object
37
+ object
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,45 @@
1
+ require "multi_json"
2
+
3
+ module Mongoid
4
+ module NestedSerialization
5
+ class Serializer
6
+ def initialize(object)
7
+ @object = object
8
+ end
9
+
10
+ # spits out a JSON hash which can be used to relocate
11
+ # the object in the collection tree
12
+ def to_json
13
+ # convert the result to JSON
14
+ MultiJson.dump(to_hash)
15
+ end
16
+
17
+ # spits out a hash which can be used to relocate
18
+ # the object in the collection tree
19
+ def to_hash(embedded_contents = nil)
20
+ if object.embedded?
21
+ # select the relation for the parent object
22
+ parent_relation = object.relations.select do |k,v|
23
+ v.macro == :embedded_in && v.class_name == object._parent.class.name
24
+ end.values.first
25
+ # embed this in the parent's hash
26
+ result = {
27
+ association: parent_relation.inverse_of,
28
+ id: object.id
29
+ }
30
+ result = result.merge(embedded: embedded_contents) if embedded_contents
31
+ self.class.new(object._parent).to_hash(result)
32
+ else # !embedded?
33
+ result = { class_name: object.class.name, id: object.id }
34
+ result[:embedded] = embedded_contents if embedded_contents
35
+ result
36
+ end
37
+ end
38
+
39
+ private
40
+ def object
41
+ @object
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module NestedSerialization
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ require "mongoid/nested_serialization/core_ext"
2
+ require "mongoid/nested_serialization/finder"
3
+ require "mongoid/nested_serialization/serializer"
4
+
5
+ module Mongoid
6
+ module NestedSerialization
7
+ def self.included(base)
8
+ base.extend ClassMethods
9
+ base.send :include, InstanceMethods
10
+ end
11
+
12
+ module ClassMethods
13
+ def find_by_json(json)
14
+ Mongoid::NestedSerialization::Finder.new(self).find(json)
15
+ end
16
+ end
17
+
18
+ module InstanceMethods
19
+ def finder_json
20
+ Mongoid::NestedSerialization::Serializer.new(self).to_json
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,2 @@
1
+ require "mongoid/nested_serialization/version"
2
+ require "mongoid/nested_serialization"
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/mongoid/nested_serialization/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.authors = ["Ryan Townsend"]
6
+ s.email = ["ryan@ryantownsend.co.uk"]
7
+ s.description = %q{Loads nested Mongoid documents using a JSON serialization}
8
+ s.summary = s.description
9
+ s.homepage = "https://github.com/ryantownsend/mongoid-nested-serialization"
10
+ s.license = "MIT"
11
+
12
+ s.files = `git ls-files`.split($\)
13
+ s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ s.test_files = s.files.grep(%r{^(spec|features)/})
15
+ s.name = "mongoid-nested-serialization"
16
+ s.require_paths = ["lib"]
17
+ s.version = Mongoid::NestedSerialization::VERSION
18
+
19
+ s.add_dependency "mongoid"
20
+ s.add_dependency "multi_json"
21
+ s.add_development_dependency "rake"
22
+ s.add_development_dependency "rspec"
23
+ s.add_development_dependency "simplecov"
24
+ s.add_development_dependency "foreman"
25
+ end
@@ -0,0 +1,25 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::NestedSerialization::Finder do
4
+ let(:account) { Mongoid::NestedSerialization::Test::Account.create }
5
+ let(:item) { account.items.create }
6
+ let(:photo) { item.photos.create }
7
+
8
+ describe "#find" do
9
+ it "should find top-level objects" do
10
+ result = account.class.find_by_json(account.finder_json)
11
+ expect(result).to eq account
12
+ end
13
+
14
+ it "should find second-level objects" do
15
+ result = item.class.find_by_json(item.finder_json)
16
+ expect(result).to eq item
17
+ end
18
+
19
+ it "should find third-level objects" do
20
+ result = photo.class.find_by_json(photo.finder_json)
21
+ expect(result).to eq photo
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,44 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::NestedSerialization::Serializer do
4
+ let(:serialized) do
5
+ Mongoid::NestedSerialization::Serializer.new(object).to_hash
6
+ end
7
+ let(:account) { Mongoid::NestedSerialization::Test::Account.create }
8
+ let(:item) { account.items.create }
9
+ let(:photo) { item.photos.create }
10
+
11
+ describe "#to_hash" do
12
+ context "given a top-level object" do
13
+ let(:object) { account }
14
+
15
+ it "should not have embedded data" do
16
+ expect(serialized.has_key?(:embedded)).to be_false
17
+ end
18
+ end
19
+
20
+ context "given a second-level object" do
21
+ let(:object) { item }
22
+
23
+ it "should have embedded data" do
24
+ expect(serialized.has_key?(:embedded)).to be_true
25
+ end
26
+
27
+ it "should not have double embedded data" do
28
+ expect(serialized[:embedded].has_key?(:embedded)).to be_false
29
+ end
30
+ end
31
+
32
+ context "given a third-level object" do
33
+ let(:object) { photo }
34
+
35
+ it "should have embedded data" do
36
+ expect(serialized.has_key?(:embedded)).to be_true
37
+ end
38
+
39
+ it "should have double embedded data" do
40
+ expect(serialized[:embedded].has_key?(:embedded)).to be_true
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,24 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::NestedSerialization do
4
+ describe "::included" do
5
+ let(:document) do
6
+ Class.new do
7
+ include Mongoid::Document
8
+ end
9
+ end
10
+
11
+ it "should be included in any Mongoid::Document models" do
12
+ expect(document.included_modules).to include Mongoid::NestedSerialization
13
+ end
14
+
15
+ it "should provide a ::find_by_json method" do
16
+ expect(document).to respond_to :find_by_json
17
+ end
18
+
19
+ it "should provide a #finder_json method" do
20
+ expect(document.new).to respond_to :finder_json
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,12 @@
1
+ require "simplecov"
2
+ SimpleCov.start do
3
+ add_filter "/spec/"
4
+ end
5
+
6
+ require "mongoid"
7
+ require "mongoid-nested-serialization"
8
+
9
+ lib = File.expand_path("../../lib", __FILE__)
10
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
11
+
12
+ Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each { |f| require f }
@@ -0,0 +1,8 @@
1
+ RSpec.configure do |config|
2
+ config.expect_with :rspec do |c|
3
+ c.syntax = :expect
4
+ end
5
+
6
+ config.color_enabled = true
7
+ config.order = :random
8
+ end
@@ -0,0 +1,27 @@
1
+ module Mongoid
2
+ module NestedSerialization
3
+ module Test
4
+ # top level object
5
+ class Account
6
+ include ::Mongoid::Document
7
+
8
+ embeds_many :items #, class_name: "Mongoid::NestedSerialization::Test::Item"
9
+ end
10
+
11
+ # second level object
12
+ class Item
13
+ include ::Mongoid::Document
14
+
15
+ embedded_in :account, inverse_of: :items
16
+ embeds_many :photos
17
+ end
18
+
19
+ # third level object
20
+ class Photo
21
+ include ::Mongoid::Document
22
+
23
+ embedded_in :item, inverse_of: :photos
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ require "mongoid"
2
+
3
+ Mongoid.load!(File.join(File.dirname(__FILE__), "mongoid.yml"), :test)
@@ -0,0 +1,6 @@
1
+ test:
2
+ sessions:
3
+ default:
4
+ hosts:
5
+ - localhost:27017
6
+ database: ryantownsend_mongoid_nested_serialization
metadata ADDED
@@ -0,0 +1,179 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-nested-serialization
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ryan Townsend
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: multi_json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: simplecov
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: foreman
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Loads nested Mongoid documents using a JSON serialization
111
+ email:
112
+ - ryan@ryantownsend.co.uk
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - .rvmrc
119
+ - .travis.yml
120
+ - Gemfile
121
+ - LICENSE
122
+ - Procfile
123
+ - README.md
124
+ - Rakefile
125
+ - lib/mongoid-nested-serialization.rb
126
+ - lib/mongoid/nested_serialization.rb
127
+ - lib/mongoid/nested_serialization/core_ext.rb
128
+ - lib/mongoid/nested_serialization/finder.rb
129
+ - lib/mongoid/nested_serialization/serializer.rb
130
+ - lib/mongoid/nested_serialization/version.rb
131
+ - mongoid_nested_serialization.gemspec
132
+ - spec/mongoid/nested_serialization/finder_spec.rb
133
+ - spec/mongoid/nested_serialization/serializer_spec.rb
134
+ - spec/mongoid/nested_serialization_spec.rb
135
+ - spec/spec_helper.rb
136
+ - spec/support/env.rb
137
+ - spec/support/models.rb
138
+ - spec/support/mongoid.rb
139
+ - spec/support/mongoid.yml
140
+ homepage: https://github.com/ryantownsend/mongoid-nested-serialization
141
+ licenses:
142
+ - MIT
143
+ post_install_message:
144
+ rdoc_options: []
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ none: false
149
+ requirements:
150
+ - - ! '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ segments:
154
+ - 0
155
+ hash: 1142570094106982720
156
+ required_rubygems_version: !ruby/object:Gem::Requirement
157
+ none: false
158
+ requirements:
159
+ - - ! '>='
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ segments:
163
+ - 0
164
+ hash: 1142570094106982720
165
+ requirements: []
166
+ rubyforge_project:
167
+ rubygems_version: 1.8.24
168
+ signing_key:
169
+ specification_version: 3
170
+ summary: Loads nested Mongoid documents using a JSON serialization
171
+ test_files:
172
+ - spec/mongoid/nested_serialization/finder_spec.rb
173
+ - spec/mongoid/nested_serialization/serializer_spec.rb
174
+ - spec/mongoid/nested_serialization_spec.rb
175
+ - spec/spec_helper.rb
176
+ - spec/support/env.rb
177
+ - spec/support/models.rb
178
+ - spec/support/mongoid.rb
179
+ - spec/support/mongoid.yml