mrkurt-mongo_mapper 0.6.8

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 (77) hide show
  1. data/.gitignore +10 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +38 -0
  4. data/Rakefile +55 -0
  5. data/VERSION +1 -0
  6. data/bin/mmconsole +60 -0
  7. data/lib/mongo_mapper.rb +139 -0
  8. data/lib/mongo_mapper/associations.rb +72 -0
  9. data/lib/mongo_mapper/associations/base.rb +113 -0
  10. data/lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb +26 -0
  11. data/lib/mongo_mapper/associations/belongs_to_proxy.rb +21 -0
  12. data/lib/mongo_mapper/associations/collection.rb +19 -0
  13. data/lib/mongo_mapper/associations/many_documents_as_proxy.rb +26 -0
  14. data/lib/mongo_mapper/associations/many_documents_proxy.rb +115 -0
  15. data/lib/mongo_mapper/associations/many_embedded_polymorphic_proxy.rb +31 -0
  16. data/lib/mongo_mapper/associations/many_embedded_proxy.rb +54 -0
  17. data/lib/mongo_mapper/associations/many_polymorphic_proxy.rb +11 -0
  18. data/lib/mongo_mapper/associations/one_proxy.rb +61 -0
  19. data/lib/mongo_mapper/associations/proxy.rb +111 -0
  20. data/lib/mongo_mapper/callbacks.rb +61 -0
  21. data/lib/mongo_mapper/dirty.rb +117 -0
  22. data/lib/mongo_mapper/document.rb +496 -0
  23. data/lib/mongo_mapper/dynamic_finder.rb +74 -0
  24. data/lib/mongo_mapper/embedded_document.rb +380 -0
  25. data/lib/mongo_mapper/finder_options.rb +145 -0
  26. data/lib/mongo_mapper/key.rb +36 -0
  27. data/lib/mongo_mapper/mongo_mapper.rb +125 -0
  28. data/lib/mongo_mapper/pagination.rb +66 -0
  29. data/lib/mongo_mapper/rails_compatibility/document.rb +15 -0
  30. data/lib/mongo_mapper/rails_compatibility/embedded_document.rb +28 -0
  31. data/lib/mongo_mapper/serialization.rb +54 -0
  32. data/lib/mongo_mapper/serializers/json_serializer.rb +48 -0
  33. data/lib/mongo_mapper/support.rb +192 -0
  34. data/lib/mongo_mapper/validations.rb +39 -0
  35. data/mongo_mapper.gemspec +173 -0
  36. data/specs.watchr +30 -0
  37. data/test/NOTE_ON_TESTING +1 -0
  38. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +55 -0
  39. data/test/functional/associations/test_belongs_to_proxy.rb +91 -0
  40. data/test/functional/associations/test_many_documents_as_proxy.rb +246 -0
  41. data/test/functional/associations/test_many_documents_proxy.rb +477 -0
  42. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +156 -0
  43. data/test/functional/associations/test_many_embedded_proxy.rb +192 -0
  44. data/test/functional/associations/test_many_polymorphic_proxy.rb +339 -0
  45. data/test/functional/associations/test_one_proxy.rb +131 -0
  46. data/test/functional/test_associations.rb +44 -0
  47. data/test/functional/test_binary.rb +33 -0
  48. data/test/functional/test_callbacks.rb +85 -0
  49. data/test/functional/test_dirty.rb +159 -0
  50. data/test/functional/test_document.rb +1198 -0
  51. data/test/functional/test_embedded_document.rb +135 -0
  52. data/test/functional/test_logger.rb +20 -0
  53. data/test/functional/test_modifiers.rb +242 -0
  54. data/test/functional/test_pagination.rb +95 -0
  55. data/test/functional/test_rails_compatibility.rb +25 -0
  56. data/test/functional/test_string_id_compatibility.rb +72 -0
  57. data/test/functional/test_validations.rb +361 -0
  58. data/test/models.rb +271 -0
  59. data/test/support/custom_matchers.rb +55 -0
  60. data/test/support/timing.rb +16 -0
  61. data/test/test_helper.rb +27 -0
  62. data/test/unit/associations/test_base.rb +182 -0
  63. data/test/unit/associations/test_proxy.rb +91 -0
  64. data/test/unit/serializers/test_json_serializer.rb +189 -0
  65. data/test/unit/test_document.rb +236 -0
  66. data/test/unit/test_dynamic_finder.rb +125 -0
  67. data/test/unit/test_embedded_document.rb +709 -0
  68. data/test/unit/test_finder_options.rb +325 -0
  69. data/test/unit/test_key.rb +172 -0
  70. data/test/unit/test_mongo_mapper.rb +65 -0
  71. data/test/unit/test_pagination.rb +119 -0
  72. data/test/unit/test_rails_compatibility.rb +52 -0
  73. data/test/unit/test_serializations.rb +52 -0
  74. data/test/unit/test_support.rb +346 -0
  75. data/test/unit/test_time_zones.rb +40 -0
  76. data/test/unit/test_validations.rb +503 -0
  77. metadata +239 -0
@@ -0,0 +1,48 @@
1
+ module MongoMapper
2
+ module Serialization
3
+ def self.included(base)
4
+ base.cattr_accessor :include_root_in_json, :instance_writer => false
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ def to_json(options={})
9
+ apply_to_json_defaults(options)
10
+
11
+ if include_root_in_json
12
+ "{#{self.class.json_class_name}: #{JsonSerializer.new(self, options).to_s}}"
13
+ else
14
+ JsonSerializer.new(self, options).to_s
15
+ end
16
+ end
17
+
18
+ def from_json(json)
19
+ self.attributes = ActiveSupport::JSON.decode(json)
20
+ self
21
+ end
22
+
23
+ class JsonSerializer < MongoMapper::Serialization::Serializer
24
+ def serialize
25
+ serializable_record.to_json
26
+ end
27
+ end
28
+
29
+ module ClassMethods
30
+ def json_class_name
31
+ @json_class_name ||= name.demodulize.underscore.inspect
32
+ end
33
+ end
34
+
35
+ private
36
+ def apply_to_json_defaults(options)
37
+ unless options[:only]
38
+ methods = [options.delete(:methods)].flatten.compact
39
+ methods << :id
40
+ options[:methods] = methods.uniq
41
+ end
42
+
43
+ except = [options.delete(:except)].flatten.compact
44
+ except << :_id
45
+ options[:except] = except
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,192 @@
1
+ require 'set'
2
+
3
+ class Array
4
+ def self.to_mongo(value)
5
+ value = value.respond_to?(:lines) ? value.lines : value
6
+ value.to_a
7
+ end
8
+
9
+ def self.from_mongo(value)
10
+ value || []
11
+ end
12
+ end
13
+
14
+ class Binary
15
+ def self.to_mongo(value)
16
+ if value.is_a?(ByteBuffer)
17
+ value
18
+ else
19
+ value.nil? ? nil : ByteBuffer.new(value)
20
+ end
21
+ end
22
+
23
+ def self.from_mongo(value)
24
+ value
25
+ end
26
+ end
27
+
28
+ class Boolean
29
+ def self.to_mongo(value)
30
+ if value.is_a?(Boolean)
31
+ value
32
+ else
33
+ ['true', 't', '1'].include?(value.to_s.downcase)
34
+ end
35
+ end
36
+
37
+ def self.from_mongo(value)
38
+ !!value
39
+ end
40
+ end
41
+
42
+ class Date
43
+ def self.to_mongo(value)
44
+ date = Date.parse(value.to_s)
45
+ Time.utc(date.year, date.month, date.day)
46
+ rescue
47
+ nil
48
+ end
49
+
50
+ def self.from_mongo(value)
51
+ value.to_date if value.present?
52
+ end
53
+ end
54
+
55
+ class Float
56
+ def self.to_mongo(value)
57
+ value.to_f
58
+ end
59
+ end
60
+
61
+ class Hash
62
+ def self.from_mongo(value)
63
+ HashWithIndifferentAccess.new(value || {})
64
+ end
65
+
66
+ def to_mongo
67
+ self
68
+ end
69
+ end
70
+
71
+ class Integer
72
+ def self.to_mongo(value)
73
+ value_to_i = value.to_i
74
+ if value_to_i == 0
75
+ value.to_s =~ /^(0x|0b)?0+/ ? 0 : nil
76
+ else
77
+ value_to_i
78
+ end
79
+ end
80
+ end
81
+
82
+ class NilClass
83
+ def to_mongo(value)
84
+ value
85
+ end
86
+
87
+ def from_mongo(value)
88
+ value
89
+ end
90
+ end
91
+
92
+ class Object
93
+ # The hidden singleton lurks behind everyone
94
+ def metaclass
95
+ class << self; self end
96
+ end
97
+
98
+ def meta_eval(&blk)
99
+ metaclass.instance_eval(&blk)
100
+ end
101
+
102
+ # Adds methods to a metaclass
103
+ def meta_def(name, &blk)
104
+ meta_eval { define_method(name, &blk) }
105
+ end
106
+
107
+ # Defines an instance method within a class
108
+ def class_def(name, &blk)
109
+ class_eval { define_method(name, &blk) }
110
+ end
111
+
112
+ def self.to_mongo(value)
113
+ value
114
+ end
115
+
116
+ def self.from_mongo(value)
117
+ value
118
+ end
119
+ end
120
+
121
+ class ObjectId
122
+ def self.to_mongo(value)
123
+ if value.blank?
124
+ nil
125
+ elsif value.is_a?(Mongo::ObjectID)
126
+ value
127
+ else
128
+ Mongo::ObjectID.from_string(value.to_s)
129
+ end
130
+ end
131
+
132
+ def self.from_mongo(value)
133
+ value
134
+ end
135
+ end
136
+
137
+ class Set
138
+ def self.to_mongo(value)
139
+ value.to_a
140
+ end
141
+
142
+ def self.from_mongo(value)
143
+ Set.new(value || [])
144
+ end
145
+ end
146
+
147
+ class String
148
+ def self.to_mongo(value)
149
+ value.nil? ? nil : value.to_s
150
+ end
151
+
152
+ def self.from_mongo(value)
153
+ value.nil? ? nil : value.to_s
154
+ end
155
+ end
156
+
157
+ class Symbol
158
+ %w{gt lt gte lte ne in nin mod size where exists}.each do |operator|
159
+ define_method operator do
160
+ MongoMapper::FinderOperator.new(self, "$#{operator}")
161
+ end
162
+ end
163
+
164
+ def asc; MongoMapper::OrderOperator.new(self, 'asc') end
165
+ def desc; MongoMapper::OrderOperator.new(self, 'desc') end
166
+ end
167
+
168
+ class Time
169
+ def self.to_mongo(value)
170
+ if value.nil? || value == ''
171
+ nil
172
+ else
173
+ time = MongoMapper.time_class.parse(value.to_s)
174
+ time && time.utc
175
+ end
176
+ end
177
+
178
+ def self.from_mongo(value)
179
+ if MongoMapper.use_time_zone? && value.present?
180
+ value.in_time_zone(Time.zone)
181
+ else
182
+ value
183
+ end
184
+ end
185
+ end
186
+
187
+ # TODO: Remove when patch accepted into driver
188
+ class Mongo::ObjectID
189
+ def to_json(options = nil)
190
+ %Q("#{to_s}")
191
+ end
192
+ end
@@ -0,0 +1,39 @@
1
+ module MongoMapper
2
+ module Validations
3
+ module Macros
4
+ def validates_uniqueness_of(*args)
5
+ add_validations(args, MongoMapper::Validations::ValidatesUniquenessOf)
6
+ end
7
+ end
8
+
9
+ class ValidatesUniquenessOf < Validatable::ValidationBase
10
+ option :scope, :case_sensitive
11
+ default :case_sensitive => true
12
+
13
+ def valid?(instance)
14
+ value = instance[attribute]
15
+ return true if allow_blank && value.blank?
16
+ base_conditions = case_sensitive ? {self.attribute => value} : {}
17
+ doc = instance.class.first(base_conditions.merge(scope_conditions(instance)).merge(where_conditions(instance)))
18
+ doc.nil? || instance._id == doc._id
19
+ end
20
+
21
+ def message(instance)
22
+ super || "has already been taken"
23
+ end
24
+
25
+ def scope_conditions(instance)
26
+ return {} unless scope
27
+ Array(scope).inject({}) do |conditions, key|
28
+ conditions.merge(key => instance[key])
29
+ end
30
+ end
31
+
32
+ def where_conditions(instance)
33
+ conditions = {}
34
+ conditions[attribute] = /#{instance[attribute].to_s}/i unless case_sensitive
35
+ conditions
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,173 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{mongo_mapper}
8
+ s.version = "0.6.8"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["John Nunemaker"]
12
+ s.date = %q{2009-12-15}
13
+ s.default_executable = %q{mmconsole}
14
+ s.email = %q{nunemaker@gmail.com}
15
+ s.executables = ["mmconsole"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "bin/mmconsole",
27
+ "lib/mongo_mapper.rb",
28
+ "lib/mongo_mapper/associations.rb",
29
+ "lib/mongo_mapper/associations/base.rb",
30
+ "lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb",
31
+ "lib/mongo_mapper/associations/belongs_to_proxy.rb",
32
+ "lib/mongo_mapper/associations/collection.rb",
33
+ "lib/mongo_mapper/associations/many_documents_as_proxy.rb",
34
+ "lib/mongo_mapper/associations/many_documents_proxy.rb",
35
+ "lib/mongo_mapper/associations/many_embedded_polymorphic_proxy.rb",
36
+ "lib/mongo_mapper/associations/many_embedded_proxy.rb",
37
+ "lib/mongo_mapper/associations/many_polymorphic_proxy.rb",
38
+ "lib/mongo_mapper/associations/proxy.rb",
39
+ "lib/mongo_mapper/callbacks.rb",
40
+ "lib/mongo_mapper/dirty.rb",
41
+ "lib/mongo_mapper/document.rb",
42
+ "lib/mongo_mapper/dynamic_finder.rb",
43
+ "lib/mongo_mapper/embedded_document.rb",
44
+ "lib/mongo_mapper/finder_options.rb",
45
+ "lib/mongo_mapper/key.rb",
46
+ "lib/mongo_mapper/observing.rb",
47
+ "lib/mongo_mapper/pagination.rb",
48
+ "lib/mongo_mapper/rails_compatibility/document.rb",
49
+ "lib/mongo_mapper/rails_compatibility/embedded_document.rb",
50
+ "lib/mongo_mapper/serialization.rb",
51
+ "lib/mongo_mapper/serializers/json_serializer.rb",
52
+ "lib/mongo_mapper/support.rb",
53
+ "lib/mongo_mapper/validations.rb",
54
+ "mongo_mapper.gemspec",
55
+ "specs.watchr",
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_documents_proxy.rb",
61
+ "test/functional/associations/test_many_embedded_polymorphic_proxy.rb",
62
+ "test/functional/associations/test_many_embedded_proxy.rb",
63
+ "test/functional/associations/test_many_polymorphic_proxy.rb",
64
+ "test/functional/test_associations.rb",
65
+ "test/functional/test_binary.rb",
66
+ "test/functional/test_callbacks.rb",
67
+ "test/functional/test_dirty.rb",
68
+ "test/functional/test_document.rb",
69
+ "test/functional/test_embedded_document.rb",
70
+ "test/functional/test_logger.rb",
71
+ "test/functional/test_pagination.rb",
72
+ "test/functional/test_rails_compatibility.rb",
73
+ "test/functional/test_string_id_compatibility.rb",
74
+ "test/functional/test_validations.rb",
75
+ "test/models.rb",
76
+ "test/support/custom_matchers.rb",
77
+ "test/support/timing.rb",
78
+ "test/test_helper.rb",
79
+ "test/unit/associations/test_base.rb",
80
+ "test/unit/associations/test_proxy.rb",
81
+ "test/unit/serializers/test_json_serializer.rb",
82
+ "test/unit/test_document.rb",
83
+ "test/unit/test_dynamic_finder.rb",
84
+ "test/unit/test_embedded_document.rb",
85
+ "test/unit/test_finder_options.rb",
86
+ "test/unit/test_key.rb",
87
+ "test/unit/test_mongo_mapper.rb",
88
+ "test/unit/test_observing.rb",
89
+ "test/unit/test_pagination.rb",
90
+ "test/unit/test_rails_compatibility.rb",
91
+ "test/unit/test_serializations.rb",
92
+ "test/unit/test_support.rb",
93
+ "test/unit/test_time_zones.rb",
94
+ "test/unit/test_validations.rb"
95
+ ]
96
+ s.homepage = %q{http://github.com/jnunemaker/mongomapper}
97
+ s.rdoc_options = ["--charset=UTF-8"]
98
+ s.require_paths = ["lib"]
99
+ s.rubygems_version = %q{1.3.5}
100
+ s.summary = %q{Awesome gem for modeling your domain and storing it in mongo}
101
+ s.test_files = [
102
+ "test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
103
+ "test/functional/associations/test_belongs_to_proxy.rb",
104
+ "test/functional/associations/test_many_documents_as_proxy.rb",
105
+ "test/functional/associations/test_many_documents_proxy.rb",
106
+ "test/functional/associations/test_many_embedded_polymorphic_proxy.rb",
107
+ "test/functional/associations/test_many_embedded_proxy.rb",
108
+ "test/functional/associations/test_many_polymorphic_proxy.rb",
109
+ "test/functional/test_associations.rb",
110
+ "test/functional/test_binary.rb",
111
+ "test/functional/test_callbacks.rb",
112
+ "test/functional/test_dirty.rb",
113
+ "test/functional/test_document.rb",
114
+ "test/functional/test_embedded_document.rb",
115
+ "test/functional/test_logger.rb",
116
+ "test/functional/test_pagination.rb",
117
+ "test/functional/test_rails_compatibility.rb",
118
+ "test/functional/test_string_id_compatibility.rb",
119
+ "test/functional/test_validations.rb",
120
+ "test/models.rb",
121
+ "test/support/custom_matchers.rb",
122
+ "test/support/timing.rb",
123
+ "test/test_helper.rb",
124
+ "test/unit/associations/test_base.rb",
125
+ "test/unit/associations/test_proxy.rb",
126
+ "test/unit/serializers/test_json_serializer.rb",
127
+ "test/unit/test_document.rb",
128
+ "test/unit/test_dynamic_finder.rb",
129
+ "test/unit/test_embedded_document.rb",
130
+ "test/unit/test_finder_options.rb",
131
+ "test/unit/test_key.rb",
132
+ "test/unit/test_mongo_mapper.rb",
133
+ "test/unit/test_observing.rb",
134
+ "test/unit/test_pagination.rb",
135
+ "test/unit/test_rails_compatibility.rb",
136
+ "test/unit/test_serializations.rb",
137
+ "test/unit/test_support.rb",
138
+ "test/unit/test_time_zones.rb",
139
+ "test/unit/test_validations.rb"
140
+ ]
141
+
142
+ if s.respond_to? :specification_version then
143
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
144
+ s.specification_version = 3
145
+
146
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
147
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.3"])
148
+ s.add_runtime_dependency(%q<mongo>, ["= 0.18.1"])
149
+ s.add_runtime_dependency(%q<jnunemaker-validatable>, ["= 1.8.1"])
150
+ s.add_development_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
151
+ s.add_development_dependency(%q<shoulda>, ["= 2.10.2"])
152
+ s.add_development_dependency(%q<timecop>, ["= 0.3.1"])
153
+ s.add_development_dependency(%q<mocha>, ["= 0.9.8"])
154
+ else
155
+ s.add_dependency(%q<activesupport>, [">= 2.3"])
156
+ s.add_dependency(%q<mongo>, ["= 0.18.1"])
157
+ s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.1"])
158
+ s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
159
+ s.add_dependency(%q<shoulda>, ["= 2.10.2"])
160
+ s.add_dependency(%q<timecop>, ["= 0.3.1"])
161
+ s.add_dependency(%q<mocha>, ["= 0.9.8"])
162
+ end
163
+ else
164
+ s.add_dependency(%q<activesupport>, [">= 2.3"])
165
+ s.add_dependency(%q<mongo>, ["= 0.18.1"])
166
+ s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.1"])
167
+ s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
168
+ s.add_dependency(%q<shoulda>, ["= 2.10.2"])
169
+ s.add_dependency(%q<timecop>, ["= 0.3.1"])
170
+ s.add_dependency(%q<mocha>, ["= 0.9.8"])
171
+ end
172
+ end
173
+