mongo_persist 0.0.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/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +63 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/lib/mongo_persist.rb +119 -0
- data/lib/mongo_persist/util.rb +56 -0
- data/spec/mongo_persist_spec.rb +65 -0
- data/spec/spec_helper.rb +9 -0
- metadata +95 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Mike Harris
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
= mongo_persist
|
2
|
+
|
3
|
+
Library to add MongoDB Persistance to normal Ruby objects
|
4
|
+
|
5
|
+
= Example
|
6
|
+
|
7
|
+
class Order
|
8
|
+
include MongoPersist
|
9
|
+
attr_accessor :po_number
|
10
|
+
fattr(:order_products) { [] }
|
11
|
+
def products
|
12
|
+
order_products.map { |x| x.product }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class OrderProduct
|
17
|
+
include MongoPersist
|
18
|
+
attr_accessor :unit_price, :quantity, :product
|
19
|
+
mongo_reference_attributes ['product']
|
20
|
+
end
|
21
|
+
|
22
|
+
class Product
|
23
|
+
include MongoPersist
|
24
|
+
attr_accessor :name
|
25
|
+
end
|
26
|
+
|
27
|
+
products = [Product.new(:name => 'Leather Couch'),Product.new(:name => 'Maroon Chair')].each { |x| x.mongo_save! }
|
28
|
+
|
29
|
+
orders = []
|
30
|
+
orders << Order.new(:po_number => 1234, :order_products => [OrderProduct.new(:unit_price => 1000, :quantity => 1, :product => products[0])]).mongo_save!
|
31
|
+
orders << Order.new(:po_number => 1235, :order_products => [OrderProduct.new(:unit_price => 200, :quantity => 2, :product => products[1])]).mongo_save!
|
32
|
+
|
33
|
+
# objects are saved to MongoDB as JSON objects
|
34
|
+
|
35
|
+
# get all order objects back from Mongo
|
36
|
+
# you get back the ruby objects you put in, not raw JSON objects
|
37
|
+
Order.collection.find_objects
|
38
|
+
|
39
|
+
# Since on OrderProduct, the product attribute was marked as a reference attribute, the product is stored in MongoDB only as a reference to the product obj
|
40
|
+
# When you read the Order/OrderProduct back out, MongoPersist takes care of fetching the Product object again. You don't have to do anything.
|
41
|
+
Order.collection.find_one_object.products.first # An object of class Product
|
42
|
+
Order.collection.find_one_object.products.first.name # Leather Couch
|
43
|
+
|
44
|
+
# Because the product is stored as a reference, if you update that product elsewhere and save to Mongo, later reads of Orders with that product will be correctly updated
|
45
|
+
products[0].name = 'White Leather Couch'
|
46
|
+
products[0].mongo_save!
|
47
|
+
Order.collection.find_one_object.products.first.name # White Leather Couch
|
48
|
+
|
49
|
+
|
50
|
+
== Note on Patches/Pull Requests
|
51
|
+
|
52
|
+
* Fork the project.
|
53
|
+
* Make your feature addition or bug fix.
|
54
|
+
* Add tests for it. This is important so I don't break it in a
|
55
|
+
future version unintentionally.
|
56
|
+
* Commit, do not mess with rakefile, version, or history.
|
57
|
+
(if you want to have your own version, that is fine but
|
58
|
+
bump version in a commit by itself I can ignore when I pull)
|
59
|
+
* Send me a pull request. Bonus points for topic branches.
|
60
|
+
|
61
|
+
== Copyright
|
62
|
+
|
63
|
+
Copyright (c) 2009 Mike Harris. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "mongo_persist"
|
8
|
+
gem.summary = %Q{Library to add MongoDB Persistance to normal Ruby objects}
|
9
|
+
gem.description = %Q{Library to add MongoDB Persistance to normal Ruby objects}
|
10
|
+
gem.email = "mharris717@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/mharris717/mongo_persist"
|
12
|
+
gem.authors = ["Mike Harris"]
|
13
|
+
gem.add_development_dependency "rspec"
|
14
|
+
gem.add_dependency 'fattr'
|
15
|
+
gem.add_dependency 'activesupport'
|
16
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
17
|
+
end
|
18
|
+
Jeweler::GemcutterTasks.new
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'spec/rake/spectask'
|
24
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
25
|
+
spec.libs << 'lib' << 'spec'
|
26
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
27
|
+
end
|
28
|
+
|
29
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
30
|
+
spec.libs << 'lib' << 'spec'
|
31
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
32
|
+
spec.rcov = true
|
33
|
+
end
|
34
|
+
|
35
|
+
task :spec => :check_dependencies
|
36
|
+
|
37
|
+
task :default => :spec
|
38
|
+
|
39
|
+
require 'rake/rdoctask'
|
40
|
+
Rake::RDocTask.new do |rdoc|
|
41
|
+
if File.exist?('VERSION')
|
42
|
+
version = File.read('VERSION')
|
43
|
+
else
|
44
|
+
version = ""
|
45
|
+
end
|
46
|
+
|
47
|
+
rdoc.rdoc_dir = 'rdoc'
|
48
|
+
rdoc.title = "mongo_persist #{version}"
|
49
|
+
rdoc.rdoc_files.include('README*')
|
50
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
51
|
+
end
|
52
|
+
|
53
|
+
Jeweler::GemcutterTasks.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,119 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'mongo'
|
3
|
+
require 'activesupport'
|
4
|
+
require 'fattr'
|
5
|
+
require File.dirname(__FILE__) + "/mongo_persist/util"
|
6
|
+
|
7
|
+
class Object
|
8
|
+
def safe_to_mongo_hash
|
9
|
+
sos(:to_mongo_hash)
|
10
|
+
end
|
11
|
+
def safe_to_mongo_object
|
12
|
+
sos(:to_mongo_object)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Array
|
17
|
+
def to_mongo_hash
|
18
|
+
map { |x| x.safe_to_mongo_hash }
|
19
|
+
end
|
20
|
+
def to_mongo_object
|
21
|
+
map { |x| x.safe_to_mongo_object }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Hash
|
26
|
+
def get_mongo_id
|
27
|
+
['_id','mongo_id'].each { |x| return self[x] if self[x] }
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
def naked_reference?
|
31
|
+
(keys - ['_id','_mongo_class','mongo_id']).empty? && !!get_mongo_id
|
32
|
+
end
|
33
|
+
def mongo_class
|
34
|
+
self['_mongo_class'] ? eval(self['_mongo_class']) : nil
|
35
|
+
end
|
36
|
+
def to_mongo_hash_for_obj
|
37
|
+
h = reject { |k,v| k == '_mongo_class' }
|
38
|
+
h['mongo_id'] = h['_id'] if h['_id']
|
39
|
+
h.reject { |k,v| k == '_id' }
|
40
|
+
end
|
41
|
+
def to_mongo_object
|
42
|
+
return self unless mongo_class
|
43
|
+
if naked_reference?
|
44
|
+
mongo_class.collection.find_one_object('_id' => get_mongo_id)
|
45
|
+
else
|
46
|
+
mongo_class.from_mongo_hash(to_mongo_hash_for_obj)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
module MongoPersist
|
52
|
+
attr_accessor :mongo_id
|
53
|
+
#can be overriden by class. If not, assumes that all instance variables should be saved.
|
54
|
+
def mongo_attributes
|
55
|
+
instance_variables.map { |x| x[1..-1] }
|
56
|
+
end
|
57
|
+
def mongo_child_attributes
|
58
|
+
mongo_attributes - self.class.mongo_reference_attributes
|
59
|
+
end
|
60
|
+
def to_mongo_hash
|
61
|
+
res = mongo_child_attributes.inject({}) { |h,attr| h.merge(attr => send(attr).safe_to_mongo_hash) }.merge("_mongo_class" => self.class.to_s)
|
62
|
+
klass.mongo_reference_attributes.each do |attr|
|
63
|
+
obj = send(attr)
|
64
|
+
res[attr] = {'_mongo_class' => obj.class.to_s, '_id' => send(attr).mongo_id}
|
65
|
+
end
|
66
|
+
res
|
67
|
+
end
|
68
|
+
def from_mongo_hash(h)
|
69
|
+
h = h.map_value { |v| v.safe_to_mongo_object }
|
70
|
+
from_hash(h)
|
71
|
+
end
|
72
|
+
def mongo_save!
|
73
|
+
if mongo_id
|
74
|
+
klass.collection.update({'_id' => mongo_id},to_mongo_hash)
|
75
|
+
else
|
76
|
+
self.mongo_id = self.class.collection.save(to_mongo_hash)
|
77
|
+
end
|
78
|
+
self
|
79
|
+
end
|
80
|
+
|
81
|
+
module ClassMethods
|
82
|
+
dsl_method(:mongo_reference_attributes) { [] }
|
83
|
+
def default_collection_name
|
84
|
+
to_s.downcase.pluralize
|
85
|
+
end
|
86
|
+
fattr(:collection) { db.collection(default_collection_name) }
|
87
|
+
def from_mongo_hash(h)
|
88
|
+
new.tap { |x| x.from_mongo_hash(h) }
|
89
|
+
end
|
90
|
+
def mongo_connection(ops)
|
91
|
+
ops.each { |k,v| send("#{k}=",v) }
|
92
|
+
end
|
93
|
+
end
|
94
|
+
def self.included(mod)
|
95
|
+
super(mod)
|
96
|
+
mod.send(:include,FromHash)
|
97
|
+
mod.send(:extend,ClassMethods)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
class ObjCursor
|
102
|
+
attr_accessor :cursor
|
103
|
+
include FromHash
|
104
|
+
def each
|
105
|
+
cursor.each { |x| yield(x.to_mongo_object) }
|
106
|
+
end
|
107
|
+
def count
|
108
|
+
cursor.count
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
class Mongo::Collection
|
113
|
+
def find_objects(*args)
|
114
|
+
ObjCursor.new(:cursor => find(*args))
|
115
|
+
end
|
116
|
+
def find_one_object(*args)
|
117
|
+
find_one(*args).to_mongo_object
|
118
|
+
end
|
119
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
class Object
|
2
|
+
def dsl_method(name,&b)
|
3
|
+
define_method(name) do |*args|
|
4
|
+
if args.empty?
|
5
|
+
res = instance_variable_get("@#{name}")
|
6
|
+
if res.nil? && block_given?
|
7
|
+
res = b.call
|
8
|
+
instance_variable_set("@#{name}",res)
|
9
|
+
end
|
10
|
+
res
|
11
|
+
else
|
12
|
+
instance_variable_set("@#{name}",args.first)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
def dsl_class_method(name,&b)
|
17
|
+
self.class.dsl_method(name,&b)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class OrderedHash
|
22
|
+
def reject(&b)
|
23
|
+
res = OrderedHash.new
|
24
|
+
each { |k,v| res[k] = v unless yield(k,v) }
|
25
|
+
res
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class Object
|
30
|
+
def sos(m)
|
31
|
+
respond_to?(m) ? send(m) : self
|
32
|
+
end
|
33
|
+
def klass
|
34
|
+
self.class
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
module FromHash
|
39
|
+
def from_hash(ops)
|
40
|
+
ops.each do |k,v|
|
41
|
+
send("#{k}=",v)
|
42
|
+
end
|
43
|
+
self
|
44
|
+
end
|
45
|
+
def initialize(ops={})
|
46
|
+
from_hash(ops)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class Hash
|
51
|
+
def map_value
|
52
|
+
res = {}
|
53
|
+
each { |k,v| res[k] = yield(v) }
|
54
|
+
res
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
def db
|
4
|
+
Mongo::Connection.new.db('test-db')
|
5
|
+
end
|
6
|
+
|
7
|
+
class Order
|
8
|
+
include MongoPersist
|
9
|
+
attr_accessor :po_number
|
10
|
+
fattr(:order_products) { [] }
|
11
|
+
def products
|
12
|
+
order_products.map { |x| x.product }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class OrderProduct
|
17
|
+
include MongoPersist
|
18
|
+
attr_accessor :unit_price, :quantity, :product
|
19
|
+
mongo_reference_attributes ['product']
|
20
|
+
end
|
21
|
+
|
22
|
+
class Product
|
23
|
+
include MongoPersist
|
24
|
+
attr_accessor :name
|
25
|
+
end
|
26
|
+
|
27
|
+
# too many assertions per test, too many "this is how things should be before i check the point of this test" assertions
|
28
|
+
describe MongoPersist do
|
29
|
+
before do
|
30
|
+
[Order,Product].each { |x| x.collection.remove }
|
31
|
+
@products = [Product.new(:name => 'Leather Couch'),Product.new(:name => 'Maroon Chair')].each { |x| x.mongo_save! }
|
32
|
+
|
33
|
+
@orders = []
|
34
|
+
@orders << Order.new(:po_number => 1234, :order_products => [OrderProduct.new(:unit_price => 1000, :quantity => 1, :product => @products[0])]).mongo_save!
|
35
|
+
@orders << Order.new(:po_number => 1235, :order_products => [OrderProduct.new(:unit_price => 200, :quantity => 2, :product => @products[1])]).mongo_save!
|
36
|
+
end
|
37
|
+
it 'should have id' do
|
38
|
+
@orders.first.mongo_id.should be
|
39
|
+
end
|
40
|
+
it 'product should have name' do
|
41
|
+
Order.collection.find_one_object.order_products.first.product.name.should be
|
42
|
+
end
|
43
|
+
it 'naked reference' do
|
44
|
+
h = {'mongo_id' => @products.first.mongo_id}
|
45
|
+
h.should be_naked_reference
|
46
|
+
h['name'] = 'abc'
|
47
|
+
h.should_not be_naked_reference
|
48
|
+
end
|
49
|
+
it 'to_mongo_object ref' do
|
50
|
+
h = {'_id' => @products.first.mongo_id, '_mongo_class' => 'Product'}
|
51
|
+
h.to_mongo_object.name.should == 'Leather Couch'
|
52
|
+
h['name'] = 'Leather Couch'
|
53
|
+
h.to_mongo_object.name.should == 'Leather Couch'
|
54
|
+
end
|
55
|
+
it 'updates' do
|
56
|
+
@products.first.from_hash(:name => 'White Leather Couch').mongo_save!
|
57
|
+
Product.collection.find.count.should == 2
|
58
|
+
Product.collection.find_objects(:name => /Leather/).count.should == 1
|
59
|
+
Product.collection.find_one_object(:name => /Leather/).name.should == 'White Leather Couch'
|
60
|
+
end
|
61
|
+
it 'reference loading' do
|
62
|
+
@products.first.from_hash(:name => 'White Leather Couch').mongo_save!
|
63
|
+
Order.collection.find_one_object(:po_number => 1234).products.first.name.should == 'White Leather Couch'
|
64
|
+
end
|
65
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongo_persist
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Harris
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-19 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: fattr
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: activesupport
|
37
|
+
type: :runtime
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
description: Library to add MongoDB Persistance to normal Ruby objects
|
46
|
+
email: mharris717@gmail.com
|
47
|
+
executables: []
|
48
|
+
|
49
|
+
extensions: []
|
50
|
+
|
51
|
+
extra_rdoc_files:
|
52
|
+
- LICENSE
|
53
|
+
- README.rdoc
|
54
|
+
files:
|
55
|
+
- .document
|
56
|
+
- .gitignore
|
57
|
+
- LICENSE
|
58
|
+
- README.rdoc
|
59
|
+
- Rakefile
|
60
|
+
- VERSION
|
61
|
+
- lib/mongo_persist.rb
|
62
|
+
- lib/mongo_persist/util.rb
|
63
|
+
- spec/mongo_persist_spec.rb
|
64
|
+
- spec/spec_helper.rb
|
65
|
+
has_rdoc: true
|
66
|
+
homepage: http://github.com/mharris717/mongo_persist
|
67
|
+
licenses: []
|
68
|
+
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options:
|
71
|
+
- --charset=UTF-8
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
version:
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: "0"
|
85
|
+
version:
|
86
|
+
requirements: []
|
87
|
+
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 1.3.5
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: Library to add MongoDB Persistance to normal Ruby objects
|
93
|
+
test_files:
|
94
|
+
- spec/mongo_persist_spec.rb
|
95
|
+
- spec/spec_helper.rb
|