mongomodel 0.5.5 → 0.5.6
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.
- checksums.yaml +5 -5
- data/.travis.yml +23 -11
- data/gemfiles/rails-3.2.gemfile +2 -2
- data/gemfiles/rails-4-observers.gemfile +1 -1
- data/gemfiles/rails-4.1.gemfile +2 -2
- data/gemfiles/{mongoid.gemfile → rails-4.2.gemfile} +2 -3
- data/gemfiles/{mongo_mapper.gemfile → rails-5.0.gemfile} +2 -3
- data/gemfiles/rails-5.1.gemfile +10 -0
- data/gemfiles/rails-5.2.gemfile +10 -0
- data/lib/mongomodel.rb +15 -15
- data/lib/mongomodel/attributes/mongo.rb +7 -7
- data/lib/mongomodel/attributes/store.rb +4 -4
- data/lib/mongomodel/attributes/typecasting.rb +7 -7
- data/lib/mongomodel/compatibility/mongo_mapper.rb +3 -3
- data/lib/mongomodel/compatibility/mongoid.rb +3 -3
- data/lib/mongomodel/concerns/abstract_class.rb +3 -3
- data/lib/mongomodel/concerns/activemodel.rb +4 -4
- data/lib/mongomodel/concerns/associations.rb +8 -8
- data/lib/mongomodel/concerns/associations/base/association.rb +5 -5
- data/lib/mongomodel/concerns/associations/base/definition.rb +4 -4
- data/lib/mongomodel/concerns/associations/base/proxy.rb +1 -1
- data/lib/mongomodel/concerns/associations/belongs_to.rb +19 -19
- data/lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb +36 -36
- data/lib/mongomodel/concerns/associations/has_many_by_ids.rb +34 -34
- data/lib/mongomodel/concerns/attribute_methods.rb +10 -10
- data/lib/mongomodel/concerns/attribute_methods/before_type_cast.rb +4 -4
- data/lib/mongomodel/concerns/attribute_methods/dirty.rb +95 -13
- data/lib/mongomodel/concerns/attribute_methods/forbidden.rb +1 -1
- data/lib/mongomodel/concerns/attribute_methods/multi_parameter_assignment.rb +6 -6
- data/lib/mongomodel/concerns/attribute_methods/nested.rb +18 -18
- data/lib/mongomodel/concerns/attribute_methods/protected.rb +3 -3
- data/lib/mongomodel/concerns/attribute_methods/query.rb +3 -3
- data/lib/mongomodel/concerns/attribute_methods/read.rb +4 -4
- data/lib/mongomodel/concerns/attribute_methods/write.rb +4 -4
- data/lib/mongomodel/concerns/attributes.rb +18 -18
- data/lib/mongomodel/concerns/callbacks.rb +7 -7
- data/lib/mongomodel/concerns/document_parent.rb +2 -2
- data/lib/mongomodel/concerns/logging.rb +2 -2
- data/lib/mongomodel/concerns/map_reduce.rb +11 -11
- data/lib/mongomodel/concerns/pretty_inspect.rb +3 -3
- data/lib/mongomodel/concerns/properties.rb +18 -18
- data/lib/mongomodel/concerns/record_status.rb +9 -13
- data/lib/mongomodel/concerns/serialization.rb +4 -4
- data/lib/mongomodel/concerns/timestamps.rb +4 -4
- data/lib/mongomodel/concerns/translation.rb +2 -2
- data/lib/mongomodel/concerns/validations.rb +5 -5
- data/lib/mongomodel/concerns/validations/associated.rb +1 -1
- data/lib/mongomodel/document.rb +6 -6
- data/lib/mongomodel/document/callbacks.rb +15 -21
- data/lib/mongomodel/document/collection_modifiers.rb +5 -5
- data/lib/mongomodel/document/dynamic_finders.rb +1 -1
- data/lib/mongomodel/document/indexes.rb +19 -19
- data/lib/mongomodel/document/optimistic_locking.rb +7 -7
- data/lib/mongomodel/document/persistence.rb +23 -23
- data/lib/mongomodel/document/scopes.rb +20 -20
- data/lib/mongomodel/document/validations.rb +6 -6
- data/lib/mongomodel/document/validations/uniqueness.rb +11 -11
- data/lib/mongomodel/embedded_document.rb +11 -11
- data/lib/mongomodel/locale/en.yml +0 -1
- data/lib/mongomodel/log_subscriber.rb +5 -5
- data/lib/mongomodel/railtie.rb +13 -13
- data/lib/mongomodel/support/collection.rb +31 -31
- data/lib/mongomodel/support/configuration.rb +11 -11
- data/lib/mongomodel/support/core_extensions.rb +1 -1
- data/lib/mongomodel/support/dynamic_finder.rb +12 -12
- data/lib/mongomodel/support/exceptions.rb +6 -6
- data/lib/mongomodel/support/instrumented_collection.rb +20 -20
- data/lib/mongomodel/support/map.rb +33 -33
- data/lib/mongomodel/support/mongo_operator.rb +6 -6
- data/lib/mongomodel/support/mongo_options.rb +18 -18
- data/lib/mongomodel/support/mongo_order.rb +16 -16
- data/lib/mongomodel/support/paginator.rb +8 -8
- data/lib/mongomodel/support/reference.rb +10 -10
- data/lib/mongomodel/support/scope.rb +37 -37
- data/lib/mongomodel/support/scope/array_methods.rb +1 -1
- data/lib/mongomodel/support/scope/batches.rb +1 -1
- data/lib/mongomodel/support/scope/dynamic_finders.rb +1 -1
- data/lib/mongomodel/support/scope/finder_methods.rb +7 -7
- data/lib/mongomodel/support/scope/load_methods.rb +1 -1
- data/lib/mongomodel/support/scope/pagination.rb +1 -1
- data/lib/mongomodel/support/scope/query_methods.rb +6 -6
- data/lib/mongomodel/support/scope/spawn_methods.rb +8 -8
- data/lib/mongomodel/support/types.rb +2 -2
- data/lib/mongomodel/support/types/array.rb +1 -1
- data/lib/mongomodel/support/types/boolean.rb +3 -3
- data/lib/mongomodel/support/types/custom.rb +3 -3
- data/lib/mongomodel/support/types/date.rb +2 -2
- data/lib/mongomodel/support/types/date_time.rb +6 -16
- data/lib/mongomodel/support/types/float.rb +1 -1
- data/lib/mongomodel/support/types/hash.rb +1 -1
- data/lib/mongomodel/support/types/integer.rb +13 -10
- data/lib/mongomodel/support/types/object.rb +5 -5
- data/lib/mongomodel/support/types/rational.rb +3 -3
- data/lib/mongomodel/support/types/time.rb +2 -2
- data/lib/mongomodel/version.rb +1 -1
- data/lib/rails/generators/mongo_model/config/templates/mongomodel.yml +3 -4
- data/lib/rails/generators/mongo_model/model/model_generator.rb +3 -3
- data/mongomodel.gemspec +5 -4
- data/spec/mongomodel/attributes/store_spec.rb +21 -21
- data/spec/mongomodel/concerns/activemodel_spec.rb +4 -4
- data/spec/mongomodel/concerns/associations/base/association_spec.rb +12 -12
- data/spec/mongomodel/concerns/associations/belongs_to_spec.rb +34 -21
- data/spec/mongomodel/concerns/associations/has_many_by_foreign_key_spec.rb +53 -53
- data/spec/mongomodel/concerns/associations/has_many_by_ids_spec.rb +30 -30
- data/spec/mongomodel/concerns/attribute_methods/before_type_cast_spec.rb +7 -7
- data/spec/mongomodel/concerns/attribute_methods/dirty_spec.rb +45 -41
- data/spec/mongomodel/concerns/attribute_methods/multi_parameter_assignment_spec.rb +7 -7
- data/spec/mongomodel/concerns/attribute_methods/nested_spec.rb +31 -31
- data/spec/mongomodel/concerns/attribute_methods/protected_spec.rb +15 -15
- data/spec/mongomodel/concerns/attribute_methods/query_spec.rb +19 -19
- data/spec/mongomodel/concerns/attribute_methods/read_spec.rb +9 -9
- data/spec/mongomodel/concerns/attribute_methods/write_spec.rb +6 -6
- data/spec/mongomodel/concerns/attribute_methods_spec.rb +13 -13
- data/spec/mongomodel/concerns/attributes_spec.rb +34 -34
- data/spec/mongomodel/concerns/callbacks_spec.rb +25 -21
- data/spec/mongomodel/concerns/logging_spec.rb +5 -3
- data/spec/mongomodel/concerns/map_reduce_spec.rb +19 -19
- data/spec/mongomodel/concerns/observing_spec.rb +3 -3
- data/spec/mongomodel/concerns/pretty_inspect_spec.rb +10 -10
- data/spec/mongomodel/concerns/properties_spec.rb +11 -11
- data/spec/mongomodel/concerns/serialization/json_serialization_spec.rb +13 -13
- data/spec/mongomodel/concerns/timestamps_spec.rb +39 -39
- data/spec/mongomodel/concerns/validations_spec.rb +41 -38
- data/spec/mongomodel/document/callbacks_spec.rb +20 -16
- data/spec/mongomodel/document/collection_modifiers_spec.rb +16 -16
- data/spec/mongomodel/document/dynamic_finders_spec.rb +46 -46
- data/spec/mongomodel/document/finders_spec.rb +15 -15
- data/spec/mongomodel/document/indexes_spec.rb +29 -29
- data/spec/mongomodel/document/optimistic_locking_spec.rb +16 -16
- data/spec/mongomodel/document/persistence_spec.rb +39 -39
- data/spec/mongomodel/document/scopes_spec.rb +17 -17
- data/spec/mongomodel/document/validations/uniqueness_spec.rb +46 -46
- data/spec/mongomodel/document/validations_spec.rb +35 -35
- data/spec/mongomodel/document_spec.rb +19 -19
- data/spec/mongomodel/embedded_document_spec.rb +19 -19
- data/spec/mongomodel/mongomodel_spec.rb +7 -6
- data/spec/mongomodel/support/collection_spec.rb +54 -54
- data/spec/mongomodel/support/configuration_spec.rb +1 -1
- data/spec/mongomodel/support/map_spec.rb +66 -66
- data/spec/mongomodel/support/mongo_operator_spec.rb +5 -5
- data/spec/mongomodel/support/mongo_options_spec.rb +42 -42
- data/spec/mongomodel/support/mongo_order_spec.rb +24 -24
- data/spec/mongomodel/support/paginator_spec.rb +15 -15
- data/spec/mongomodel/support/property_spec.rb +29 -23
- data/spec/mongomodel/support/scope_spec.rb +205 -204
- data/spec/spec_helper.rb +13 -2
- data/spec/support/callbacks.rb +3 -8
- data/spec/support/helpers/define_class.rb +7 -7
- data/spec/support/helpers/document_finder_stubs.rb +6 -6
- data/spec/support/helpers/specs_for.rb +1 -1
- data/spec/support/helpers/validations.rb +1 -1
- data/spec/support/matchers/find_with.rb +8 -8
- data/spec/support/matchers/respond_to_boolean.rb +3 -3
- data/spec/support/matchers/run_callbacks.rb +6 -9
- data/spec/support/models.rb +5 -5
- metadata +23 -7
|
@@ -2,23 +2,23 @@ module MongoModel
|
|
|
2
2
|
module AttributeMethods
|
|
3
3
|
module Write
|
|
4
4
|
extend ActiveSupport::Concern
|
|
5
|
-
|
|
5
|
+
|
|
6
6
|
included do
|
|
7
7
|
attribute_method_suffix "="
|
|
8
8
|
end
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
# Updates the attribute identified by <tt>name</tt> with the specified +value+.
|
|
11
11
|
# Values are typecast to the appropriate type determined by the property.
|
|
12
12
|
def write_attribute(name, value)
|
|
13
13
|
attributes[name.to_sym] = value
|
|
14
14
|
end
|
|
15
|
-
|
|
15
|
+
|
|
16
16
|
# Updates the attribute identified by <tt>name</tt> with the specified +value+.
|
|
17
17
|
# (Alias for the protected write_attribute method).
|
|
18
18
|
def []=(name, value)
|
|
19
19
|
write_attribute(name, value)
|
|
20
20
|
end
|
|
21
|
-
|
|
21
|
+
|
|
22
22
|
private
|
|
23
23
|
# Handle *= for method_missing.
|
|
24
24
|
def attribute=(attribute_name, value)
|
|
@@ -3,19 +3,19 @@ require 'active_support/core_ext/module/aliasing'
|
|
|
3
3
|
module MongoModel
|
|
4
4
|
module Attributes
|
|
5
5
|
extend ActiveSupport::Concern
|
|
6
|
-
|
|
6
|
+
|
|
7
7
|
def initialize(attrs={}, options={})
|
|
8
8
|
assign_attributes(attrs || {}, options)
|
|
9
9
|
yield self if block_given?
|
|
10
10
|
end
|
|
11
|
-
|
|
11
|
+
|
|
12
12
|
def attributes
|
|
13
13
|
@attributes ||= Attributes::Store.new(self)
|
|
14
14
|
end
|
|
15
|
-
|
|
15
|
+
|
|
16
16
|
def assign_attributes(attrs, options={})
|
|
17
17
|
return unless attrs
|
|
18
|
-
|
|
18
|
+
|
|
19
19
|
attrs.each do |attr, value|
|
|
20
20
|
if respond_to?("#{attr}=")
|
|
21
21
|
send("#{attr}=", value)
|
|
@@ -24,51 +24,51 @@ module MongoModel
|
|
|
24
24
|
end
|
|
25
25
|
end
|
|
26
26
|
end
|
|
27
|
-
|
|
27
|
+
|
|
28
28
|
def attributes=(attrs)
|
|
29
29
|
assign_attributes(attrs)
|
|
30
30
|
end
|
|
31
|
-
|
|
31
|
+
|
|
32
32
|
def freeze
|
|
33
33
|
attributes.freeze; self
|
|
34
34
|
end
|
|
35
|
-
|
|
35
|
+
|
|
36
36
|
def frozen?
|
|
37
37
|
attributes.frozen?
|
|
38
38
|
end
|
|
39
|
-
|
|
39
|
+
|
|
40
40
|
# Returns duplicated record with unfreezed attributes.
|
|
41
41
|
def dup
|
|
42
42
|
obj = super
|
|
43
43
|
obj.instance_variable_set('@attributes', instance_variable_get('@attributes').dup)
|
|
44
44
|
obj
|
|
45
45
|
end
|
|
46
|
-
|
|
46
|
+
|
|
47
47
|
def to_mongo
|
|
48
48
|
attributes.to_mongo
|
|
49
49
|
end
|
|
50
|
-
|
|
50
|
+
|
|
51
51
|
def embedded_documents
|
|
52
52
|
docs = []
|
|
53
|
-
|
|
53
|
+
|
|
54
54
|
docs.concat attributes.values.select { |attr| attr.is_a?(EmbeddedDocument) }
|
|
55
|
-
|
|
55
|
+
|
|
56
56
|
attributes.values.select { |attr| attr.is_a?(Collection) }.each do |collection|
|
|
57
57
|
docs.concat collection.embedded_documents
|
|
58
58
|
end
|
|
59
|
-
|
|
59
|
+
|
|
60
60
|
attributes.values.select { |attr| attr.is_a?(Map) && attr.to <= EmbeddedDocument }.each do |map|
|
|
61
61
|
docs.concat map.values
|
|
62
62
|
end
|
|
63
|
-
|
|
63
|
+
|
|
64
64
|
docs
|
|
65
65
|
end
|
|
66
|
-
|
|
66
|
+
|
|
67
67
|
protected
|
|
68
68
|
def sanitize_for_mass_assignment(attrs, options={})
|
|
69
69
|
attrs
|
|
70
70
|
end
|
|
71
|
-
|
|
71
|
+
|
|
72
72
|
module ClassMethods
|
|
73
73
|
def from_mongo(hash)
|
|
74
74
|
if hash
|
|
@@ -77,11 +77,11 @@ module MongoModel
|
|
|
77
77
|
doc
|
|
78
78
|
end
|
|
79
79
|
end
|
|
80
|
-
|
|
80
|
+
|
|
81
81
|
private
|
|
82
82
|
def class_for_type(type)
|
|
83
83
|
klass = type.constantize
|
|
84
|
-
|
|
84
|
+
|
|
85
85
|
if klass.ancestors.include?(self)
|
|
86
86
|
klass
|
|
87
87
|
else
|
|
@@ -20,7 +20,7 @@ module MongoModel
|
|
|
20
20
|
# * (6) <tt>after_save</tt>
|
|
21
21
|
#
|
|
22
22
|
# That's a total of eight callbacks, which gives you immense power to react and prepare for each state in the
|
|
23
|
-
# MongoModel lifecycle. The sequence for calling <tt>Document#save</tt> for an existing record is similar, except that each
|
|
23
|
+
# MongoModel lifecycle. The sequence for calling <tt>Document#save</tt> for an existing record is similar, except that each
|
|
24
24
|
# <tt>_on_create</tt> callback is replaced by the corresponding <tt>_on_update</tt> callback.
|
|
25
25
|
#
|
|
26
26
|
# Examples:
|
|
@@ -199,23 +199,23 @@ module MongoModel
|
|
|
199
199
|
:after_create, :before_update, :around_update, :after_update,
|
|
200
200
|
:before_destroy, :around_destroy, :after_destroy
|
|
201
201
|
]
|
|
202
|
-
|
|
202
|
+
|
|
203
203
|
module ClassMethods
|
|
204
204
|
include ActiveModel::Callbacks
|
|
205
205
|
end
|
|
206
|
-
|
|
206
|
+
|
|
207
207
|
included do
|
|
208
208
|
include ActiveModel::Validations::Callbacks
|
|
209
|
-
|
|
209
|
+
|
|
210
210
|
define_model_callbacks :initialize, :find, :only => :after
|
|
211
211
|
define_model_callbacks :save, :create, :update, :destroy
|
|
212
212
|
end
|
|
213
|
-
|
|
213
|
+
|
|
214
214
|
def initialize(*args, &block) #:nodoc:
|
|
215
215
|
super
|
|
216
216
|
run_callbacks_with_embedded(:initialize)
|
|
217
217
|
end
|
|
218
|
-
|
|
218
|
+
|
|
219
219
|
def run_callbacks_with_embedded(kind, *args, &block)
|
|
220
220
|
if block_given?
|
|
221
221
|
embedded_callbacks = nest_embedded_callbacks(kind, *args, &block)
|
|
@@ -225,7 +225,7 @@ module MongoModel
|
|
|
225
225
|
embedded_documents.each { |doc| doc.run_callbacks(kind, *args) } unless kind == :initialize
|
|
226
226
|
end
|
|
227
227
|
end
|
|
228
|
-
|
|
228
|
+
|
|
229
229
|
private
|
|
230
230
|
def nest_embedded_callbacks(kind, *args, &block)
|
|
231
231
|
embedded_documents.inject(block) do |callback, doc|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
module MongoModel
|
|
2
2
|
module DocumentParent
|
|
3
3
|
extend ActiveSupport::Concern
|
|
4
|
-
|
|
4
|
+
|
|
5
5
|
def parent_document
|
|
6
6
|
if @_parent_document.is_a?(Proc)
|
|
7
7
|
case @_parent_document.arity
|
|
@@ -12,7 +12,7 @@ module MongoModel
|
|
|
12
12
|
@_parent_document
|
|
13
13
|
end
|
|
14
14
|
end
|
|
15
|
-
|
|
15
|
+
|
|
16
16
|
def parent_document=(doc)
|
|
17
17
|
@_parent_document = doc
|
|
18
18
|
end
|
|
@@ -1,45 +1,45 @@
|
|
|
1
1
|
module MongoModel::MapReduce
|
|
2
2
|
extend ActiveSupport::Concern
|
|
3
|
-
|
|
3
|
+
|
|
4
4
|
include MongoModel::DocumentExtensions::Scopes
|
|
5
|
-
|
|
5
|
+
|
|
6
6
|
included do
|
|
7
7
|
class_attribute :parent_collection
|
|
8
8
|
end
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
module ClassMethods
|
|
11
11
|
def from_mongo(attrs)
|
|
12
12
|
new(attrs['_id'], attrs['value'])
|
|
13
13
|
end
|
|
14
|
-
|
|
14
|
+
|
|
15
15
|
def cached
|
|
16
16
|
from(collection_name)
|
|
17
17
|
end
|
|
18
|
-
|
|
18
|
+
|
|
19
19
|
def database
|
|
20
20
|
parent_collection.db
|
|
21
21
|
end
|
|
22
|
-
|
|
22
|
+
|
|
23
23
|
def collection
|
|
24
24
|
parent_collection.map_reduce(map_function, reduce_function, map_reduce_options)
|
|
25
25
|
end
|
|
26
|
-
|
|
26
|
+
|
|
27
27
|
def collection_name
|
|
28
28
|
@_collection_name || default_collection_name
|
|
29
29
|
end
|
|
30
|
-
|
|
30
|
+
|
|
31
31
|
def collection_name=(name)
|
|
32
32
|
@_collection_name = name
|
|
33
33
|
end
|
|
34
|
-
|
|
34
|
+
|
|
35
35
|
def default_collection_name
|
|
36
36
|
[parent_collection.name, name.demodulize.tableize.gsub(/\//, '.')].join("._")
|
|
37
37
|
end
|
|
38
|
-
|
|
38
|
+
|
|
39
39
|
def map_function
|
|
40
40
|
raise NotImplementedError, "map_function is not implemented"
|
|
41
41
|
end
|
|
42
|
-
|
|
42
|
+
|
|
43
43
|
def reduce_function
|
|
44
44
|
raise NotImplementedError, "reduce_function is not implemented"
|
|
45
45
|
end
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
module MongoModel
|
|
2
2
|
module PrettyInspect
|
|
3
3
|
extend ActiveSupport::Concern
|
|
4
|
-
|
|
4
|
+
|
|
5
5
|
module ClassMethods
|
|
6
6
|
# Returns a string like 'Post(title:String, body:String)'
|
|
7
7
|
def inspect
|
|
@@ -13,12 +13,12 @@ module MongoModel
|
|
|
13
13
|
end
|
|
14
14
|
end
|
|
15
15
|
end
|
|
16
|
-
|
|
16
|
+
|
|
17
17
|
# Returns the contents of the document as a nicely formatted string.
|
|
18
18
|
def inspect
|
|
19
19
|
"#<#{self.class.name} #{attributes_for_inspect}>"
|
|
20
20
|
end
|
|
21
|
-
|
|
21
|
+
|
|
22
22
|
private
|
|
23
23
|
def attributes_for_inspect
|
|
24
24
|
attrs = self.class.model_properties.map { |name, property| "#{name}: #{send(name).inspect}" }
|
|
@@ -6,57 +6,57 @@ require 'active_support/core_ext/hash/except'
|
|
|
6
6
|
module MongoModel
|
|
7
7
|
module Properties
|
|
8
8
|
extend ActiveSupport::Concern
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
def properties
|
|
11
11
|
self.class.properties
|
|
12
12
|
end
|
|
13
|
-
|
|
13
|
+
|
|
14
14
|
module ClassMethods
|
|
15
15
|
def property(name, type, options={})
|
|
16
16
|
properties[name.to_sym] = Property.new(name, type, options).tap do |property|
|
|
17
17
|
include type.mongomodel_accessors(property) if type.respond_to?(:mongomodel_accessors)
|
|
18
18
|
end
|
|
19
19
|
end
|
|
20
|
-
|
|
20
|
+
|
|
21
21
|
def properties
|
|
22
22
|
@properties ||= ActiveSupport::OrderedHash.new
|
|
23
23
|
end
|
|
24
|
-
|
|
24
|
+
|
|
25
25
|
def properties=(properties)
|
|
26
26
|
@properties = properties
|
|
27
27
|
end
|
|
28
|
-
|
|
28
|
+
|
|
29
29
|
def model_properties
|
|
30
30
|
properties.reject { |k, p| p.internal? }
|
|
31
31
|
end
|
|
32
|
-
|
|
32
|
+
|
|
33
33
|
def internal_properties
|
|
34
34
|
properties.select { |k, p| p.internal? }.map { |k, p| p }
|
|
35
35
|
end
|
|
36
|
-
|
|
36
|
+
|
|
37
37
|
def inherited(subclass)
|
|
38
38
|
super
|
|
39
39
|
subclass.properties = properties.dup
|
|
40
40
|
end
|
|
41
41
|
end
|
|
42
|
-
|
|
42
|
+
|
|
43
43
|
class Property
|
|
44
44
|
delegate :cast, :boolean, :to_mongo, :from_mongo, :to_query, :to => :type_converter
|
|
45
|
-
|
|
45
|
+
|
|
46
46
|
attr_reader :name, :type, :options
|
|
47
|
-
|
|
47
|
+
|
|
48
48
|
def initialize(name, type, options={})
|
|
49
49
|
@name, @type, @options = name.to_sym, type, options
|
|
50
50
|
end
|
|
51
|
-
|
|
51
|
+
|
|
52
52
|
def as
|
|
53
53
|
options[:as] || name.to_s
|
|
54
54
|
end
|
|
55
|
-
|
|
55
|
+
|
|
56
56
|
def default(instance)
|
|
57
57
|
if options.key?(:default)
|
|
58
58
|
default = options[:default]
|
|
59
|
-
|
|
59
|
+
|
|
60
60
|
if default.is_a?(Proc)
|
|
61
61
|
case default.arity
|
|
62
62
|
when 0 then instance.instance_exec(&default)
|
|
@@ -69,23 +69,23 @@ module MongoModel
|
|
|
69
69
|
type.mongomodel_default(instance)
|
|
70
70
|
end
|
|
71
71
|
end
|
|
72
|
-
|
|
72
|
+
|
|
73
73
|
def ==(other)
|
|
74
74
|
other.is_a?(self.class) && name == other.name && type == other.type && options == other.options
|
|
75
75
|
end
|
|
76
|
-
|
|
76
|
+
|
|
77
77
|
def embeddable?
|
|
78
78
|
type.ancestors.include?(EmbeddedDocument)
|
|
79
79
|
end
|
|
80
|
-
|
|
80
|
+
|
|
81
81
|
def internal?
|
|
82
82
|
as =~ /^_/ || options[:internal]
|
|
83
83
|
end
|
|
84
|
-
|
|
84
|
+
|
|
85
85
|
def validate?
|
|
86
86
|
options[:validate] != false
|
|
87
87
|
end
|
|
88
|
-
|
|
88
|
+
|
|
89
89
|
private
|
|
90
90
|
def type_converter
|
|
91
91
|
@type_converter ||= Types.converter_for(type)
|
|
@@ -3,35 +3,31 @@ require 'active_support/core_ext/module/aliasing'
|
|
|
3
3
|
module MongoModel
|
|
4
4
|
module RecordStatus
|
|
5
5
|
extend ActiveSupport::Concern
|
|
6
|
-
|
|
7
|
-
included do
|
|
8
|
-
alias_method_chain :initialize, :record_status
|
|
9
|
-
end
|
|
10
|
-
|
|
6
|
+
|
|
11
7
|
def new_record?
|
|
12
8
|
@_new_record
|
|
13
9
|
end
|
|
14
|
-
|
|
10
|
+
|
|
15
11
|
def destroyed?
|
|
16
12
|
@_destroyed
|
|
17
13
|
end
|
|
18
|
-
|
|
19
|
-
def
|
|
14
|
+
|
|
15
|
+
def initialize(*)
|
|
20
16
|
set_new_record(true)
|
|
21
17
|
set_destroyed(false)
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
|
|
19
|
+
super
|
|
24
20
|
end
|
|
25
|
-
|
|
21
|
+
|
|
26
22
|
protected
|
|
27
23
|
def set_new_record(value)
|
|
28
24
|
set_record_status(:new_record, value)
|
|
29
25
|
end
|
|
30
|
-
|
|
26
|
+
|
|
31
27
|
def set_destroyed(value)
|
|
32
28
|
set_record_status(:destroyed, value)
|
|
33
29
|
end
|
|
34
|
-
|
|
30
|
+
|
|
35
31
|
private
|
|
36
32
|
def set_record_status(type, value)
|
|
37
33
|
instance_variable_set("@_#{type}", value)
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
module MongoModel
|
|
2
2
|
module Serialization
|
|
3
3
|
extend ActiveSupport::Concern
|
|
4
|
-
|
|
4
|
+
|
|
5
5
|
include ActiveModel::Serializers::JSON
|
|
6
|
-
|
|
6
|
+
|
|
7
7
|
def serializable_hash(given_options = nil)
|
|
8
8
|
options = given_options ? given_options.dup : {}
|
|
9
9
|
|
|
@@ -12,7 +12,7 @@ module MongoModel
|
|
|
12
12
|
options[:methods] = Array.wrap(options[:methods]).map { |n| n.to_s }
|
|
13
13
|
|
|
14
14
|
attribute_names = attributes_for_serialization
|
|
15
|
-
|
|
15
|
+
|
|
16
16
|
if options[:only].any?
|
|
17
17
|
attribute_names &= options[:only]
|
|
18
18
|
elsif options[:except].any?
|
|
@@ -29,7 +29,7 @@ module MongoModel
|
|
|
29
29
|
hash
|
|
30
30
|
}
|
|
31
31
|
end
|
|
32
|
-
|
|
32
|
+
|
|
33
33
|
protected
|
|
34
34
|
def attributes_for_serialization
|
|
35
35
|
properties.reject { |name, property|
|