webbynode-mongo_mapper 0.6.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. data/.gitignore +10 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +53 -0
  4. data/Rakefile +55 -0
  5. data/VERSION +1 -0
  6. data/bin/mmconsole +60 -0
  7. data/lib/mongo_mapper.rb +134 -0
  8. data/lib/mongo_mapper/associations.rb +183 -0
  9. data/lib/mongo_mapper/associations/base.rb +110 -0
  10. data/lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb +34 -0
  11. data/lib/mongo_mapper/associations/belongs_to_proxy.rb +22 -0
  12. data/lib/mongo_mapper/associations/many_documents_as_proxy.rb +25 -0
  13. data/lib/mongo_mapper/associations/many_documents_proxy.rb +127 -0
  14. data/lib/mongo_mapper/associations/many_embedded_polymorphic_proxy.rb +33 -0
  15. data/lib/mongo_mapper/associations/many_embedded_proxy.rb +53 -0
  16. data/lib/mongo_mapper/associations/many_polymorphic_proxy.rb +11 -0
  17. data/lib/mongo_mapper/associations/many_proxy.rb +6 -0
  18. data/lib/mongo_mapper/associations/proxy.rb +80 -0
  19. data/lib/mongo_mapper/callbacks.rb +109 -0
  20. data/lib/mongo_mapper/dirty.rb +136 -0
  21. data/lib/mongo_mapper/document.rb +481 -0
  22. data/lib/mongo_mapper/dynamic_finder.rb +35 -0
  23. data/lib/mongo_mapper/embedded_document.rb +386 -0
  24. data/lib/mongo_mapper/finder_options.rb +133 -0
  25. data/lib/mongo_mapper/key.rb +36 -0
  26. data/lib/mongo_mapper/observing.rb +50 -0
  27. data/lib/mongo_mapper/pagination.rb +53 -0
  28. data/lib/mongo_mapper/rails_compatibility/document.rb +15 -0
  29. data/lib/mongo_mapper/rails_compatibility/embedded_document.rb +27 -0
  30. data/lib/mongo_mapper/serialization.rb +56 -0
  31. data/lib/mongo_mapper/serializers/json_serializer.rb +92 -0
  32. data/lib/mongo_mapper/support.rb +193 -0
  33. data/lib/mongo_mapper/validations.rb +41 -0
  34. data/mongo_mapper.gemspec +171 -0
  35. data/specs.watchr +32 -0
  36. data/test/NOTE_ON_TESTING +1 -0
  37. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +55 -0
  38. data/test/functional/associations/test_belongs_to_proxy.rb +48 -0
  39. data/test/functional/associations/test_many_documents_as_proxy.rb +246 -0
  40. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +156 -0
  41. data/test/functional/associations/test_many_embedded_proxy.rb +196 -0
  42. data/test/functional/associations/test_many_polymorphic_proxy.rb +339 -0
  43. data/test/functional/associations/test_many_proxy.rb +384 -0
  44. data/test/functional/test_associations.rb +44 -0
  45. data/test/functional/test_binary.rb +18 -0
  46. data/test/functional/test_callbacks.rb +85 -0
  47. data/test/functional/test_dirty.rb +159 -0
  48. data/test/functional/test_document.rb +1180 -0
  49. data/test/functional/test_embedded_document.rb +125 -0
  50. data/test/functional/test_logger.rb +20 -0
  51. data/test/functional/test_pagination.rb +95 -0
  52. data/test/functional/test_rails_compatibility.rb +25 -0
  53. data/test/functional/test_string_id_compatibility.rb +72 -0
  54. data/test/functional/test_validations.rb +369 -0
  55. data/test/models.rb +271 -0
  56. data/test/support/custom_matchers.rb +55 -0
  57. data/test/support/timing.rb +16 -0
  58. data/test/test_helper.rb +27 -0
  59. data/test/unit/serializers/test_json_serializer.rb +189 -0
  60. data/test/unit/test_association_base.rb +166 -0
  61. data/test/unit/test_document.rb +204 -0
  62. data/test/unit/test_dynamic_finder.rb +125 -0
  63. data/test/unit/test_embedded_document.rb +718 -0
  64. data/test/unit/test_finder_options.rb +296 -0
  65. data/test/unit/test_key.rb +172 -0
  66. data/test/unit/test_mongo_mapper.rb +65 -0
  67. data/test/unit/test_observing.rb +101 -0
  68. data/test/unit/test_pagination.rb +113 -0
  69. data/test/unit/test_rails_compatibility.rb +49 -0
  70. data/test/unit/test_serializations.rb +52 -0
  71. data/test/unit/test_support.rb +342 -0
  72. data/test/unit/test_time_zones.rb +40 -0
  73. data/test/unit/test_validations.rb +503 -0
  74. metadata +234 -0
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ *~
7
+ *.gem
8
+ tmp
9
+ .yardoc
10
+ doc/*
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,53 @@
1
+ = MongoMapper
2
+
3
+ Awesome gem for modeling your domain and storing it in mongo.
4
+
5
+ Releases are tagged on github and released on gemcutter. Master is pushed to whenever I add a patch or a new feature, but I do not release a new gem version each time I push.
6
+
7
+ == Note on Patches/Pull Requests
8
+
9
+ * Fork the project.
10
+ * Make your feature addition or bug fix.
11
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
12
+ * 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)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Install
16
+
17
+ MongoMapper is only released on gemcutter. To install, you can setup gemcutter as your default gem source.
18
+
19
+ $ gem install gemcutter
20
+ $ gem tumble
21
+
22
+ Then you can install MM:
23
+
24
+ $ gem install mongo_mapper
25
+
26
+ You can also just declare the source:
27
+
28
+ $ gem install mongo_mapper -s http://gemcutter.org
29
+
30
+ == Dependencies
31
+
32
+ * ActiveSupport (typically the latest version)
33
+ * Mongo Ruby Driver (mongo)
34
+ * My fork of the validatable gem (jnunemaker-validatable)
35
+
36
+ == Documentation
37
+
38
+ Documentation is lacking right now because if you can't look through the code right now and feel comfortable, this is probably too young for you to use. Wait til it stabilizes a bit more.
39
+
40
+ http://rdoc.info/projects/jnunemaker/mongomapper
41
+ http://groups.google.com/group/mongomapper
42
+
43
+ == More Info
44
+
45
+ You can learn more about mongo here:
46
+ http://www.mongodb.org/
47
+
48
+ You can learn more about the mongo ruby driver here:
49
+ http://github.com/mongodb/mongo-ruby-driver/tree/master
50
+
51
+ == Copyright
52
+
53
+ Copyright (c) 2009 John Nunemaker. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,55 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'jeweler'
4
+ require 'yard'
5
+ require 'yard/rake/yardoc_task'
6
+
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = "webbynode-mongo_mapper"
9
+ gem.summary = %Q{Awesome gem for modeling your domain and storing it in mongo}
10
+ gem.email = "nunemaker@gmail.com"
11
+ gem.homepage = "http://github.com/jnunemaker/mongomapper"
12
+ gem.authors = ["John Nunemaker", "Felipe Coury"]
13
+
14
+ gem.add_dependency('activesupport', '>= 2.3')
15
+ gem.add_dependency('mongo', '0.17.1')
16
+ gem.add_dependency('jnunemaker-validatable', '1.8.1')
17
+
18
+ gem.add_development_dependency('jnunemaker-matchy', '0.4.0')
19
+ gem.add_development_dependency('shoulda', '2.10.2')
20
+ gem.add_development_dependency('timecop', '0.3.1')
21
+ gem.add_development_dependency('mocha', '0.9.8')
22
+ end
23
+
24
+ Jeweler::GemcutterTasks.new
25
+
26
+ require 'rake/testtask'
27
+ Rake::TestTask.new(:test) do |test|
28
+ test.libs << 'test'
29
+ test.ruby_opts << '-rubygems'
30
+ test.pattern = 'test/**/test_*.rb'
31
+ test.verbose = true
32
+ end
33
+
34
+ namespace :test do
35
+ Rake::TestTask.new(:units) do |test|
36
+ test.libs << 'test'
37
+ test.ruby_opts << '-rubygems'
38
+ test.pattern = 'test/unit/**/test_*.rb'
39
+ test.verbose = true
40
+ end
41
+
42
+ Rake::TestTask.new(:functionals) do |test|
43
+ test.libs << 'test'
44
+ test.ruby_opts << '-rubygems'
45
+ test.pattern = 'test/functional/**/test_*.rb'
46
+ test.verbose = true
47
+ end
48
+ end
49
+
50
+ task :default => :test
51
+ task :test => :check_dependencies
52
+
53
+ YARD::Rake::YardocTask.new(:doc) do |t|
54
+ t.options = ["--legacy"] if RUBY_VERSION < "1.9.0"
55
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.6.4
data/bin/mmconsole ADDED
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
3
+
4
+ begin
5
+ require 'mongo_mapper'
6
+ require 'irb'
7
+ rescue LoadError
8
+ require 'rubygems'
9
+ retry
10
+ end
11
+
12
+ IRB.setup(nil)
13
+ irb = IRB::Irb.new
14
+
15
+ IRB.conf[:MAIN_CONTEXT] = irb.context
16
+
17
+ irb.context.evaluate("require 'irb/completion'", 0)
18
+ irb.context.evaluate(%@
19
+ include MongoMapper
20
+
21
+ MongoMapper.database = "mmtest"
22
+ $db = MongoMapper.database
23
+
24
+ @, 0)
25
+
26
+ puts %@
27
+ Welcome to the MongoMapper Console!
28
+
29
+ Example 1:
30
+ things = $db.collection("things")
31
+ things.insert("name" => "Raw Thing")
32
+ things.insert("name" => "Another Thing", "date" => Time.now)
33
+
34
+ cursor = things.find("name" => "Raw Thing")
35
+ puts cursor.next_object.inspect
36
+
37
+ Example 2:
38
+ class Thing
39
+ include MongoMapper::Document
40
+ key :name, String, :required => true
41
+ key :date, Time
42
+ end
43
+
44
+ thing = Thing.new
45
+ thing.name = "My thing"
46
+ thing.date = Time.now
47
+ thing.save
48
+
49
+ all_things = Thing.all
50
+ puts all_things.map { |object| object.name }.inspect
51
+ @
52
+
53
+ trap("SIGINT") do
54
+ irb.signal_handle
55
+ end
56
+
57
+ catch(:IRB_EXIT) do
58
+ irb.eval_input
59
+ end
60
+
@@ -0,0 +1,134 @@
1
+ require 'active_support'
2
+ require 'mongo'
3
+ require 'validatable'
4
+
5
+ module MongoMapper
6
+ # generic MM error
7
+ class MongoMapperError < StandardError; end
8
+
9
+ # raised when key expected to exist but not found
10
+ class KeyNotFound < MongoMapperError; end
11
+
12
+ # raised when document expected but not found
13
+ class DocumentNotFound < MongoMapperError; end
14
+
15
+ # raised when document not valid and using !
16
+ class DocumentNotValid < MongoMapperError
17
+ def initialize(document)
18
+ super("Validation failed: #{document.errors.full_messages.join(", ")}")
19
+ end
20
+ end
21
+
22
+ # @api public
23
+ def self.connection
24
+ @@connection ||= Mongo::Connection.new
25
+ end
26
+
27
+ # @api public
28
+ def self.connection=(new_connection)
29
+ @@connection = new_connection
30
+ end
31
+
32
+ # @api public
33
+ def self.logger
34
+ connection.logger
35
+ end
36
+
37
+ # @api public
38
+ def self.database=(name)
39
+ @@database = nil
40
+ @@database_name = name
41
+ end
42
+
43
+ # @api public
44
+ def self.database
45
+ if @@database_name.blank?
46
+ raise 'You forgot to set the default database name: MongoMapper.database = "foobar"'
47
+ end
48
+
49
+ @@database ||= MongoMapper.connection.db(@@database_name)
50
+ end
51
+
52
+ # @api private
53
+ def self.ensured_indexes
54
+ @@ensured_indexes ||= []
55
+ end
56
+
57
+ # @api private
58
+ def self.ensure_index(klass, keys, options={})
59
+ ensured_indexes << {:klass => klass, :keys => keys, :options => options}
60
+ end
61
+
62
+ # @api public
63
+ def self.ensure_indexes!
64
+ ensured_indexes.each do |index|
65
+ unique = index[:options].delete(:unique)
66
+ index[:klass].collection.create_index(index[:keys], unique)
67
+ end
68
+ end
69
+
70
+ # @api private
71
+ module Finders
72
+ def dynamic_find(finder, args)
73
+ attributes = {}
74
+ finder.attributes.each_with_index do |attr, index|
75
+ attributes[attr] = args[index]
76
+ end
77
+
78
+ options = args.extract_options!.merge(attributes)
79
+ result = find(finder.finder, options)
80
+
81
+ if result.nil?
82
+ if finder.bang
83
+ raise DocumentNotFound, "Couldn't find Document with #{attributes.inspect} in collection named #{collection.name}"
84
+ end
85
+
86
+ if finder.instantiator
87
+ self.send(finder.instantiator, attributes)
88
+ end
89
+ else
90
+ result
91
+ end
92
+ end
93
+ end
94
+
95
+ # @api private
96
+ def self.use_time_zone?
97
+ Time.respond_to?(:zone) && Time.zone ? true : false
98
+ end
99
+
100
+ # @api private
101
+ def self.time_class
102
+ use_time_zone? ? Time.zone : Time
103
+ end
104
+
105
+ def self.normalize_object_id(value)
106
+ value.is_a?(String) ? Mongo::ObjectID.from_string(value) : value
107
+ end
108
+ end
109
+
110
+ require 'mongo_mapper/support'
111
+ require 'mongo_mapper/associations'
112
+ require 'mongo_mapper/associations/base'
113
+ require 'mongo_mapper/associations/proxy'
114
+ require 'mongo_mapper/associations/many_documents_proxy'
115
+ require 'mongo_mapper/associations/belongs_to_proxy'
116
+ require 'mongo_mapper/associations/belongs_to_polymorphic_proxy'
117
+ require 'mongo_mapper/associations/many_proxy'
118
+ require 'mongo_mapper/associations/many_polymorphic_proxy'
119
+ require 'mongo_mapper/associations/many_embedded_proxy'
120
+ require 'mongo_mapper/associations/many_embedded_polymorphic_proxy'
121
+ require 'mongo_mapper/associations/many_documents_as_proxy'
122
+ require 'mongo_mapper/callbacks'
123
+ require 'mongo_mapper/finder_options'
124
+ require 'mongo_mapper/dirty'
125
+ require 'mongo_mapper/dynamic_finder'
126
+ require 'mongo_mapper/key'
127
+ require 'mongo_mapper/observing'
128
+ require 'mongo_mapper/pagination'
129
+ require 'mongo_mapper/serialization'
130
+ require 'mongo_mapper/validations'
131
+ require 'mongo_mapper/rails_compatibility/document'
132
+ require 'mongo_mapper/rails_compatibility/embedded_document'
133
+ require 'mongo_mapper/embedded_document'
134
+ require 'mongo_mapper/document'
@@ -0,0 +1,183 @@
1
+ module MongoMapper
2
+ module Associations
3
+ module ClassMethods
4
+ ##
5
+ # This macro allows you define a "belongs-to" relationship between one
6
+ # document and some other document.
7
+ #
8
+ # == Requirements
9
+ #
10
+ # Usage of this macro requires that your document define a key that can
11
+ # be used to store the ID of the target document that is the parent of
12
+ # this document.
13
+ #
14
+ # == Conventions
15
+ #
16
+ # The following is a list of the conventions used by MongoMapper in
17
+ # defining a belongs-to relationship. Each can likely be overridden via
18
+ # the +options+ parameter.
19
+ #
20
+ # * The name of your belongs-to association is the lowercase, singular
21
+ # name of the target class
22
+ # * A key with the name of your association exists with an "_id" suffix
23
+ # to store the ID of the target of this relationship
24
+ #
25
+ # @param [Symbol] association_id The name of this association
26
+ # @param [Hash] options Optional parameters that define the
27
+ # characteristics of this relationship. These are often used to
28
+ # override MongoMapper conventions.
29
+ # @option options [Boolean] :polymorphic (false) Set this option to
30
+ # <code>true</code> to define a relationship that can be between this
31
+ # document and any other type of document. Note that you *must* also
32
+ # have a key on your document to store the type of document in this
33
+ # relationship.
34
+ # @option options [String] :class_name If your relationship doesn't use
35
+ # the name of some class, you *must* use this option to indicate the
36
+ # target class for this relationship.
37
+ # @option options [Symbol] :foreign_key Use this option to specify a
38
+ # non-conventional key that stores the ID of the parent in this
39
+ # relationship
40
+ #
41
+ # @example Conventional, and simple, usage of <code>belongs_to</code>
42
+ # class Novel
43
+ # include MongoMapper::Document
44
+ #
45
+ # key :author_id, String # our "foreign key"
46
+ #
47
+ # belongs_to :author
48
+ # end
49
+ #
50
+ # @example Using :foreign_key and :class_name
51
+ # class Pet
52
+ # include MongoMapper::Document
53
+ #
54
+ # key :person_id, String
55
+ #
56
+ # belongs_to :owner,
57
+ # :foreign_key => :person_id,
58
+ # :class_name => "Person"
59
+ # end
60
+ #
61
+ # @example Defining a polymorphic belongs-to relationship
62
+ # class Vehicle
63
+ # include MongoMapper::Document
64
+ #
65
+ # key :owner_id, String
66
+ # key :owner_type, String
67
+ #
68
+ # belongs_to :owner,
69
+ # :polymorphic => true
70
+ # end
71
+ #
72
+ # @example Non-standard polymorphic belongs-to relationship
73
+ # class Vehicle
74
+ # include MongoMapper::Document
75
+ #
76
+ # key :person_id, String
77
+ # key :person_type, String
78
+ #
79
+ # belongs_to :owner,
80
+ # :polymorphic => true,
81
+ # :foreign_key => "person_id",
82
+ # :type_key_name => "person_type"
83
+ # end
84
+ def belongs_to(association_id, options={}, &extension)
85
+ create_association(:belongs_to, association_id, options, &extension)
86
+ self
87
+ end
88
+
89
+ ##
90
+ # This macro allows you to define a "has-many" relationship between a
91
+ # document, and numerous child documents.
92
+ #
93
+ # == Conventions
94
+ #
95
+ # The following is a list of the conventions used by MongoMapper in
96
+ # defining this relationship. Each can likely be overridden via the
97
+ # +options+ parameter.
98
+ #
99
+ # * The name of your association is the lowercase, *plural* name of the
100
+ # target class
101
+ # * Your target class must have a "foreign key" bearing the name of this
102
+ # class suffixed by "_id"
103
+ #
104
+ # @param [Symbol] association_id The name of this association
105
+ # @param [Hash] options Optional parameters that define the
106
+ # characteristics of this relationship. These are often used to
107
+ # override MongoMapper conventions.
108
+ # @option options [String] :class_name If your relationship doesn't use
109
+ # the name of some class, you *must* use this option to indicate the
110
+ # target class for this relationship.
111
+ # @option options [Symbol] :foreign_key Use this option to specify a
112
+ # non-conventional key that stores the ID of the parent in this
113
+ # relationship
114
+ # @option options [#to_s] :as Used when the target relationship is
115
+ # polymorphic (i.e. the +belongs_to+ has set <tt>:polymorphic</tt> to
116
+ # +true+). See examples for usage.
117
+ def many(association_id, options={}, &extension)
118
+ create_association(:many, association_id, options, &extension)
119
+ self
120
+ end
121
+
122
+ def associations
123
+ @associations ||= self.superclass.respond_to?(:associations) ?
124
+ self.superclass.associations :
125
+ HashWithIndifferentAccess.new
126
+ end
127
+
128
+ private
129
+ def create_association(type, name, options, &extension)
130
+ association = Associations::Base.new(type, name, options, &extension)
131
+ associations[association.name] = association
132
+ define_association_methods(association)
133
+ define_dependent_callback(association)
134
+ association
135
+ end
136
+
137
+ def define_association_methods(association)
138
+ define_method(association.name) do
139
+ get_proxy(association)
140
+ end
141
+
142
+ define_method("#{association.name}=") do |value|
143
+ get_proxy(association).replace(value)
144
+ value
145
+ end
146
+ end
147
+
148
+ def define_dependent_callback(association)
149
+ if association.options[:dependent]
150
+ if association.many?
151
+ define_dependent_callback_for_many(association)
152
+ end
153
+ end
154
+ end
155
+
156
+ def define_dependent_callback_for_many(association)
157
+ after_destroy do |doc|
158
+ if !association.embeddable?
159
+ case association.options[:dependent]
160
+ when :destroy
161
+ doc.get_proxy(association).destroy_all
162
+ when :delete_all
163
+ doc.get_proxy(association).delete_all
164
+ when :nullify
165
+ doc.get_proxy(association).nullify
166
+ end
167
+ end
168
+ end
169
+ end
170
+ end
171
+
172
+ module InstanceMethods
173
+ def get_proxy(association)
174
+ unless proxy = self.instance_variable_get(association.ivar)
175
+ proxy = association.proxy_class.new(self, association)
176
+ self.instance_variable_set(association.ivar, proxy) if !frozen?
177
+ end
178
+
179
+ proxy
180
+ end
181
+ end
182
+ end
183
+ end