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
@@ -21,7 +21,7 @@ module MongoModel
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
def first(count=nil)
|
26
26
|
if loaded?
|
27
27
|
count ? to_a.first(count) : to_a.first
|
@@ -29,7 +29,7 @@ module MongoModel
|
|
29
29
|
count ? limit(count).to_a : limit(1).to_a[0]
|
30
30
|
end
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
33
|
def last(count=nil)
|
34
34
|
if loaded?
|
35
35
|
count ? to_a.last(count) : to_a.last
|
@@ -37,24 +37,24 @@ module MongoModel
|
|
37
37
|
count ? reverse_order.limit(count).to_a : reverse_order.limit(1).to_a[0]
|
38
38
|
end
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
def all
|
42
42
|
to_a
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
def exists?(id)
|
46
46
|
where(:id => id).any?
|
47
47
|
end
|
48
|
-
|
48
|
+
|
49
49
|
def apply_finder_options(options={})
|
50
50
|
result = clone
|
51
|
-
|
51
|
+
|
52
52
|
result = result.where(options[:conditions]) if options[:conditions]
|
53
53
|
result = result.order(options[:order]) if options[:order]
|
54
54
|
result = result.select(options[:select]) if options[:select]
|
55
55
|
result = result.limit(options[:limit]) if options[:limit]
|
56
56
|
result = result.offset(options[:offset]) if options[:offset]
|
57
|
-
|
57
|
+
|
58
58
|
result
|
59
59
|
end
|
60
60
|
end
|
@@ -5,10 +5,10 @@ module MongoModel
|
|
5
5
|
SINGLE_VALUE_METHODS.each { |m| instance_variable_set("@#{m}_value", nil) }
|
6
6
|
MULTI_VALUE_METHODS.each { |m| instance_variable_set("@#{m}_values", []) }
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
MULTI_VALUE_METHODS.each do |query_method|
|
10
10
|
attr_accessor :"#{query_method}_values"
|
11
|
-
|
11
|
+
|
12
12
|
class_eval <<-CEVAL, __FILE__
|
13
13
|
def #{query_method}(*args, &block)
|
14
14
|
new_scope = clone
|
@@ -16,7 +16,7 @@ module MongoModel
|
|
16
16
|
new_scope.#{query_method}_values += value if value.present?
|
17
17
|
new_scope
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
def #{query_method}!(*args, &block)
|
21
21
|
new_scope = clone
|
22
22
|
value = Array.wrap(args.flatten).reject { |x| x.blank? }
|
@@ -25,7 +25,7 @@ module MongoModel
|
|
25
25
|
end
|
26
26
|
CEVAL
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
SINGLE_VALUE_METHODS.each do |query_method|
|
30
30
|
attr_accessor :"#{query_method}_value"
|
31
31
|
|
@@ -37,13 +37,13 @@ module MongoModel
|
|
37
37
|
end
|
38
38
|
CEVAL
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
def from(value, &block)
|
42
42
|
new_scope = clone
|
43
43
|
new_scope.from_value = InstrumentedCollection.new(value.is_a?(String) ? klass.database.collection(value) : value)
|
44
44
|
new_scope
|
45
45
|
end
|
46
|
-
|
46
|
+
|
47
47
|
def reverse_order
|
48
48
|
if order_values.empty?
|
49
49
|
order(:id.desc)
|
@@ -3,33 +3,33 @@ module MongoModel
|
|
3
3
|
module SpawnMethods
|
4
4
|
def merge(scope)
|
5
5
|
result = clone
|
6
|
-
|
6
|
+
|
7
7
|
MULTI_VALUE_METHODS.each do |method|
|
8
8
|
values = send(:"#{method}_values") + scope.send(:"#{method}_values")
|
9
9
|
result.send(:"#{method}_values=", values.uniq)
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
SINGLE_VALUE_METHODS.each do |method|
|
13
13
|
value = scope.send(:"#{method}_value")
|
14
14
|
result.send(:"#{method}_value=", value) if value
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
result.on_load_proc = scope.on_load_proc
|
18
|
-
|
18
|
+
|
19
19
|
result
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
def except(*exceptions)
|
23
23
|
result = self.class.new(klass)
|
24
|
-
|
24
|
+
|
25
25
|
MULTI_VALUE_METHODS.each do |method|
|
26
26
|
result.send(:"#{method}_values=", send(:"#{method}_values")) unless exceptions.include?(method)
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
SINGLE_VALUE_METHODS.each do |method|
|
30
30
|
result.send(:"#{method}_value=", send(:"#{method}_value")) unless exceptions.include?(method)
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
33
|
result
|
34
34
|
end
|
35
35
|
end
|
@@ -4,11 +4,11 @@ require 'mongomodel/support/types/custom'
|
|
4
4
|
module MongoModel
|
5
5
|
module Types
|
6
6
|
CONVERTERS = Hash.new { |h, k| h[k] = Types::Custom.new(k) }
|
7
|
-
|
7
|
+
|
8
8
|
def self.register_converter(type, converter)
|
9
9
|
CONVERTERS[type] = converter
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
def self.converter_for(type)
|
13
13
|
CONVERTERS[type]
|
14
14
|
end
|
@@ -3,7 +3,7 @@ module MongoModel
|
|
3
3
|
class Boolean < Object
|
4
4
|
TRUE_VALUES = [ true, 'true', 't', 'TRUE', 'T', 'yes', 'YES', 'Y', 'y', '1', 1 ]
|
5
5
|
FALSE_VALUES = [ false, 'false', 'f', 'FALSE', 'F', 'no', 'NO', 'N', 'n', '0', 0 ]
|
6
|
-
|
6
|
+
|
7
7
|
def cast(value)
|
8
8
|
if true?(value)
|
9
9
|
true
|
@@ -11,12 +11,12 @@ module MongoModel
|
|
11
11
|
false
|
12
12
|
end
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
private
|
16
16
|
def true?(value)
|
17
17
|
TRUE_VALUES.include?(value)
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
def false?(value)
|
21
21
|
FALSE_VALUES.include?(value)
|
22
22
|
end
|
@@ -6,7 +6,7 @@ module MongoModel
|
|
6
6
|
def initialize(type)
|
7
7
|
@type = type
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
def cast(value)
|
11
11
|
if value.is_a?(@type)
|
12
12
|
value
|
@@ -16,7 +16,7 @@ module MongoModel
|
|
16
16
|
@type.new(value)
|
17
17
|
end
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
def to_mongo(value)
|
21
21
|
if value.respond_to?(:to_mongo)
|
22
22
|
value.to_mongo
|
@@ -24,7 +24,7 @@ module MongoModel
|
|
24
24
|
value
|
25
25
|
end
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
def from_mongo(value)
|
29
29
|
if @type.respond_to?(:from_mongo)
|
30
30
|
value = value.with_indifferent_access if value.respond_to?(:with_indifferent_access)
|
@@ -18,34 +18,24 @@ module MongoModel
|
|
18
18
|
rescue
|
19
19
|
nil
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
def to_mongo(value)
|
23
23
|
to_time(value.utc) if value
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
def from_mongo(t)
|
27
27
|
::DateTime.civil(t.year, t.month, t.day, t.hour, t.min, t.sec + Rational(t.usec, 1000000)) if t
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
private
|
31
31
|
# Define our own to_time method as DateTime.to_time in ActiveSupport may return
|
32
32
|
# the DateTime object unchanged, whereas BSON expects an actual Time object.
|
33
33
|
def to_time(dt)
|
34
|
-
::Time.utc(dt.year, dt.month, dt.day, dt.hour, dt.min, dt.sec,
|
34
|
+
::Time.utc(dt.year, dt.month, dt.day, dt.hour, dt.min, dt.sec, dt.sec_fraction.to_f * 1000000)
|
35
35
|
end
|
36
|
-
|
37
|
-
def sec_fraction(dt)
|
38
|
-
if RUBY_VERSION <= "1.8.7"
|
39
|
-
# Ruby 1.8.7 DateTime#sec_fraction unit is in days
|
40
|
-
dt.sec_fraction * 86400
|
41
|
-
else
|
42
|
-
dt.sec_fraction
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
36
|
+
|
46
37
|
def round_microseconds(dt)
|
47
|
-
|
48
|
-
dt.change(:sec => Rational((seconds * 1000).round, 1000))
|
38
|
+
::DateTime.civil(dt.year, dt.month, dt.day, dt.hour, dt.min, dt.sec + Rational(dt.sec_fraction).truncate(3))
|
49
39
|
end
|
50
40
|
end
|
51
41
|
end
|
@@ -2,20 +2,23 @@ module MongoModel
|
|
2
2
|
module Types
|
3
3
|
class Integer < Object
|
4
4
|
def cast(value)
|
5
|
-
if value
|
6
|
-
nil
|
7
|
-
else
|
8
|
-
begin
|
9
|
-
Kernel::Integer(value)
|
10
|
-
rescue ArgumentError
|
11
|
-
Kernel::Float(value).to_i rescue nil
|
12
|
-
end
|
13
|
-
end
|
5
|
+
to_integer(value) if value
|
14
6
|
end
|
15
|
-
|
7
|
+
|
16
8
|
def boolean(value)
|
17
9
|
!value.zero?
|
18
10
|
end
|
11
|
+
|
12
|
+
def from_mongo(value)
|
13
|
+
to_integer(value)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
def to_integer(value)
|
18
|
+
Kernel::Integer(value)
|
19
|
+
rescue ArgumentError, TypeError
|
20
|
+
Kernel::Float(value).to_i rescue nil
|
21
|
+
end
|
19
22
|
end
|
20
23
|
end
|
21
24
|
end
|
@@ -4,23 +4,23 @@ module MongoModel
|
|
4
4
|
def cast(value)
|
5
5
|
value
|
6
6
|
end
|
7
|
-
|
7
|
+
|
8
8
|
def boolean(value)
|
9
9
|
!value.blank?
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
def to_mongo(value)
|
13
13
|
value
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
def from_mongo(value)
|
17
17
|
value
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
def to_query(value)
|
21
21
|
to_mongo(cast(value))
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
protected
|
25
25
|
def convert(value)
|
26
26
|
Types.converter_for(value.class).to_mongo(value)
|
@@ -19,15 +19,15 @@ module MongoModel
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
def from_mongo(value)
|
24
24
|
rational_from_string(value)
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
def to_mongo(value)
|
28
28
|
value.to_s
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
private
|
32
32
|
if RUBY_VERSION >= '1.9.2'
|
33
33
|
def rational_from_string(str)
|
data/lib/mongomodel/version.rb
CHANGED