fcoury-mongomapper 0.2.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 +7 -0
- data/History +30 -0
- data/LICENSE +20 -0
- data/README.rdoc +39 -0
- data/Rakefile +71 -0
- data/VERSION +1 -0
- data/lib/mongomapper.rb +70 -0
- data/lib/mongomapper/associations.rb +69 -0
- data/lib/mongomapper/associations/array_proxy.rb +6 -0
- data/lib/mongomapper/associations/base.rb +54 -0
- data/lib/mongomapper/associations/belongs_to_proxy.rb +26 -0
- data/lib/mongomapper/associations/has_many_embedded_proxy.rb +19 -0
- data/lib/mongomapper/associations/has_many_proxy.rb +29 -0
- data/lib/mongomapper/associations/polymorphic_belongs_to_proxy.rb +31 -0
- data/lib/mongomapper/associations/proxy.rb +66 -0
- data/lib/mongomapper/callbacks.rb +106 -0
- data/lib/mongomapper/document.rb +276 -0
- data/lib/mongomapper/document_rails_compatibility.rb +13 -0
- data/lib/mongomapper/embedded_document.rb +248 -0
- data/lib/mongomapper/embedded_document_rails_compatibility.rb +22 -0
- data/lib/mongomapper/finder_options.rb +81 -0
- data/lib/mongomapper/key.rb +82 -0
- data/lib/mongomapper/observing.rb +50 -0
- data/lib/mongomapper/save_with_validation.rb +19 -0
- data/lib/mongomapper/serialization.rb +55 -0
- data/lib/mongomapper/serializers/json_serializer.rb +77 -0
- data/lib/mongomapper/validations.rb +47 -0
- data/mongomapper.gemspec +105 -0
- data/test/serializers/test_json_serializer.rb +104 -0
- data/test/test_associations.rb +444 -0
- data/test/test_callbacks.rb +84 -0
- data/test/test_document.rb +1002 -0
- data/test/test_embedded_document.rb +253 -0
- data/test/test_finder_options.rb +148 -0
- data/test/test_helper.rb +62 -0
- data/test/test_key.rb +200 -0
- data/test/test_mongomapper.rb +28 -0
- data/test/test_observing.rb +101 -0
- data/test/test_rails_compatibility.rb +73 -0
- data/test/test_serializations.rb +54 -0
- data/test/test_validations.rb +409 -0
- metadata +155 -0
data/.gitignore
ADDED
data/History
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
0.2.1(working)
|
2
|
+
* 1 major addition
|
3
|
+
* Added Document#paginate which works just like find but adds pagination (dcu did basics and I pimped)
|
4
|
+
* Added a basic console for playing around with MongoMapper (dcu)
|
5
|
+
* Embedded associations can now be deeply nested (Keith Hanson)
|
6
|
+
|
7
|
+
0.2.0 7/7/2009
|
8
|
+
* 2 major additions (observers, associations), several minor additions, and a few bug fixes
|
9
|
+
* Added observers
|
10
|
+
* many now supports embedded docs or docs in another collection (dcu on github)
|
11
|
+
* added belongs_to association (dcu)
|
12
|
+
* added validates_uniqueness_of (dcu)
|
13
|
+
* added :unique key shortcut to add validates_uniqueness_of automatically
|
14
|
+
* now tracking descendants of document (dcu)
|
15
|
+
* added validates_exclusion_of and validates_inclusion_of
|
16
|
+
* Bumped required version of validatable for callback fixes
|
17
|
+
* More thorough use of converting find conditions and options to mongo speak
|
18
|
+
* #attributes= no longer bombs when given nil
|
19
|
+
|
20
|
+
0.1.2 7/3/2009
|
21
|
+
* 2 minor changes
|
22
|
+
* Straightened out callbacks and added validate, validate_on_create and validate_on_update.
|
23
|
+
* Attributes passed into attributes= now call writer methods if they exist. This is mostly for virtual attributes.
|
24
|
+
|
25
|
+
0.1.1 6/28/2009
|
26
|
+
* 1 minor change
|
27
|
+
* bumped ruby driver to 0.9 and removed hacks I had in while waiting for it
|
28
|
+
|
29
|
+
0.1.0 6/26/2009
|
30
|
+
* Initial release
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 John Nunemaker
|
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,39 @@
|
|
1
|
+
= MongoMapper
|
2
|
+
|
3
|
+
Awesome gem for modeling your domain and storing it in mongo.
|
4
|
+
|
5
|
+
Releases are tagged on github and also released as gems on github and rubyforge. Master is pushed to whenever I add a patch or a new feature. To build from master, you can clone the code, generate the updated gemspec, build the gem and install.
|
6
|
+
|
7
|
+
* rake gemspec
|
8
|
+
* gem build mongomapper.gemspec
|
9
|
+
* gem install the gem that was built
|
10
|
+
|
11
|
+
== Note on Patches/Pull Requests
|
12
|
+
|
13
|
+
* Fork the project.
|
14
|
+
* Make your feature addition or bug fix.
|
15
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
16
|
+
* Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself in another branch so I can ignore when I pull)
|
17
|
+
* Send me a pull request. Bonus points for topic branches.
|
18
|
+
|
19
|
+
== Dependencies
|
20
|
+
|
21
|
+
* ActiveSupport (activesupport)
|
22
|
+
* Mongo Ruby Driver (mongodb-mongo)
|
23
|
+
* My fork of the validatable gem (jnunemaker-validatable)
|
24
|
+
|
25
|
+
== Documentation
|
26
|
+
|
27
|
+
http://rdoc.info/projects/jnunemaker/mongomapper
|
28
|
+
|
29
|
+
== More Info
|
30
|
+
|
31
|
+
You can learn more about mongo here:
|
32
|
+
http://www.mongodb.org/
|
33
|
+
|
34
|
+
You can learn more about the mongo ruby driver here:
|
35
|
+
http://github.com/mongodb/mongo-ruby-driver/tree/master
|
36
|
+
|
37
|
+
== Copyright
|
38
|
+
|
39
|
+
Copyright (c) 2009 John Nunemaker. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "mongomapper"
|
8
|
+
gem.summary = %Q{Awesome gem for modeling your domain and storing it in mongo}
|
9
|
+
gem.email = "nunemaker@gmail.com"
|
10
|
+
gem.homepage = "http://github.com/jnunemaker/mongomapper"
|
11
|
+
gem.authors = ["John Nunemaker"]
|
12
|
+
gem.rubyforge_project = "mongomapper"
|
13
|
+
|
14
|
+
gem.add_dependency('activesupport')
|
15
|
+
gem.add_dependency('mongodb-mongo', '0.9')
|
16
|
+
gem.add_dependency('jnunemaker-validatable', '1.7.1')
|
17
|
+
|
18
|
+
gem.add_development_dependency('mocha', '0.9.4')
|
19
|
+
gem.add_development_dependency('jnunemaker-matchy', '0.4.0')
|
20
|
+
end
|
21
|
+
|
22
|
+
Jeweler::RubyforgeTasks.new
|
23
|
+
rescue LoadError
|
24
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
25
|
+
end
|
26
|
+
|
27
|
+
require 'rake/testtask'
|
28
|
+
Rake::TestTask.new(:test) do |test|
|
29
|
+
test.libs << 'lib' << 'test'
|
30
|
+
test.pattern = 'test/**/test_*.rb'
|
31
|
+
test.verbose = true
|
32
|
+
end
|
33
|
+
|
34
|
+
begin
|
35
|
+
require 'rcov/rcovtask'
|
36
|
+
Rcov::RcovTask.new do |test|
|
37
|
+
test.libs << 'test'
|
38
|
+
test.pattern = 'test/**/test_*.rb'
|
39
|
+
test.verbose = true
|
40
|
+
end
|
41
|
+
rescue LoadError
|
42
|
+
task :rcov do
|
43
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
begin
|
48
|
+
require 'cucumber/rake/task'
|
49
|
+
Cucumber::Rake::Task.new(:features)
|
50
|
+
rescue LoadError
|
51
|
+
task :features do
|
52
|
+
abort "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
task :default => :test
|
57
|
+
|
58
|
+
require 'rake/rdoctask'
|
59
|
+
Rake::RDocTask.new do |rdoc|
|
60
|
+
if File.exist?('VERSION.yml')
|
61
|
+
config = YAML.load(File.read('VERSION.yml'))
|
62
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
63
|
+
else
|
64
|
+
version = ""
|
65
|
+
end
|
66
|
+
|
67
|
+
rdoc.rdoc_dir = 'rdoc'
|
68
|
+
rdoc.title = "MongoMapper #{version}"
|
69
|
+
rdoc.rdoc_files.include('README*')
|
70
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
71
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.0
|
data/lib/mongomapper.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
require 'rubygems'
|
3
|
+
|
4
|
+
gem 'activesupport'
|
5
|
+
gem 'mongodb-mongo', '0.9'
|
6
|
+
gem 'jnunemaker-validatable', '1.7.1'
|
7
|
+
|
8
|
+
require 'activesupport'
|
9
|
+
require 'mongo'
|
10
|
+
require 'validatable'
|
11
|
+
|
12
|
+
if RUBY_VERSION.to_f == 1.8
|
13
|
+
class BlankSlate #:nodoc:
|
14
|
+
instance_methods.each { |m| undef_method m unless m =~ /^__|instance_eval/ }
|
15
|
+
end
|
16
|
+
else
|
17
|
+
class BlankSlate < BasicObject; end
|
18
|
+
end
|
19
|
+
|
20
|
+
dir = Pathname(__FILE__).dirname.expand_path + 'mongomapper'
|
21
|
+
|
22
|
+
require dir + 'callbacks'
|
23
|
+
require dir + 'finder_options'
|
24
|
+
require dir + 'key'
|
25
|
+
require dir + 'observing'
|
26
|
+
require dir + 'pagination'
|
27
|
+
require dir + 'document_rails_compatibility'
|
28
|
+
require dir + 'embedded_document_rails_compatibility'
|
29
|
+
require dir + 'save_with_validation'
|
30
|
+
require dir + 'serialization'
|
31
|
+
require dir + 'validations'
|
32
|
+
|
33
|
+
require dir + 'associations/proxy'
|
34
|
+
require dir + 'associations/array_proxy'
|
35
|
+
require dir + 'associations/base'
|
36
|
+
require dir + 'associations/has_many_proxy'
|
37
|
+
require dir + 'associations/has_many_embedded_proxy'
|
38
|
+
require dir + 'associations/polymorphic_has_many_embedded_proxy'
|
39
|
+
require dir + 'associations/belongs_to_proxy'
|
40
|
+
require dir + 'associations/polymorphic_belongs_to_proxy'
|
41
|
+
require dir + 'associations'
|
42
|
+
|
43
|
+
require dir + 'embedded_document'
|
44
|
+
require dir + 'document'
|
45
|
+
|
46
|
+
module MongoMapper
|
47
|
+
class DocumentNotFound < StandardError; end
|
48
|
+
class DocumentNotValid < StandardError
|
49
|
+
def initialize(document)
|
50
|
+
@document = document
|
51
|
+
super("Validation failed: #{@document.errors.full_messages.join(", ")}")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.connection
|
56
|
+
@@connection ||= XGen::Mongo::Driver::Mongo.new
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.connection=(new_connection)
|
60
|
+
@@connection = new_connection
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.database=(name)
|
64
|
+
@@database = MongoMapper.connection.db(name)
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.database
|
68
|
+
@@database
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module Associations
|
3
|
+
module ClassMethods
|
4
|
+
def belongs_to(association_id, options = {})
|
5
|
+
association = create_association(:belongs_to, association_id, options)
|
6
|
+
|
7
|
+
ref_id = "#{association_id}_id"
|
8
|
+
key ref_id, String
|
9
|
+
|
10
|
+
define_method("#{ref_id}=") do |value|
|
11
|
+
write_attribute(ref_id, value)
|
12
|
+
end
|
13
|
+
|
14
|
+
if options[:polymorphic]
|
15
|
+
ref_type = "#{association_id}_type"
|
16
|
+
key ref_type, String
|
17
|
+
|
18
|
+
define_method("#{ref_type}=") do |value|
|
19
|
+
write_attribute(ref_type, value)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
define_association_methods(association)
|
24
|
+
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
def many(association_id, options = {})
|
29
|
+
association = create_association(:many, association_id, options)
|
30
|
+
define_association_methods(association)
|
31
|
+
|
32
|
+
self
|
33
|
+
end
|
34
|
+
|
35
|
+
def associations
|
36
|
+
@associations ||= HashWithIndifferentAccess.new
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
def create_association(type, name, options)
|
41
|
+
association = Associations::Base.new(type, name, options)
|
42
|
+
associations[association.name] = association
|
43
|
+
association
|
44
|
+
end
|
45
|
+
|
46
|
+
def define_association_methods(association)
|
47
|
+
define_method(association.name) do
|
48
|
+
get_proxy(association)
|
49
|
+
end
|
50
|
+
|
51
|
+
define_method("#{association.name}=") do |value|
|
52
|
+
get_proxy(association).replace(value)
|
53
|
+
value
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
module InstanceMethods
|
59
|
+
def get_proxy(association)
|
60
|
+
proxy = self.instance_variable_get(association.ivar)
|
61
|
+
if proxy.nil?
|
62
|
+
proxy = association.proxy_class.new(self, association)
|
63
|
+
self.instance_variable_set(association.ivar, proxy)
|
64
|
+
end
|
65
|
+
proxy
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module Associations
|
3
|
+
class Base
|
4
|
+
attr_reader :type, :name, :options
|
5
|
+
|
6
|
+
def initialize(type, name, options = {})
|
7
|
+
@options = options
|
8
|
+
@type = type
|
9
|
+
@name = name
|
10
|
+
end
|
11
|
+
|
12
|
+
def klass
|
13
|
+
class_name.constantize
|
14
|
+
end
|
15
|
+
|
16
|
+
def class_name
|
17
|
+
@class_name ||= begin
|
18
|
+
if cn = options[:class_name]
|
19
|
+
cn
|
20
|
+
elsif @type == :many
|
21
|
+
name.to_s.singularize.camelize
|
22
|
+
else
|
23
|
+
name.to_s.camelize
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def ivar
|
29
|
+
@ivar ||= "@_#{name}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def proxy_class
|
33
|
+
case @type
|
34
|
+
when :belongs_to
|
35
|
+
if @options[:polymorphic]
|
36
|
+
PolymorphicBelongsToProxy
|
37
|
+
else
|
38
|
+
BelongsToProxy
|
39
|
+
end
|
40
|
+
when :many
|
41
|
+
if self.klass.embeddable?
|
42
|
+
if @options[:polymorphic]
|
43
|
+
PolymorphicHasManyEmbeddedProxy
|
44
|
+
else
|
45
|
+
HasManyEmbeddedProxy
|
46
|
+
end
|
47
|
+
else
|
48
|
+
HasManyProxy
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module Associations
|
3
|
+
class BelongsToProxy < Proxy
|
4
|
+
def replace(v)
|
5
|
+
ref_id = "#{@association.name}_id"
|
6
|
+
|
7
|
+
if v
|
8
|
+
v.save if v.new?
|
9
|
+
@owner.__send__(:write_attribute, ref_id, v.id)
|
10
|
+
else
|
11
|
+
@owner.__send__(:write_attribute, ref_id, nil)
|
12
|
+
end
|
13
|
+
|
14
|
+
reload_target
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
def find_target
|
19
|
+
ref = @owner.__send__(:read_attribute, "#{@association.name}_id")
|
20
|
+
if ref
|
21
|
+
@association.klass.find(ref)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module Associations
|
3
|
+
class HasManyEmbeddedProxy < ArrayProxy
|
4
|
+
def replace(v)
|
5
|
+
@_values = v.map { |e| e.kind_of?(EmbeddedDocument) ? e.attributes : e }
|
6
|
+
@target = nil
|
7
|
+
|
8
|
+
reload_target
|
9
|
+
end
|
10
|
+
|
11
|
+
protected
|
12
|
+
def find_target
|
13
|
+
(@_values || []).map do |e|
|
14
|
+
@association.klass.new(e)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module Associations
|
3
|
+
class HasManyProxy < ArrayProxy
|
4
|
+
def replace(v)
|
5
|
+
if load_target
|
6
|
+
@target.map(&:destroy)
|
7
|
+
end
|
8
|
+
|
9
|
+
v.each do |o|
|
10
|
+
@owner.save if @owner.new?
|
11
|
+
o.__send__(:write_attribute, self.foreign_key, @owner.id)
|
12
|
+
o.save
|
13
|
+
o
|
14
|
+
end
|
15
|
+
|
16
|
+
reload_target
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
def find_target
|
21
|
+
@association.klass.find(:all, {:conditions => {self.foreign_key => @owner.id}})
|
22
|
+
end
|
23
|
+
|
24
|
+
def foreign_key
|
25
|
+
@association.options[:foreign_key] || @owner.class.name.underscore.gsub("/", "_") + "_id"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|