mongomodel 0.5.5 → 0.5.6
Sign up to get free protection for your applications and to get access to all the features.
- 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
@@ -16,42 +16,42 @@ module MongoModel
|
|
16
16
|
def initialize(options)
|
17
17
|
@options = DEFAULTS.merge(options).stringify_keys
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
def host
|
21
21
|
options['host']
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
def port
|
25
25
|
options['port']
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
def database
|
29
29
|
options['database']
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
def username
|
33
33
|
options['username']
|
34
34
|
end
|
35
|
-
|
35
|
+
|
36
36
|
def password
|
37
37
|
options['password']
|
38
38
|
end
|
39
|
-
|
39
|
+
|
40
40
|
def replicas
|
41
41
|
options['replicas'] || []
|
42
42
|
end
|
43
|
-
|
43
|
+
|
44
44
|
def establish_connection
|
45
45
|
@database = connection.db(database)
|
46
46
|
@database.authenticate(username, password) if username.present?
|
47
47
|
@database
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
def use_database(database)
|
51
51
|
options['database'] = database
|
52
52
|
establish_connection
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
def connection
|
56
56
|
if replicas.any?
|
57
57
|
@connection ||= Mongo::MongoReplicaSetClient.new(replicas, connection_options)
|
@@ -59,11 +59,11 @@ module MongoModel
|
|
59
59
|
@connection ||= Mongo::MongoClient.new(host, port, connection_options)
|
60
60
|
end
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
def connection_options
|
64
64
|
options.except('host', 'port', 'database', 'username', 'password', 'replicas').symbolize_keys
|
65
65
|
end
|
66
|
-
|
66
|
+
|
67
67
|
def self.defaults
|
68
68
|
new({})
|
69
69
|
end
|
@@ -4,7 +4,7 @@ class Symbol
|
|
4
4
|
[:lt, :lte, :gt, :gte, :ne, :in, :nin, :mod, :all, :size, :exists, :near].each do |operator|
|
5
5
|
define_method(operator) { MongoModel::MongoOperator.new(self, operator) } unless method_defined?(operator)
|
6
6
|
end
|
7
|
-
|
7
|
+
|
8
8
|
define_method(:asc) { MongoModel::MongoOrder::Clause.new(self, :ascending) } unless method_defined?(:asc)
|
9
9
|
define_method(:desc) { MongoModel::MongoOrder::Clause.new(self, :descending) } unless method_defined?(:desc)
|
10
10
|
end
|
@@ -3,13 +3,13 @@ module MongoModel
|
|
3
3
|
def initialize(scope, attribute_names, finder=:first, bang=false)
|
4
4
|
@scope, @attribute_names, @finder, @bang = scope, attribute_names, finder, bang
|
5
5
|
end
|
6
|
-
|
6
|
+
|
7
7
|
def execute(*args)
|
8
8
|
options = args.extract_options!
|
9
9
|
conditions = build_conditions(args)
|
10
|
-
|
10
|
+
|
11
11
|
result = @scope.where(conditions).send(instantiator? ? :first : @finder)
|
12
|
-
|
12
|
+
|
13
13
|
if result.nil?
|
14
14
|
if bang?
|
15
15
|
raise DocumentNotFound, "Couldn't find #{@scope.klass.to_s} with #{conditions.inspect}"
|
@@ -17,22 +17,22 @@ module MongoModel
|
|
17
17
|
return @scope.send(@finder, conditions)
|
18
18
|
end
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
result
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
def bang?
|
25
25
|
@bang
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
def instantiator?
|
29
29
|
@finder == :new || @finder == :create
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
def self.match(scope, method)
|
33
33
|
finder = :first
|
34
34
|
bang = false
|
35
|
-
|
35
|
+
|
36
36
|
case method.to_s
|
37
37
|
when /^find_(all_by|last_by|by)_([_a-zA-Z]\w*)$/
|
38
38
|
finder = :last if $1 == 'last_by'
|
@@ -47,21 +47,21 @@ module MongoModel
|
|
47
47
|
else
|
48
48
|
return nil
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
names = names.split('_and_')
|
52
52
|
if names.all? { |n| scope.klass.properties.include?(n.to_sym) }
|
53
53
|
new(scope, names, finder, bang)
|
54
54
|
end
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
private
|
58
58
|
def build_conditions(args)
|
59
59
|
result = {}
|
60
|
-
|
60
|
+
|
61
61
|
@attribute_names.zip(args) do |attribute, value|
|
62
62
|
result[attribute.to_sym] = value
|
63
63
|
end
|
64
|
-
|
64
|
+
|
65
65
|
result
|
66
66
|
end
|
67
67
|
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
module MongoModel
|
2
2
|
class DocumentNotFound < StandardError; end
|
3
|
-
|
3
|
+
|
4
4
|
class DocumentNotSaved < StandardError; end
|
5
|
-
|
5
|
+
|
6
6
|
# Raised by <tt>save!</tt> and <tt>create!</tt> when the document is invalid. Use the
|
7
7
|
# +document+ method to retrieve the document which did not validate.
|
8
8
|
# begin
|
@@ -12,16 +12,16 @@ module MongoModel
|
|
12
12
|
# end
|
13
13
|
class DocumentInvalid < DocumentNotSaved
|
14
14
|
attr_reader :document
|
15
|
-
|
15
|
+
|
16
16
|
def initialize(document)
|
17
17
|
@document = document
|
18
|
-
|
18
|
+
|
19
19
|
errors = @document.errors.full_messages.join(I18n.t('support.array.words_connector', :default => ', '))
|
20
20
|
super(I18n.t('mongomodel.errors.messages.document_invalid', :errors => errors))
|
21
21
|
end
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
class AssociationTypeMismatch < StandardError; end
|
25
|
-
|
25
|
+
|
26
26
|
class TooManyDocuments < StandardError; end
|
27
27
|
end
|
@@ -6,46 +6,46 @@
|
|
6
6
|
module MongoModel
|
7
7
|
class InstrumentedCursor
|
8
8
|
attr_reader :cursor
|
9
|
-
|
9
|
+
|
10
10
|
def initialize(cursor)
|
11
11
|
@cursor = cursor
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
def to_a
|
15
15
|
instrument(query_description) do
|
16
16
|
cursor.to_a
|
17
17
|
end
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
def count
|
21
21
|
instrument("count(#{cursor.selector.inspect})") do
|
22
22
|
cursor.count
|
23
23
|
end
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
private
|
27
27
|
def method_missing(method, *args, &block)
|
28
28
|
cursor.send(method, *args, &block)
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
def query_description
|
32
32
|
"find(#{cursor.selector.inspect}, #{cursor.fields ? cursor.fields.inspect : '{}'})" +
|
33
33
|
"#{cursor.skip != 0 ? ('.skip(' + cursor.skip.to_s + ')') : ''}#{cursor.limit != 0 ? ('.limit(' + cursor.limit.to_s + ')') : ''}" +
|
34
34
|
"#{cursor.order ? ('.sort(' + cursor.order.inspect + ')') : ''}"
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
def instrument(query, &block)
|
38
38
|
ActiveSupport::Notifications.instrument("query.mongomodel", :collection => cursor.collection.name, :query => query, &block)
|
39
39
|
end
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
class InstrumentedCollection
|
43
43
|
attr_reader :collection
|
44
|
-
|
44
|
+
|
45
45
|
def initialize(collection)
|
46
46
|
@collection = collection
|
47
47
|
end
|
48
|
-
|
48
|
+
|
49
49
|
def ==(other)
|
50
50
|
case other
|
51
51
|
when self.class
|
@@ -54,10 +54,10 @@ module MongoModel
|
|
54
54
|
collection == other
|
55
55
|
end
|
56
56
|
end
|
57
|
-
|
57
|
+
|
58
58
|
def find(selector={}, options={})
|
59
59
|
cursor = InstrumentedCursor.new(collection.find(selector, options))
|
60
|
-
|
60
|
+
|
61
61
|
if block_given?
|
62
62
|
yield cursor
|
63
63
|
cursor.close
|
@@ -66,7 +66,7 @@ module MongoModel
|
|
66
66
|
cursor
|
67
67
|
end
|
68
68
|
end
|
69
|
-
|
69
|
+
|
70
70
|
def save(doc, options={})
|
71
71
|
if doc.has_key?(:_id) || doc.has_key?('_id')
|
72
72
|
selector = { '_id' => doc[:_id] || doc['_id'] }
|
@@ -79,48 +79,48 @@ module MongoModel
|
|
79
79
|
end
|
80
80
|
end
|
81
81
|
end
|
82
|
-
|
82
|
+
|
83
83
|
def remove(selector={}, options={})
|
84
84
|
instrument("remove(#{selector.inspect})") do
|
85
85
|
collection.remove(selector, options)
|
86
86
|
end
|
87
87
|
end
|
88
|
-
|
88
|
+
|
89
89
|
def update(selector, document, options={})
|
90
90
|
instrument("update(#{selector.inspect}, #{document.inspect})") do
|
91
91
|
collection.update(selector, document, options)
|
92
92
|
end
|
93
93
|
end
|
94
|
-
|
94
|
+
|
95
95
|
def create_index(spec, options={})
|
96
96
|
instrument("create_index(#{spec.inspect})") do
|
97
97
|
collection.create_index(spec, options)
|
98
98
|
end
|
99
99
|
end
|
100
|
-
|
100
|
+
|
101
101
|
def group(options, condition={}, initial={}, reduce=nil, finalize=nil)
|
102
102
|
instrument("group(#{options.inspect})") do
|
103
103
|
collection.group(options, condition, initial, reduce, finalize)
|
104
104
|
end
|
105
105
|
end
|
106
|
-
|
106
|
+
|
107
107
|
def distinct(key, query=nil)
|
108
108
|
instrument("distinct(#{key.inspect}#{query.present? ? ', ' + query.inspect : ''})") do
|
109
109
|
collection.distinct(key, query)
|
110
110
|
end
|
111
111
|
end
|
112
|
-
|
112
|
+
|
113
113
|
def map_reduce(map, reduce, options={})
|
114
114
|
instrument("map_reduce(#{options.inspect})") do
|
115
115
|
collection.map_reduce(map, reduce, options)
|
116
116
|
end
|
117
117
|
end
|
118
|
-
|
118
|
+
|
119
119
|
private
|
120
120
|
def method_missing(method, *args, &block)
|
121
121
|
collection.send(method, *args, &block)
|
122
122
|
end
|
123
|
-
|
123
|
+
|
124
124
|
def instrument(query, &block)
|
125
125
|
ActiveSupport::Notifications.instrument("query.mongomodel", :collection => collection.name, :query => query, &block)
|
126
126
|
end
|
@@ -5,30 +5,30 @@ module MongoModel
|
|
5
5
|
module PropertyDefaults
|
6
6
|
def property(name, *args, &block) #:nodoc:
|
7
7
|
property = super(name, *args, &block)
|
8
|
-
|
8
|
+
|
9
9
|
if property.type <= Map
|
10
10
|
property.options[:default] ||= lambda { property.type.new }
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
property
|
14
14
|
end
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
class_attribute :from
|
18
18
|
self.from = String
|
19
|
-
|
19
|
+
|
20
20
|
class_attribute :to
|
21
21
|
self.to = Object
|
22
|
-
|
22
|
+
|
23
23
|
HASH_CONVERTER = Types.converter_for(Hash)
|
24
|
-
|
24
|
+
|
25
25
|
class << self
|
26
26
|
def [](mapping)
|
27
27
|
raise "Exactly one mapping must be specified" unless mapping.keys.size == 1
|
28
|
-
|
28
|
+
|
29
29
|
from = mapping.keys.first
|
30
30
|
to = mapping.values.first
|
31
|
-
|
31
|
+
|
32
32
|
@map_class_cache ||= {}
|
33
33
|
@map_class_cache[[from, to]] ||= begin
|
34
34
|
map = Class.new(Map)
|
@@ -37,13 +37,13 @@ module MongoModel
|
|
37
37
|
map
|
38
38
|
end
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
def from_mongo(hash)
|
42
42
|
result = new
|
43
43
|
hash.each_pair { |k, v| result[from_converter.from_mongo(k)] = instantiate(v) }
|
44
44
|
result
|
45
45
|
end
|
46
|
-
|
46
|
+
|
47
47
|
def inspect
|
48
48
|
if self == Map
|
49
49
|
"Map"
|
@@ -51,15 +51,15 @@ module MongoModel
|
|
51
51
|
"Map[#{from} => #{to}]"
|
52
52
|
end
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
def from_converter
|
56
56
|
@from_converter ||= Types.converter_for(from)
|
57
57
|
end
|
58
|
-
|
58
|
+
|
59
59
|
def to_converter
|
60
60
|
@to_converter ||= Types.converter_for(to)
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
private
|
64
64
|
def instantiate(item)
|
65
65
|
if item.is_a?(Hash) && item['_type']
|
@@ -69,85 +69,85 @@ module MongoModel
|
|
69
69
|
end
|
70
70
|
end
|
71
71
|
end
|
72
|
-
|
72
|
+
|
73
73
|
def initialize(hash={})
|
74
74
|
super()
|
75
75
|
update(hash)
|
76
76
|
end
|
77
|
-
|
77
|
+
|
78
78
|
def to_mongo
|
79
79
|
HASH_CONVERTER.to_mongo(self)
|
80
80
|
end
|
81
|
-
|
81
|
+
|
82
82
|
def [](key)
|
83
83
|
super(convert_key(key))
|
84
84
|
end
|
85
|
-
|
85
|
+
|
86
86
|
def []=(key, value)
|
87
87
|
super(convert_key(key), convert_value(value))
|
88
88
|
end
|
89
|
-
|
89
|
+
|
90
90
|
def store(key, value)
|
91
91
|
super(convert_key(key), convert_value(value))
|
92
92
|
end
|
93
|
-
|
93
|
+
|
94
94
|
def delete(key)
|
95
95
|
super(convert_key(key))
|
96
96
|
end
|
97
|
-
|
97
|
+
|
98
98
|
def fetch(key, *args, &block)
|
99
99
|
super(convert_key(key), *args, &block)
|
100
100
|
end
|
101
|
-
|
101
|
+
|
102
102
|
def key?(key)
|
103
103
|
super(convert_key(key))
|
104
104
|
end
|
105
|
-
|
105
|
+
|
106
106
|
alias_method :include?, :key?
|
107
107
|
alias_method :has_key?, :key?
|
108
108
|
alias_method :member?, :key?
|
109
|
-
|
109
|
+
|
110
110
|
def value?(value)
|
111
111
|
super(convert_value(value))
|
112
112
|
end
|
113
|
-
|
113
|
+
|
114
114
|
alias_method :has_value?, :value?
|
115
|
-
|
115
|
+
|
116
116
|
def index(value)
|
117
117
|
super(convert_value(value))
|
118
118
|
end
|
119
|
-
|
119
|
+
|
120
120
|
def key(value)
|
121
121
|
super(convert_value(value))
|
122
122
|
end
|
123
|
-
|
123
|
+
|
124
124
|
def update(hash)
|
125
125
|
hash.each_pair { |k, v| self[k] = v }
|
126
126
|
self
|
127
127
|
end
|
128
|
-
|
128
|
+
|
129
129
|
def replace(hash)
|
130
130
|
clear
|
131
131
|
update(hash)
|
132
132
|
end
|
133
|
-
|
133
|
+
|
134
134
|
def merge(hash)
|
135
135
|
dup.update(super(hash))
|
136
136
|
end
|
137
|
-
|
137
|
+
|
138
138
|
def merge!(hash)
|
139
139
|
update(merge(hash))
|
140
140
|
end
|
141
|
-
|
141
|
+
|
142
142
|
def values_at(*keys)
|
143
143
|
super(*keys.map { |k| convert_key(k) })
|
144
144
|
end
|
145
|
-
|
145
|
+
|
146
146
|
private
|
147
147
|
def convert_key(key)
|
148
148
|
self.class.from_converter.cast(key)
|
149
149
|
end
|
150
|
-
|
150
|
+
|
151
151
|
def convert_value(value)
|
152
152
|
self.class.to_converter.cast(value)
|
153
153
|
end
|