crnixon-mongomapper 0.2.0 → 0.3.4

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.
Files changed (68) hide show
  1. data/.gitignore +1 -0
  2. data/History +48 -0
  3. data/README.rdoc +5 -3
  4. data/Rakefile +6 -4
  5. data/VERSION +1 -1
  6. data/bin/mmconsole +56 -0
  7. data/lib/mongomapper.rb +29 -18
  8. data/lib/mongomapper/associations.rb +53 -38
  9. data/lib/mongomapper/associations/base.rb +53 -20
  10. data/lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb +34 -0
  11. data/lib/mongomapper/associations/belongs_to_proxy.rb +10 -14
  12. data/lib/mongomapper/associations/many_documents_as_proxy.rb +27 -0
  13. data/lib/mongomapper/associations/many_documents_proxy.rb +103 -0
  14. data/lib/mongomapper/associations/many_embedded_polymorphic_proxy.rb +33 -0
  15. data/lib/mongomapper/associations/{has_many_embedded_proxy.rb → many_embedded_proxy.rb} +6 -8
  16. data/lib/mongomapper/associations/many_polymorphic_proxy.rb +11 -0
  17. data/lib/mongomapper/associations/{array_proxy.rb → many_proxy.rb} +1 -1
  18. data/lib/mongomapper/associations/proxy.rb +24 -21
  19. data/lib/mongomapper/callbacks.rb +1 -1
  20. data/lib/mongomapper/document.rb +160 -74
  21. data/lib/mongomapper/dynamic_finder.rb +38 -0
  22. data/lib/mongomapper/embedded_document.rb +154 -105
  23. data/lib/mongomapper/finder_options.rb +11 -7
  24. data/lib/mongomapper/key.rb +15 -21
  25. data/lib/mongomapper/pagination.rb +52 -0
  26. data/lib/mongomapper/rails_compatibility/document.rb +15 -0
  27. data/lib/mongomapper/rails_compatibility/embedded_document.rb +25 -0
  28. data/lib/mongomapper/serialization.rb +1 -1
  29. data/lib/mongomapper/serializers/json_serializer.rb +15 -0
  30. data/lib/mongomapper/support.rb +30 -0
  31. data/mongomapper.gemspec +87 -46
  32. data/test/NOTE_ON_TESTING +1 -0
  33. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +53 -0
  34. data/test/functional/associations/test_belongs_to_proxy.rb +45 -0
  35. data/test/functional/associations/test_many_documents_as_proxy.rb +253 -0
  36. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +131 -0
  37. data/test/functional/associations/test_many_embedded_proxy.rb +106 -0
  38. data/test/functional/associations/test_many_polymorphic_proxy.rb +261 -0
  39. data/test/functional/associations/test_many_proxy.rb +295 -0
  40. data/test/functional/test_associations.rb +47 -0
  41. data/test/{test_callbacks.rb → functional/test_callbacks.rb} +2 -1
  42. data/test/functional/test_document.rb +952 -0
  43. data/test/functional/test_pagination.rb +81 -0
  44. data/test/functional/test_rails_compatibility.rb +30 -0
  45. data/test/functional/test_validations.rb +172 -0
  46. data/test/models.rb +169 -0
  47. data/test/test_helper.rb +7 -2
  48. data/test/unit/serializers/test_json_serializer.rb +189 -0
  49. data/test/unit/test_association_base.rb +144 -0
  50. data/test/unit/test_document.rb +123 -0
  51. data/test/unit/test_embedded_document.rb +526 -0
  52. data/test/{test_finder_options.rb → unit/test_finder_options.rb} +36 -1
  53. data/test/{test_key.rb → unit/test_key.rb} +59 -12
  54. data/test/{test_mongomapper.rb → unit/test_mongomapper.rb} +0 -0
  55. data/test/{test_observing.rb → unit/test_observing.rb} +0 -0
  56. data/test/unit/test_pagination.rb +113 -0
  57. data/test/unit/test_rails_compatibility.rb +34 -0
  58. data/test/{test_serializations.rb → unit/test_serializations.rb} +0 -2
  59. data/test/{test_validations.rb → unit/test_validations.rb} +0 -134
  60. metadata +81 -43
  61. data/lib/mongomapper/associations/has_many_proxy.rb +0 -28
  62. data/lib/mongomapper/associations/polymorphic_belongs_to_proxy.rb +0 -31
  63. data/lib/mongomapper/rails_compatibility.rb +0 -23
  64. data/test/serializers/test_json_serializer.rb +0 -104
  65. data/test/test_associations.rb +0 -174
  66. data/test/test_document.rb +0 -944
  67. data/test/test_embedded_document.rb +0 -253
  68. data/test/test_rails_compatibility.rb +0 -29
@@ -0,0 +1,52 @@
1
+ module MongoMapper
2
+ module Pagination
3
+ class PaginationProxy < BasicObject
4
+ attr_accessor :subject
5
+ attr_reader :total_entries, :per_page, :current_page
6
+ alias limit per_page
7
+
8
+ def initialize(total_entries, current_page, per_page=nil)
9
+ @total_entries = total_entries.to_i
10
+ self.per_page = per_page
11
+ self.current_page = current_page
12
+ end
13
+
14
+ def total_pages
15
+ (total_entries / per_page.to_f).ceil
16
+ end
17
+
18
+ def out_of_bounds?
19
+ current_page > total_pages
20
+ end
21
+
22
+ def previous_page
23
+ current_page > 1 ? (current_page - 1) : nil
24
+ end
25
+
26
+ def next_page
27
+ current_page < total_pages ? (current_page + 1) : nil
28
+ end
29
+
30
+ def skip
31
+ (current_page - 1) * per_page
32
+ end
33
+ alias offset skip
34
+
35
+ def method_missing(name, *args, &block)
36
+ @subject.send(name, *args, &block)
37
+ end
38
+
39
+ private
40
+ def per_page=(value)
41
+ value = 25 if value.blank?
42
+ @per_page = value.to_i
43
+ end
44
+
45
+ def current_page=(value)
46
+ value = value.to_i
47
+ value = 1 if value < 1
48
+ @current_page = value
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,15 @@
1
+ module MongoMapper
2
+ module RailsCompatibility
3
+ module Document
4
+ def self.included(model)
5
+ model.class_eval do
6
+ alias_method :new_record?, :new?
7
+ end
8
+ end
9
+
10
+ def to_param
11
+ id
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ module MongoMapper
2
+ module RailsCompatibility
3
+ module EmbeddedDocument
4
+ def self.included(model)
5
+ model.class_eval do
6
+ extend ClassMethods
7
+ end
8
+
9
+ class << model
10
+ alias has_many many
11
+ end
12
+ end
13
+
14
+ module ClassMethods
15
+ def column_names
16
+ keys.keys
17
+ end
18
+ end
19
+
20
+ def to_param
21
+ raise "Missing to_param method in #{self.class}. You should implement it to return the unique identifier of this embedded document within a document."
22
+ end
23
+ end
24
+ end
25
+ end
@@ -10,7 +10,7 @@ module MongoMapper #:nodoc:
10
10
  end
11
11
 
12
12
  def serializable_key_names
13
- key_names = @record.send :defined_key_names
13
+ key_names = @record.attributes.keys
14
14
 
15
15
  if options[:only]
16
16
  options.delete(:except)
@@ -50,6 +50,8 @@ module MongoMapper #:nodoc:
50
50
  # "created_at": "2006/08/01", "awesome": true,
51
51
  # "permalink": "1-konata-izumi"}
52
52
  def to_json(options = {})
53
+ apply_to_json_defaults(options)
54
+
53
55
  if include_root_in_json
54
56
  "{#{self.class.json_class_name}: #{JsonSerializer.new(self, options).to_s}}"
55
57
  else
@@ -73,5 +75,18 @@ module MongoMapper #:nodoc:
73
75
  @json_class_name ||= name.demodulize.underscore.inspect
74
76
  end
75
77
  end
78
+
79
+ private
80
+ def apply_to_json_defaults(options)
81
+ unless options[:only]
82
+ methods = [options.delete(:methods)].flatten.compact
83
+ methods << :id
84
+ options[:methods] = methods.uniq
85
+ end
86
+
87
+ except = [options.delete(:except)].flatten.compact
88
+ except << :_id
89
+ options[:except] = except
90
+ end
76
91
  end
77
92
  end
@@ -0,0 +1,30 @@
1
+ class BasicObject #:nodoc:
2
+ instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|instance_eval|proxy_|^object_id$)/ }
3
+ end unless defined?(BasicObject)
4
+
5
+ class Boolean
6
+ def self.mm_typecast(value)
7
+ ['true', 't', '1'].include?(value.to_s.downcase)
8
+ end
9
+ end
10
+
11
+ class Object
12
+ # The hidden singleton lurks behind everyone
13
+ def metaclass
14
+ class << self; self end
15
+ end
16
+
17
+ def meta_eval(&blk)
18
+ metaclass.instance_eval(&blk)
19
+ end
20
+
21
+ # Adds methods to a metaclass
22
+ def meta_def(name, &blk)
23
+ meta_eval { define_method(name, &blk) }
24
+ end
25
+
26
+ # Defines an instance method within a class
27
+ def class_def(name, &blk)
28
+ class_eval { define_method(name, &blk) }
29
+ end
30
+ end
data/mongomapper.gemspec CHANGED
@@ -1,13 +1,18 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
1
4
  # -*- encoding: utf-8 -*-
2
5
 
3
6
  Gem::Specification.new do |s|
4
7
  s.name = %q{mongomapper}
5
- s.version = "0.2.0"
8
+ s.version = "0.3.4"
6
9
 
7
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
11
  s.authors = ["John Nunemaker"]
9
- s.date = %q{2009-07-07}
12
+ s.date = %q{2009-08-28}
13
+ s.default_executable = %q{mmconsole}
10
14
  s.email = %q{nunemaker@gmail.com}
15
+ s.executables = ["mmconsole"]
11
16
  s.extra_rdoc_files = [
12
17
  "LICENSE",
13
18
  "README.rdoc"
@@ -19,86 +24,122 @@ Gem::Specification.new do |s|
19
24
  "README.rdoc",
20
25
  "Rakefile",
21
26
  "VERSION",
27
+ "bin/mmconsole",
22
28
  "lib/mongomapper.rb",
23
29
  "lib/mongomapper/associations.rb",
24
- "lib/mongomapper/associations/array_proxy.rb",
25
30
  "lib/mongomapper/associations/base.rb",
31
+ "lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb",
26
32
  "lib/mongomapper/associations/belongs_to_proxy.rb",
27
- "lib/mongomapper/associations/has_many_embedded_proxy.rb",
28
- "lib/mongomapper/associations/has_many_proxy.rb",
29
- "lib/mongomapper/associations/polymorphic_belongs_to_proxy.rb",
33
+ "lib/mongomapper/associations/many_documents_as_proxy.rb",
34
+ "lib/mongomapper/associations/many_documents_proxy.rb",
35
+ "lib/mongomapper/associations/many_embedded_polymorphic_proxy.rb",
36
+ "lib/mongomapper/associations/many_embedded_proxy.rb",
37
+ "lib/mongomapper/associations/many_polymorphic_proxy.rb",
38
+ "lib/mongomapper/associations/many_proxy.rb",
30
39
  "lib/mongomapper/associations/proxy.rb",
31
40
  "lib/mongomapper/callbacks.rb",
32
41
  "lib/mongomapper/document.rb",
42
+ "lib/mongomapper/dynamic_finder.rb",
33
43
  "lib/mongomapper/embedded_document.rb",
34
44
  "lib/mongomapper/finder_options.rb",
35
45
  "lib/mongomapper/key.rb",
36
46
  "lib/mongomapper/observing.rb",
37
- "lib/mongomapper/rails_compatibility.rb",
47
+ "lib/mongomapper/pagination.rb",
48
+ "lib/mongomapper/rails_compatibility/document.rb",
49
+ "lib/mongomapper/rails_compatibility/embedded_document.rb",
38
50
  "lib/mongomapper/save_with_validation.rb",
39
51
  "lib/mongomapper/serialization.rb",
40
52
  "lib/mongomapper/serializers/json_serializer.rb",
53
+ "lib/mongomapper/support.rb",
41
54
  "lib/mongomapper/validations.rb",
42
55
  "mongomapper.gemspec",
43
- "test/serializers/test_json_serializer.rb",
44
- "test/test_associations.rb",
45
- "test/test_callbacks.rb",
46
- "test/test_document.rb",
47
- "test/test_embedded_document.rb",
48
- "test/test_finder_options.rb",
56
+ "test/NOTE_ON_TESTING",
57
+ "test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
58
+ "test/functional/associations/test_belongs_to_proxy.rb",
59
+ "test/functional/associations/test_many_documents_as_proxy.rb",
60
+ "test/functional/associations/test_many_embedded_polymorphic_proxy.rb",
61
+ "test/functional/associations/test_many_embedded_proxy.rb",
62
+ "test/functional/associations/test_many_polymorphic_proxy.rb",
63
+ "test/functional/associations/test_many_proxy.rb",
64
+ "test/functional/test_associations.rb",
65
+ "test/functional/test_callbacks.rb",
66
+ "test/functional/test_document.rb",
67
+ "test/functional/test_pagination.rb",
68
+ "test/functional/test_rails_compatibility.rb",
69
+ "test/functional/test_validations.rb",
70
+ "test/models.rb",
49
71
  "test/test_helper.rb",
50
- "test/test_key.rb",
51
- "test/test_mongomapper.rb",
52
- "test/test_observing.rb",
53
- "test/test_rails_compatibility.rb",
54
- "test/test_serializations.rb",
55
- "test/test_validations.rb"
72
+ "test/unit/serializers/test_json_serializer.rb",
73
+ "test/unit/test_association_base.rb",
74
+ "test/unit/test_document.rb",
75
+ "test/unit/test_embedded_document.rb",
76
+ "test/unit/test_finder_options.rb",
77
+ "test/unit/test_key.rb",
78
+ "test/unit/test_mongomapper.rb",
79
+ "test/unit/test_observing.rb",
80
+ "test/unit/test_pagination.rb",
81
+ "test/unit/test_rails_compatibility.rb",
82
+ "test/unit/test_serializations.rb",
83
+ "test/unit/test_validations.rb"
56
84
  ]
57
- s.has_rdoc = true
58
85
  s.homepage = %q{http://github.com/jnunemaker/mongomapper}
59
86
  s.rdoc_options = ["--charset=UTF-8"]
60
87
  s.require_paths = ["lib"]
61
88
  s.rubyforge_project = %q{mongomapper}
62
- s.rubygems_version = %q{1.3.1}
89
+ s.rubygems_version = %q{1.3.5}
63
90
  s.summary = %q{Awesome gem for modeling your domain and storing it in mongo}
64
91
  s.test_files = [
65
- "test/serializers/test_json_serializer.rb",
66
- "test/test_associations.rb",
67
- "test/test_callbacks.rb",
68
- "test/test_document.rb",
69
- "test/test_embedded_document.rb",
70
- "test/test_finder_options.rb",
92
+ "test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
93
+ "test/functional/associations/test_belongs_to_proxy.rb",
94
+ "test/functional/associations/test_many_documents_as_proxy.rb",
95
+ "test/functional/associations/test_many_embedded_polymorphic_proxy.rb",
96
+ "test/functional/associations/test_many_embedded_proxy.rb",
97
+ "test/functional/associations/test_many_polymorphic_proxy.rb",
98
+ "test/functional/associations/test_many_proxy.rb",
99
+ "test/functional/test_associations.rb",
100
+ "test/functional/test_callbacks.rb",
101
+ "test/functional/test_document.rb",
102
+ "test/functional/test_pagination.rb",
103
+ "test/functional/test_rails_compatibility.rb",
104
+ "test/functional/test_validations.rb",
105
+ "test/models.rb",
71
106
  "test/test_helper.rb",
72
- "test/test_key.rb",
73
- "test/test_mongomapper.rb",
74
- "test/test_observing.rb",
75
- "test/test_rails_compatibility.rb",
76
- "test/test_serializations.rb",
77
- "test/test_validations.rb"
107
+ "test/unit/serializers/test_json_serializer.rb",
108
+ "test/unit/test_association_base.rb",
109
+ "test/unit/test_document.rb",
110
+ "test/unit/test_embedded_document.rb",
111
+ "test/unit/test_finder_options.rb",
112
+ "test/unit/test_key.rb",
113
+ "test/unit/test_mongomapper.rb",
114
+ "test/unit/test_observing.rb",
115
+ "test/unit/test_pagination.rb",
116
+ "test/unit/test_rails_compatibility.rb",
117
+ "test/unit/test_serializations.rb",
118
+ "test/unit/test_validations.rb"
78
119
  ]
79
120
 
80
121
  if s.respond_to? :specification_version then
81
122
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
82
- s.specification_version = 2
123
+ s.specification_version = 3
83
124
 
84
125
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
85
126
  s.add_runtime_dependency(%q<activesupport>, [">= 0"])
86
- s.add_runtime_dependency(%q<mongodb-mongo>, ["= 0.9"])
87
- s.add_runtime_dependency(%q<jnunemaker-validatable>, ["= 1.7.1"])
88
- s.add_development_dependency(%q<mocha>, ["= 0.9.4"])
89
- s.add_development_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
127
+ s.add_runtime_dependency(%q<mongodb-mongo>, [">= 0.11.1"])
128
+ s.add_runtime_dependency(%q<jnunemaker-validatable>, [">= 1.7.2"])
129
+ s.add_development_dependency(%q<mocha>, [">= 0.9.4"])
130
+ s.add_development_dependency(%q<jnunemaker-matchy>, [">= 0.4.0"])
90
131
  else
91
132
  s.add_dependency(%q<activesupport>, [">= 0"])
92
- s.add_dependency(%q<mongodb-mongo>, ["= 0.9"])
93
- s.add_dependency(%q<jnunemaker-validatable>, ["= 1.7.1"])
94
- s.add_dependency(%q<mocha>, ["= 0.9.4"])
95
- s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
133
+ s.add_dependency(%q<mongodb-mongo>, [">= 0.11.1"])
134
+ s.add_dependency(%q<jnunemaker-validatable>, [">= 1.7.2"])
135
+ s.add_dependency(%q<mocha>, [">= 0.9.4"])
136
+ s.add_dependency(%q<jnunemaker-matchy>, [">= 0.4.0"])
96
137
  end
97
138
  else
98
139
  s.add_dependency(%q<activesupport>, [">= 0"])
99
- s.add_dependency(%q<mongodb-mongo>, ["= 0.9"])
100
- s.add_dependency(%q<jnunemaker-validatable>, ["= 1.7.1"])
101
- s.add_dependency(%q<mocha>, ["= 0.9.4"])
102
- s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
140
+ s.add_dependency(%q<mongodb-mongo>, [">= 0.11.1"])
141
+ s.add_dependency(%q<jnunemaker-validatable>, [">= 1.7.2"])
142
+ s.add_dependency(%q<mocha>, [">= 0.9.4"])
143
+ s.add_dependency(%q<jnunemaker-matchy>, [">= 0.4.0"])
103
144
  end
104
145
  end
@@ -0,0 +1 @@
1
+ I am doing my best to keep unit and functional tests separate. As I see them, functional tests hit the database and should never care about internals. Unit tests do not hit the database.
@@ -0,0 +1,53 @@
1
+ require 'test_helper'
2
+ require 'models'
3
+
4
+ class BelongsToPolymorphicProxyTest < Test::Unit::TestCase
5
+ def setup
6
+ clear_all_collections
7
+ end
8
+
9
+ should "default to nil" do
10
+ status = Status.new
11
+ status.target.should be_nil
12
+ end
13
+
14
+ should "be able to replace the association" do
15
+ status = Status.new
16
+ project = Project.new(:name => "mongomapper")
17
+ status.target = project
18
+ status.save.should be_true
19
+
20
+ from_db = Status.find(status.id)
21
+ from_db.target.should_not be_nil
22
+ from_db.target_id.should == project.id
23
+ from_db.target_type.should == "Project"
24
+ from_db.target.name.should == "mongomapper"
25
+ end
26
+
27
+ should "unset the association" do
28
+ status = Status.new
29
+ project = Project.new(:name => "mongomapper")
30
+ status.target = project
31
+ status.save.should be_true
32
+
33
+ from_db = Status.find(status.id)
34
+ from_db.target = nil
35
+ from_db.target_type.should be_nil
36
+ from_db.target_id.should be_nil
37
+ from_db.target.should be_nil
38
+ end
39
+
40
+ context "association id set but document not found" do
41
+ setup do
42
+ @status = Status.new
43
+ project = Project.new(:name => "mongomapper")
44
+ @status.target = project
45
+ @status.save.should be_true
46
+ project.destroy
47
+ end
48
+
49
+ should "return nil instead of raising error" do
50
+ @status.target.should be_nil
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,45 @@
1
+ require 'test_helper'
2
+ require 'models'
3
+
4
+ class BelongsToProxyTest < Test::Unit::TestCase
5
+ def setup
6
+ clear_all_collections
7
+ end
8
+
9
+ should "default to nil" do
10
+ status = Status.new
11
+ status.project.should be_nil
12
+ end
13
+
14
+ should "be able to replace the association" do
15
+ status = Status.new
16
+ project = Project.new(:name => "mongomapper")
17
+ status.project = project
18
+ status.save.should be_true
19
+
20
+ from_db = Status.find(status.id)
21
+ from_db.project.should_not be_nil
22
+ from_db.project.name.should == "mongomapper"
23
+ end
24
+
25
+ should "unset the association" do
26
+ status = Status.new
27
+ project = Project.new(:name => "mongomapper")
28
+ status.project = project
29
+ status.save.should be_true
30
+
31
+ from_db = Status.find(status.id)
32
+ from_db.project = nil
33
+ from_db.project.should be_nil
34
+ end
35
+
36
+ context "association id set but document not found" do
37
+ setup do
38
+ @status = Status.new(:name => 'Foo', :project_id => '1234')
39
+ end
40
+
41
+ should "return nil instead of raising error" do
42
+ @status.project.should be_nil
43
+ end
44
+ end
45
+ end