thorsson-mongo_mapper 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +27 -0
  3. data/bin/mmconsole +60 -0
  4. data/examples/keys.rb +40 -0
  5. data/examples/modifiers/set.rb +25 -0
  6. data/examples/plugins.rb +41 -0
  7. data/examples/querying.rb +35 -0
  8. data/examples/scopes.rb +52 -0
  9. data/lib/mongo_mapper.rb +79 -0
  10. data/lib/mongo_mapper/connection.rb +83 -0
  11. data/lib/mongo_mapper/document.rb +41 -0
  12. data/lib/mongo_mapper/embedded_document.rb +31 -0
  13. data/lib/mongo_mapper/exceptions.rb +27 -0
  14. data/lib/mongo_mapper/extensions/array.rb +19 -0
  15. data/lib/mongo_mapper/extensions/binary.rb +22 -0
  16. data/lib/mongo_mapper/extensions/boolean.rb +44 -0
  17. data/lib/mongo_mapper/extensions/date.rb +25 -0
  18. data/lib/mongo_mapper/extensions/float.rb +14 -0
  19. data/lib/mongo_mapper/extensions/hash.rb +14 -0
  20. data/lib/mongo_mapper/extensions/integer.rb +19 -0
  21. data/lib/mongo_mapper/extensions/kernel.rb +9 -0
  22. data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
  23. data/lib/mongo_mapper/extensions/object.rb +27 -0
  24. data/lib/mongo_mapper/extensions/object_id.rb +30 -0
  25. data/lib/mongo_mapper/extensions/set.rb +20 -0
  26. data/lib/mongo_mapper/extensions/string.rb +18 -0
  27. data/lib/mongo_mapper/extensions/time.rb +29 -0
  28. data/lib/mongo_mapper/plugins.rb +16 -0
  29. data/lib/mongo_mapper/plugins/accessible.rb +44 -0
  30. data/lib/mongo_mapper/plugins/associations.rb +97 -0
  31. data/lib/mongo_mapper/plugins/associations/base.rb +124 -0
  32. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +29 -0
  33. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +24 -0
  34. data/lib/mongo_mapper/plugins/associations/collection.rb +22 -0
  35. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +40 -0
  36. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +127 -0
  37. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  38. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +110 -0
  39. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -0
  40. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +24 -0
  41. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -0
  42. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +40 -0
  43. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +68 -0
  44. data/lib/mongo_mapper/plugins/associations/proxy.rb +126 -0
  45. data/lib/mongo_mapper/plugins/caching.rb +21 -0
  46. data/lib/mongo_mapper/plugins/callbacks.rb +241 -0
  47. data/lib/mongo_mapper/plugins/clone.rb +19 -0
  48. data/lib/mongo_mapper/plugins/descendants.rb +17 -0
  49. data/lib/mongo_mapper/plugins/dirty.rb +120 -0
  50. data/lib/mongo_mapper/plugins/document.rb +41 -0
  51. data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
  52. data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +44 -0
  53. data/lib/mongo_mapper/plugins/embedded_document.rb +49 -0
  54. data/lib/mongo_mapper/plugins/equality.rb +17 -0
  55. data/lib/mongo_mapper/plugins/identity_map.rb +128 -0
  56. data/lib/mongo_mapper/plugins/indexes.rb +12 -0
  57. data/lib/mongo_mapper/plugins/inspect.rb +15 -0
  58. data/lib/mongo_mapper/plugins/keys.rb +309 -0
  59. data/lib/mongo_mapper/plugins/keys/key.rb +55 -0
  60. data/lib/mongo_mapper/plugins/logger.rb +18 -0
  61. data/lib/mongo_mapper/plugins/modifiers.rb +112 -0
  62. data/lib/mongo_mapper/plugins/pagination.rb +14 -0
  63. data/lib/mongo_mapper/plugins/persistence.rb +69 -0
  64. data/lib/mongo_mapper/plugins/protected.rb +53 -0
  65. data/lib/mongo_mapper/plugins/querying.rb +176 -0
  66. data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
  67. data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
  68. data/lib/mongo_mapper/plugins/rails.rb +58 -0
  69. data/lib/mongo_mapper/plugins/safe.rb +28 -0
  70. data/lib/mongo_mapper/plugins/sci.rb +32 -0
  71. data/lib/mongo_mapper/plugins/scopes.rb +21 -0
  72. data/lib/mongo_mapper/plugins/serialization.rb +76 -0
  73. data/lib/mongo_mapper/plugins/timestamps.rb +22 -0
  74. data/lib/mongo_mapper/plugins/userstamps.rb +15 -0
  75. data/lib/mongo_mapper/plugins/validations.rb +50 -0
  76. data/lib/mongo_mapper/support/descendant_appends.rb +45 -0
  77. data/lib/mongo_mapper/version.rb +4 -0
  78. data/test/_NOTE_ON_TESTING +1 -0
  79. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
  80. data/test/functional/associations/test_belongs_to_proxy.rb +93 -0
  81. data/test/functional/associations/test_in_array_proxy.rb +319 -0
  82. data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
  83. data/test/functional/associations/test_many_documents_proxy.rb +575 -0
  84. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
  85. data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
  86. data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
  87. data/test/functional/associations/test_one_embedded_proxy.rb +67 -0
  88. data/test/functional/associations/test_one_proxy.rb +182 -0
  89. data/test/functional/test_accessible.rb +168 -0
  90. data/test/functional/test_associations.rb +44 -0
  91. data/test/functional/test_binary.rb +27 -0
  92. data/test/functional/test_caching.rb +76 -0
  93. data/test/functional/test_callbacks.rb +151 -0
  94. data/test/functional/test_dirty.rb +163 -0
  95. data/test/functional/test_document.rb +253 -0
  96. data/test/functional/test_dynamic_querying.rb +75 -0
  97. data/test/functional/test_embedded_document.rb +210 -0
  98. data/test/functional/test_identity_map.rb +506 -0
  99. data/test/functional/test_indexes.rb +42 -0
  100. data/test/functional/test_logger.rb +20 -0
  101. data/test/functional/test_modifiers.rb +416 -0
  102. data/test/functional/test_pagination.rb +91 -0
  103. data/test/functional/test_protected.rb +175 -0
  104. data/test/functional/test_querying.rb +873 -0
  105. data/test/functional/test_safe.rb +76 -0
  106. data/test/functional/test_sci.rb +230 -0
  107. data/test/functional/test_scopes.rb +171 -0
  108. data/test/functional/test_string_id_compatibility.rb +67 -0
  109. data/test/functional/test_timestamps.rb +62 -0
  110. data/test/functional/test_userstamps.rb +27 -0
  111. data/test/functional/test_validations.rb +342 -0
  112. data/test/models.rb +233 -0
  113. data/test/test_active_model_lint.rb +13 -0
  114. data/test/test_helper.rb +104 -0
  115. data/test/unit/associations/test_base.rb +212 -0
  116. data/test/unit/associations/test_proxy.rb +105 -0
  117. data/test/unit/serializers/test_json_serializer.rb +202 -0
  118. data/test/unit/test_clone.rb +69 -0
  119. data/test/unit/test_descendant_appends.rb +71 -0
  120. data/test/unit/test_document.rb +213 -0
  121. data/test/unit/test_dynamic_finder.rb +125 -0
  122. data/test/unit/test_embedded_document.rb +644 -0
  123. data/test/unit/test_extensions.rb +380 -0
  124. data/test/unit/test_key.rb +185 -0
  125. data/test/unit/test_keys.rb +55 -0
  126. data/test/unit/test_mongo_mapper.rb +110 -0
  127. data/test/unit/test_pagination.rb +11 -0
  128. data/test/unit/test_plugins.rb +50 -0
  129. data/test/unit/test_rails.rb +181 -0
  130. data/test/unit/test_rails_compatibility.rb +52 -0
  131. data/test/unit/test_serialization.rb +51 -0
  132. data/test/unit/test_time_zones.rb +39 -0
  133. data/test/unit/test_validations.rb +544 -0
  134. metadata +316 -0
@@ -0,0 +1,41 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Document
4
+ extend Support::DescendantAppends
5
+
6
+ def self.included(model)
7
+ model.class_eval do
8
+ extend Plugins
9
+
10
+ plugin Plugins::Document
11
+ plugin Plugins::Querying # for now needs to be before associations (save_to_collection)
12
+ plugin Plugins::Associations
13
+ plugin Plugins::Caching
14
+ plugin Plugins::Clone
15
+ plugin Plugins::Descendants
16
+ plugin Plugins::DynamicQuerying
17
+ plugin Plugins::Equality
18
+ plugin Plugins::Inspect
19
+ plugin Plugins::Indexes
20
+ plugin Plugins::Keys
21
+ plugin Plugins::Dirty # for now dirty needs to be after keys
22
+ plugin Plugins::Logger
23
+ plugin Plugins::Modifiers
24
+ plugin Plugins::Pagination
25
+ plugin Plugins::Persistence
26
+ plugin Plugins::Accessible
27
+ plugin Plugins::Protected
28
+ plugin Plugins::Rails
29
+ plugin Plugins::Safe # needs to be after querying (save_to_collection)
30
+ plugin Plugins::Sci
31
+ plugin Plugins::Scopes
32
+ plugin Plugins::Serialization
33
+ plugin Plugins::Timestamps
34
+ plugin Plugins::Userstamps
35
+ plugin Plugins::Validations
36
+ plugin Plugins::Callbacks # for now callbacks needs to be after validations
37
+ end
38
+ super
39
+ end
40
+ end # Document
41
+ end # MongoMapper
@@ -0,0 +1,31 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module EmbeddedDocument
4
+ extend Support::DescendantAppends
5
+
6
+ def self.included(model)
7
+ model.class_eval do
8
+ extend Plugins
9
+
10
+ plugin Plugins::EmbeddedDocument
11
+ plugin Plugins::Associations
12
+ plugin Plugins::Caching
13
+ plugin Plugins::Clone
14
+ plugin Plugins::Descendants
15
+ plugin Plugins::Equality
16
+ plugin Plugins::Inspect
17
+ plugin Plugins::Keys
18
+ plugin Plugins::Logger
19
+ plugin Plugins::Persistence
20
+ plugin Plugins::Accessible
21
+ plugin Plugins::Protected
22
+ plugin Plugins::Rails
23
+ plugin Plugins::Sci
24
+ plugin Plugins::Serialization
25
+ plugin Plugins::Validations
26
+ plugin Plugins::Callbacks
27
+ end
28
+ super
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ # generic MM error
4
+ class Error < StandardError; end
5
+
6
+ # raised when document expected but not found
7
+ class DocumentNotFound < Error; end
8
+
9
+ # raised when trying to connect using uri with incorrect scheme
10
+ class InvalidScheme < Error; end
11
+
12
+ # raised when trying to do something not supported, mostly for edocs
13
+ class NotSupported < Error; end
14
+
15
+ # raised when document not valid and using !
16
+ class DocumentNotValid < Error
17
+ def initialize(document)
18
+ super("Validation failed: #{document.errors.full_messages.join(", ")}")
19
+ end
20
+ end
21
+
22
+ class AccessibleOrProtected < Error
23
+ def initialize(name)
24
+ super("Declare either attr_protected or attr_accessible for #{name}, but not both.")
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Extensions
4
+ module Array
5
+ def to_mongo(value)
6
+ value = value.respond_to?(:lines) ? value.lines : value
7
+ value.to_a
8
+ end
9
+
10
+ def from_mongo(value)
11
+ value || []
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ class Array
18
+ extend MongoMapper::Extensions::Array
19
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Extensions
4
+ module Binary
5
+ def to_mongo(value)
6
+ if value.is_a?(::BSON::Binary)
7
+ value
8
+ else
9
+ value.nil? ? nil : ::BSON::Binary.new(value)
10
+ end
11
+ end
12
+
13
+ def from_mongo(value)
14
+ value
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ class Binary
21
+ extend MongoMapper::Extensions::Binary
22
+ end
@@ -0,0 +1,44 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Extensions
4
+ module Boolean
5
+ Mapping = {
6
+ true => true,
7
+ 'true' => true,
8
+ 'TRUE' => true,
9
+ 'True' => true,
10
+ 't' => true,
11
+ 'T' => true,
12
+ '1' => true,
13
+ 1 => true,
14
+ 1.0 => true,
15
+ false => false,
16
+ 'false' => false,
17
+ 'FALSE' => false,
18
+ 'False' => false,
19
+ 'f' => false,
20
+ 'F' => false,
21
+ '0' => false,
22
+ 0 => false,
23
+ 0.0 => false,
24
+ nil => nil
25
+ }
26
+
27
+ def to_mongo(value)
28
+ if value.is_a?(Boolean)
29
+ value
30
+ else
31
+ Mapping[value]
32
+ end
33
+ end
34
+
35
+ def from_mongo(value)
36
+ value.nil? ? nil : !!value
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ class Boolean
43
+ extend MongoMapper::Extensions::Boolean
44
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Extensions
4
+ module Date
5
+ def to_mongo(value)
6
+ if value.nil? || value == ''
7
+ nil
8
+ else
9
+ date = value.is_a?(::Date) || value.is_a?(::Time) ? value : ::Date.parse(value.to_s)
10
+ ::Time.utc(date.year, date.month, date.day)
11
+ end
12
+ rescue
13
+ nil
14
+ end
15
+
16
+ def from_mongo(value)
17
+ value.to_date if value.present?
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ class Date
24
+ extend MongoMapper::Extensions::Date
25
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Extensions
4
+ module Float
5
+ def to_mongo(value)
6
+ value.nil? ? nil : value.to_f
7
+ end
8
+ end
9
+ end
10
+ end
11
+
12
+ class Float
13
+ extend MongoMapper::Extensions::Float
14
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Extensions
4
+ module Hash
5
+ def from_mongo(value)
6
+ HashWithIndifferentAccess.new(value || {})
7
+ end
8
+ end
9
+ end
10
+ end
11
+
12
+ class Hash
13
+ extend MongoMapper::Extensions::Hash
14
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Extensions
4
+ module Integer
5
+ def to_mongo(value)
6
+ value_to_i = value.to_i
7
+ if value_to_i == 0 && value != value_to_i
8
+ value.to_s =~ /^(0x|0b)?0+/ ? 0 : nil
9
+ else
10
+ value_to_i
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ class Integer
18
+ extend MongoMapper::Extensions::Integer
19
+ end
@@ -0,0 +1,9 @@
1
+ # encoding: UTF-8
2
+ module Kernel
3
+ # Returns the object's singleton class.
4
+ def singleton_class
5
+ class << self
6
+ self
7
+ end
8
+ end unless respond_to?(:singleton_class) # exists in 1.9.2
9
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Extensions
4
+ module NilClass
5
+ def to_mongo(value)
6
+ value
7
+ end
8
+
9
+ def from_mongo(value)
10
+ value
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ class NilClass
17
+ include MongoMapper::Extensions::NilClass
18
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Extensions
4
+ module Object
5
+ module ClassMethods
6
+ def to_mongo(value)
7
+ value
8
+ end
9
+
10
+ def from_mongo(value)
11
+ value
12
+ end
13
+ end
14
+
15
+ module InstanceMethods
16
+ def to_mongo
17
+ self.class.to_mongo(self)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ class Object
25
+ extend MongoMapper::Extensions::Object::ClassMethods
26
+ include MongoMapper::Extensions::Object::InstanceMethods
27
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Extensions
4
+ module ObjectId
5
+ def to_mongo(value)
6
+ Plucky.to_object_id(value)
7
+ end
8
+
9
+ def from_mongo(value)
10
+ value
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ class ObjectId
17
+ extend MongoMapper::Extensions::ObjectId
18
+ end
19
+
20
+ class BSON::ObjectID
21
+ alias_method :original_to_json, :to_json
22
+
23
+ def as_json(options=nil)
24
+ to_s
25
+ end
26
+
27
+ def to_json(options = nil)
28
+ as_json.to_json
29
+ end
30
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: UTF-8
2
+ require 'set'
3
+
4
+ module MongoMapper
5
+ module Extensions
6
+ module Set
7
+ def to_mongo(value)
8
+ value.to_a
9
+ end
10
+
11
+ def from_mongo(value)
12
+ (value || []).to_set
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ class Set
19
+ extend MongoMapper::Extensions::Set
20
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Extensions
4
+ module String
5
+ def to_mongo(value)
6
+ value.nil? ? nil : value.to_s
7
+ end
8
+
9
+ def from_mongo(value)
10
+ value.nil? ? nil : value.to_s
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ class String
17
+ extend MongoMapper::Extensions::String
18
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Extensions
4
+ module Time
5
+ def to_mongo(value)
6
+ if value.nil? || value == ''
7
+ nil
8
+ else
9
+ time_class = ::Time.try(:zone).present? ? ::Time.zone : ::Time
10
+ time = value.is_a?(::Time) ? value : time_class.parse(value.to_s)
11
+ # strip milliseconds as Ruby does micro and bson does milli and rounding rounded wrong
12
+ at(time.to_i).utc if time
13
+ end
14
+ end
15
+
16
+ def from_mongo(value)
17
+ if ::Time.try(:zone).present? && value.present?
18
+ value.in_time_zone(::Time.zone)
19
+ else
20
+ value
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ class Time
28
+ extend MongoMapper::Extensions::Time
29
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ def plugins
5
+ @plugins ||= []
6
+ end
7
+
8
+ def plugin(mod)
9
+ #extend mod::ClassMethods if mod.const_defined?(:ClassMethods)
10
+ extend mod::ClassMethods if mod.constants.include?(:ClassMethods)
11
+ include mod::InstanceMethods if mod.const_defined?(:InstanceMethods)
12
+ mod.configure(self) if mod.respond_to?(:configure)
13
+ plugins << mod
14
+ end
15
+ end
16
+ end