ramsingla-mongomapper 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/.gitignore +7 -0
  2. data/History +30 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +39 -0
  5. data/Rakefile +71 -0
  6. data/VERSION +1 -0
  7. data/lib/mongomapper.rb +60 -0
  8. data/lib/mongomapper/associations.rb +69 -0
  9. data/lib/mongomapper/associations/array_proxy.rb +6 -0
  10. data/lib/mongomapper/associations/base.rb +50 -0
  11. data/lib/mongomapper/associations/belongs_to_proxy.rb +26 -0
  12. data/lib/mongomapper/associations/has_many_embedded_proxy.rb +19 -0
  13. data/lib/mongomapper/associations/has_many_proxy.rb +28 -0
  14. data/lib/mongomapper/associations/polymorphic_belongs_to_proxy.rb +31 -0
  15. data/lib/mongomapper/associations/proxy.rb +60 -0
  16. data/lib/mongomapper/callbacks.rb +106 -0
  17. data/lib/mongomapper/document.rb +263 -0
  18. data/lib/mongomapper/embedded_document.rb +295 -0
  19. data/lib/mongomapper/finder_options.rb +81 -0
  20. data/lib/mongomapper/key.rb +82 -0
  21. data/lib/mongomapper/observing.rb +50 -0
  22. data/lib/mongomapper/pagination.rb +52 -0
  23. data/lib/mongomapper/rails_compatibility.rb +23 -0
  24. data/lib/mongomapper/save_with_validation.rb +19 -0
  25. data/lib/mongomapper/serialization.rb +55 -0
  26. data/lib/mongomapper/serializers/json_serializer.rb +77 -0
  27. data/lib/mongomapper/validations.rb +47 -0
  28. data/mongomapper.gemspec +105 -0
  29. data/test/serializers/test_json_serializer.rb +104 -0
  30. data/test/test_associations.rb +211 -0
  31. data/test/test_callbacks.rb +84 -0
  32. data/test/test_document.rb +995 -0
  33. data/test/test_embedded_document.rb +253 -0
  34. data/test/test_finder_options.rb +148 -0
  35. data/test/test_helper.rb +62 -0
  36. data/test/test_key.rb +200 -0
  37. data/test/test_mongomapper.rb +28 -0
  38. data/test/test_observing.rb +101 -0
  39. data/test/test_rails_compatibility.rb +29 -0
  40. data/test/test_serializations.rb +54 -0
  41. data/test/test_validations.rb +409 -0
  42. metadata +156 -0
@@ -0,0 +1,7 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ *~
7
+ *.gem
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.
@@ -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.
@@ -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
@@ -0,0 +1,60 @@
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
+ dir = Pathname(__FILE__).dirname.expand_path + 'mongomapper'
13
+
14
+ require dir + 'callbacks'
15
+ require dir + 'finder_options'
16
+ require dir + 'key'
17
+ require dir + 'observing'
18
+ require dir + 'pagination'
19
+ require dir + 'rails_compatibility'
20
+ require dir + 'save_with_validation'
21
+ require dir + 'serialization'
22
+ require dir + 'validations'
23
+
24
+ require dir + 'associations/proxy'
25
+ require dir + 'associations/array_proxy'
26
+ require dir + 'associations/base'
27
+ require dir + 'associations/has_many_proxy'
28
+ require dir + 'associations/has_many_embedded_proxy'
29
+ require dir + 'associations/belongs_to_proxy'
30
+ require dir + 'associations/polymorphic_belongs_to_proxy'
31
+ require dir + 'associations'
32
+
33
+ require dir + 'embedded_document'
34
+ require dir + 'document'
35
+
36
+ module MongoMapper
37
+ class DocumentNotFound < StandardError; end
38
+ class DocumentNotValid < StandardError
39
+ def initialize(document)
40
+ @document = document
41
+ super("Validation failed: #{@document.errors.full_messages.join(", ")}")
42
+ end
43
+ end
44
+
45
+ def self.connection
46
+ @@connection ||= XGen::Mongo::Driver::Mongo.new
47
+ end
48
+
49
+ def self.connection=(new_connection)
50
+ @@connection = new_connection
51
+ end
52
+
53
+ def self.database=(name)
54
+ @@database = MongoMapper.connection.db(name)
55
+ end
56
+
57
+ def self.database
58
+ @@database
59
+ end
60
+ 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,6 @@
1
+ module MongoMapper
2
+ module Associations
3
+ class ArrayProxy < Proxy
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,50 @@
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
+ HasManyEmbeddedProxy
43
+ else
44
+ HasManyProxy
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ 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,28 @@
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
+ reload_target
16
+ end
17
+
18
+ protected
19
+ def find_target
20
+ @association.klass.find(:all, {:conditions => {self.foreign_key => @owner.id}})
21
+ end
22
+
23
+ def foreign_key
24
+ @association.options[:foreign_key] || @owner.class.name.underscore.gsub("/", "_") + "_id"
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,31 @@
1
+ module MongoMapper
2
+ module Associations
3
+ class PolymorphicBelongsToProxy < Proxy
4
+ def replace(v)
5
+ ref_id = "#{@association.name}_id"
6
+ ref_type = "#{@association.name}_type"
7
+
8
+ if v
9
+ v.save if v.new?
10
+ @owner.__send__(:write_attribute, ref_id, v.id)
11
+ @owner.__send__(:write_attribute, ref_type, v.class.name)
12
+ else
13
+ @owner.__send__(:write_attribute, ref_id, nil)
14
+ @owner.__send__(:write_attribute, ref_type, nil)
15
+ end
16
+ @owner.save
17
+
18
+ reload_target
19
+ end
20
+
21
+ protected
22
+ def find_target
23
+ ref_id = @owner.__send__(:read_attribute, "#{@association.name}_id")
24
+ ref_type = @owner.__send__(:read_attribute, "#{@association.name}_type")
25
+ if ref_id && ref_type
26
+ ref_type.constantize.find(ref_id)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end