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.
Files changed (156) hide show
  1. checksums.yaml +5 -5
  2. data/.travis.yml +23 -11
  3. data/gemfiles/rails-3.2.gemfile +2 -2
  4. data/gemfiles/rails-4-observers.gemfile +1 -1
  5. data/gemfiles/rails-4.1.gemfile +2 -2
  6. data/gemfiles/{mongoid.gemfile → rails-4.2.gemfile} +2 -3
  7. data/gemfiles/{mongo_mapper.gemfile → rails-5.0.gemfile} +2 -3
  8. data/gemfiles/rails-5.1.gemfile +10 -0
  9. data/gemfiles/rails-5.2.gemfile +10 -0
  10. data/lib/mongomodel.rb +15 -15
  11. data/lib/mongomodel/attributes/mongo.rb +7 -7
  12. data/lib/mongomodel/attributes/store.rb +4 -4
  13. data/lib/mongomodel/attributes/typecasting.rb +7 -7
  14. data/lib/mongomodel/compatibility/mongo_mapper.rb +3 -3
  15. data/lib/mongomodel/compatibility/mongoid.rb +3 -3
  16. data/lib/mongomodel/concerns/abstract_class.rb +3 -3
  17. data/lib/mongomodel/concerns/activemodel.rb +4 -4
  18. data/lib/mongomodel/concerns/associations.rb +8 -8
  19. data/lib/mongomodel/concerns/associations/base/association.rb +5 -5
  20. data/lib/mongomodel/concerns/associations/base/definition.rb +4 -4
  21. data/lib/mongomodel/concerns/associations/base/proxy.rb +1 -1
  22. data/lib/mongomodel/concerns/associations/belongs_to.rb +19 -19
  23. data/lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb +36 -36
  24. data/lib/mongomodel/concerns/associations/has_many_by_ids.rb +34 -34
  25. data/lib/mongomodel/concerns/attribute_methods.rb +10 -10
  26. data/lib/mongomodel/concerns/attribute_methods/before_type_cast.rb +4 -4
  27. data/lib/mongomodel/concerns/attribute_methods/dirty.rb +95 -13
  28. data/lib/mongomodel/concerns/attribute_methods/forbidden.rb +1 -1
  29. data/lib/mongomodel/concerns/attribute_methods/multi_parameter_assignment.rb +6 -6
  30. data/lib/mongomodel/concerns/attribute_methods/nested.rb +18 -18
  31. data/lib/mongomodel/concerns/attribute_methods/protected.rb +3 -3
  32. data/lib/mongomodel/concerns/attribute_methods/query.rb +3 -3
  33. data/lib/mongomodel/concerns/attribute_methods/read.rb +4 -4
  34. data/lib/mongomodel/concerns/attribute_methods/write.rb +4 -4
  35. data/lib/mongomodel/concerns/attributes.rb +18 -18
  36. data/lib/mongomodel/concerns/callbacks.rb +7 -7
  37. data/lib/mongomodel/concerns/document_parent.rb +2 -2
  38. data/lib/mongomodel/concerns/logging.rb +2 -2
  39. data/lib/mongomodel/concerns/map_reduce.rb +11 -11
  40. data/lib/mongomodel/concerns/pretty_inspect.rb +3 -3
  41. data/lib/mongomodel/concerns/properties.rb +18 -18
  42. data/lib/mongomodel/concerns/record_status.rb +9 -13
  43. data/lib/mongomodel/concerns/serialization.rb +4 -4
  44. data/lib/mongomodel/concerns/timestamps.rb +4 -4
  45. data/lib/mongomodel/concerns/translation.rb +2 -2
  46. data/lib/mongomodel/concerns/validations.rb +5 -5
  47. data/lib/mongomodel/concerns/validations/associated.rb +1 -1
  48. data/lib/mongomodel/document.rb +6 -6
  49. data/lib/mongomodel/document/callbacks.rb +15 -21
  50. data/lib/mongomodel/document/collection_modifiers.rb +5 -5
  51. data/lib/mongomodel/document/dynamic_finders.rb +1 -1
  52. data/lib/mongomodel/document/indexes.rb +19 -19
  53. data/lib/mongomodel/document/optimistic_locking.rb +7 -7
  54. data/lib/mongomodel/document/persistence.rb +23 -23
  55. data/lib/mongomodel/document/scopes.rb +20 -20
  56. data/lib/mongomodel/document/validations.rb +6 -6
  57. data/lib/mongomodel/document/validations/uniqueness.rb +11 -11
  58. data/lib/mongomodel/embedded_document.rb +11 -11
  59. data/lib/mongomodel/locale/en.yml +0 -1
  60. data/lib/mongomodel/log_subscriber.rb +5 -5
  61. data/lib/mongomodel/railtie.rb +13 -13
  62. data/lib/mongomodel/support/collection.rb +31 -31
  63. data/lib/mongomodel/support/configuration.rb +11 -11
  64. data/lib/mongomodel/support/core_extensions.rb +1 -1
  65. data/lib/mongomodel/support/dynamic_finder.rb +12 -12
  66. data/lib/mongomodel/support/exceptions.rb +6 -6
  67. data/lib/mongomodel/support/instrumented_collection.rb +20 -20
  68. data/lib/mongomodel/support/map.rb +33 -33
  69. data/lib/mongomodel/support/mongo_operator.rb +6 -6
  70. data/lib/mongomodel/support/mongo_options.rb +18 -18
  71. data/lib/mongomodel/support/mongo_order.rb +16 -16
  72. data/lib/mongomodel/support/paginator.rb +8 -8
  73. data/lib/mongomodel/support/reference.rb +10 -10
  74. data/lib/mongomodel/support/scope.rb +37 -37
  75. data/lib/mongomodel/support/scope/array_methods.rb +1 -1
  76. data/lib/mongomodel/support/scope/batches.rb +1 -1
  77. data/lib/mongomodel/support/scope/dynamic_finders.rb +1 -1
  78. data/lib/mongomodel/support/scope/finder_methods.rb +7 -7
  79. data/lib/mongomodel/support/scope/load_methods.rb +1 -1
  80. data/lib/mongomodel/support/scope/pagination.rb +1 -1
  81. data/lib/mongomodel/support/scope/query_methods.rb +6 -6
  82. data/lib/mongomodel/support/scope/spawn_methods.rb +8 -8
  83. data/lib/mongomodel/support/types.rb +2 -2
  84. data/lib/mongomodel/support/types/array.rb +1 -1
  85. data/lib/mongomodel/support/types/boolean.rb +3 -3
  86. data/lib/mongomodel/support/types/custom.rb +3 -3
  87. data/lib/mongomodel/support/types/date.rb +2 -2
  88. data/lib/mongomodel/support/types/date_time.rb +6 -16
  89. data/lib/mongomodel/support/types/float.rb +1 -1
  90. data/lib/mongomodel/support/types/hash.rb +1 -1
  91. data/lib/mongomodel/support/types/integer.rb +13 -10
  92. data/lib/mongomodel/support/types/object.rb +5 -5
  93. data/lib/mongomodel/support/types/rational.rb +3 -3
  94. data/lib/mongomodel/support/types/time.rb +2 -2
  95. data/lib/mongomodel/version.rb +1 -1
  96. data/lib/rails/generators/mongo_model/config/templates/mongomodel.yml +3 -4
  97. data/lib/rails/generators/mongo_model/model/model_generator.rb +3 -3
  98. data/mongomodel.gemspec +5 -4
  99. data/spec/mongomodel/attributes/store_spec.rb +21 -21
  100. data/spec/mongomodel/concerns/activemodel_spec.rb +4 -4
  101. data/spec/mongomodel/concerns/associations/base/association_spec.rb +12 -12
  102. data/spec/mongomodel/concerns/associations/belongs_to_spec.rb +34 -21
  103. data/spec/mongomodel/concerns/associations/has_many_by_foreign_key_spec.rb +53 -53
  104. data/spec/mongomodel/concerns/associations/has_many_by_ids_spec.rb +30 -30
  105. data/spec/mongomodel/concerns/attribute_methods/before_type_cast_spec.rb +7 -7
  106. data/spec/mongomodel/concerns/attribute_methods/dirty_spec.rb +45 -41
  107. data/spec/mongomodel/concerns/attribute_methods/multi_parameter_assignment_spec.rb +7 -7
  108. data/spec/mongomodel/concerns/attribute_methods/nested_spec.rb +31 -31
  109. data/spec/mongomodel/concerns/attribute_methods/protected_spec.rb +15 -15
  110. data/spec/mongomodel/concerns/attribute_methods/query_spec.rb +19 -19
  111. data/spec/mongomodel/concerns/attribute_methods/read_spec.rb +9 -9
  112. data/spec/mongomodel/concerns/attribute_methods/write_spec.rb +6 -6
  113. data/spec/mongomodel/concerns/attribute_methods_spec.rb +13 -13
  114. data/spec/mongomodel/concerns/attributes_spec.rb +34 -34
  115. data/spec/mongomodel/concerns/callbacks_spec.rb +25 -21
  116. data/spec/mongomodel/concerns/logging_spec.rb +5 -3
  117. data/spec/mongomodel/concerns/map_reduce_spec.rb +19 -19
  118. data/spec/mongomodel/concerns/observing_spec.rb +3 -3
  119. data/spec/mongomodel/concerns/pretty_inspect_spec.rb +10 -10
  120. data/spec/mongomodel/concerns/properties_spec.rb +11 -11
  121. data/spec/mongomodel/concerns/serialization/json_serialization_spec.rb +13 -13
  122. data/spec/mongomodel/concerns/timestamps_spec.rb +39 -39
  123. data/spec/mongomodel/concerns/validations_spec.rb +41 -38
  124. data/spec/mongomodel/document/callbacks_spec.rb +20 -16
  125. data/spec/mongomodel/document/collection_modifiers_spec.rb +16 -16
  126. data/spec/mongomodel/document/dynamic_finders_spec.rb +46 -46
  127. data/spec/mongomodel/document/finders_spec.rb +15 -15
  128. data/spec/mongomodel/document/indexes_spec.rb +29 -29
  129. data/spec/mongomodel/document/optimistic_locking_spec.rb +16 -16
  130. data/spec/mongomodel/document/persistence_spec.rb +39 -39
  131. data/spec/mongomodel/document/scopes_spec.rb +17 -17
  132. data/spec/mongomodel/document/validations/uniqueness_spec.rb +46 -46
  133. data/spec/mongomodel/document/validations_spec.rb +35 -35
  134. data/spec/mongomodel/document_spec.rb +19 -19
  135. data/spec/mongomodel/embedded_document_spec.rb +19 -19
  136. data/spec/mongomodel/mongomodel_spec.rb +7 -6
  137. data/spec/mongomodel/support/collection_spec.rb +54 -54
  138. data/spec/mongomodel/support/configuration_spec.rb +1 -1
  139. data/spec/mongomodel/support/map_spec.rb +66 -66
  140. data/spec/mongomodel/support/mongo_operator_spec.rb +5 -5
  141. data/spec/mongomodel/support/mongo_options_spec.rb +42 -42
  142. data/spec/mongomodel/support/mongo_order_spec.rb +24 -24
  143. data/spec/mongomodel/support/paginator_spec.rb +15 -15
  144. data/spec/mongomodel/support/property_spec.rb +29 -23
  145. data/spec/mongomodel/support/scope_spec.rb +205 -204
  146. data/spec/spec_helper.rb +13 -2
  147. data/spec/support/callbacks.rb +3 -8
  148. data/spec/support/helpers/define_class.rb +7 -7
  149. data/spec/support/helpers/document_finder_stubs.rb +6 -6
  150. data/spec/support/helpers/specs_for.rb +1 -1
  151. data/spec/support/helpers/validations.rb +1 -1
  152. data/spec/support/matchers/find_with.rb +8 -8
  153. data/spec/support/matchers/respond_to_boolean.rb +3 -3
  154. data/spec/support/matchers/run_callbacks.rb +6 -9
  155. data/spec/support/models.rb +5 -5
  156. metadata +23 -7
@@ -2,39 +2,39 @@ module MongoModel
2
2
  module DocumentExtensions
3
3
  module Scopes
4
4
  extend ActiveSupport::Concern
5
-
5
+
6
6
  delegate :current_scope, :to => "self.class"
7
-
7
+
8
8
  def initialize(*)
9
9
  self.attributes = current_scope.options_for_create if respond_to?(:attributes=)
10
10
  super
11
11
  end
12
-
12
+
13
13
  module ClassMethods
14
14
  delegate :find, :first, :last, :all, :exists?, :count, :to => :scoped
15
15
  delegate :update, :update_all, :delete, :delete_all, :destroy, :destroy_all, :to => :scoped
16
16
  delegate :select, :order, :where, :limit, :offset, :from, :paginate, :in_batches, :to => :scoped
17
-
17
+
18
18
  def unscoped
19
19
  @unscoped ||= MongoModel::Scope.new(self)
20
20
  end
21
-
21
+
22
22
  def scoped
23
23
  current_scope.clone
24
24
  end
25
-
25
+
26
26
  def scope(name, scope)
27
27
  name = name.to_sym
28
-
28
+
29
29
  if !scopes[name] && respond_to?(name, true)
30
30
  logger.warn "Creating scope :#{name}. " \
31
31
  "Overwriting existing method #{self.name}.#{name}."
32
32
  end
33
-
33
+
34
34
  scopes[name] = lambda do |*args|
35
35
  scope.is_a?(Proc) ? scope.call(*args) : scope
36
36
  end
37
-
37
+
38
38
  singleton_class.class_eval do
39
39
  define_method(name) do |*args|
40
40
  s = scopes[name].call(*args)
@@ -42,55 +42,55 @@ module MongoModel
42
42
  end
43
43
  end
44
44
  end
45
-
45
+
46
46
  def default_scope(scope)
47
47
  reset_current_scopes
48
48
  previous_scope = default_scoping.last || unscoped
49
49
  default_scoping << previous_scope.merge(scope)
50
50
  end
51
-
51
+
52
52
  def current_scope
53
53
  current_scopes.last || unscoped
54
54
  end
55
-
55
+
56
56
  def scopes
57
57
  @_scopes ||= {}
58
58
  end
59
-
59
+
60
60
  def scopes=(scopes)
61
61
  @_scopes = scopes
62
62
  end
63
-
63
+
64
64
  def default_scoping
65
65
  @_default_scoping ||= []
66
66
  end
67
-
67
+
68
68
  def default_scoping=(scoping)
69
69
  @_default_scoping = scoping
70
70
  end
71
-
71
+
72
72
  def inherited(subclass)
73
73
  super
74
74
  subclass.scopes = scopes.dup
75
75
  subclass.default_scoping = default_scoping.dup
76
76
  end
77
-
77
+
78
78
  protected
79
79
  def with_scope(scope, &block)
80
80
  current_scopes << current_scope.merge(scope)
81
-
81
+
82
82
  begin
83
83
  yield
84
84
  ensure
85
85
  current_scopes.pop
86
86
  end
87
87
  end
88
-
88
+
89
89
  private
90
90
  def current_scopes
91
91
  Thread.current[:"#{self}_scopes"] ||= default_scoping.dup
92
92
  end
93
-
93
+
94
94
  def reset_current_scopes
95
95
  Thread.current[:"#{self}_scopes"] = nil
96
96
  end
@@ -6,12 +6,12 @@ module MongoModel
6
6
  module ClassMethods
7
7
  def property(name, *args, &block) #:nodoc:
8
8
  property = super
9
-
9
+
10
10
  validates_uniqueness_of(name) if property.options[:unique]
11
-
11
+
12
12
  property
13
13
  end
14
-
14
+
15
15
  # Creates an object just like Document.create but calls save! instead of save
16
16
  # so an exception is raised if the document is invalid.
17
17
  def create!(attributes={}, &block)
@@ -24,7 +24,7 @@ module MongoModel
24
24
  end
25
25
  end
26
26
  end
27
-
27
+
28
28
  # The validation process on save can be skipped by passing <tt>:validate => false</tt>. The regular Document#save method is
29
29
  # replaced with this when the validations module is mixed in, which it is by default.
30
30
  def save(options={})
@@ -39,7 +39,7 @@ module MongoModel
39
39
  false
40
40
  end
41
41
  end
42
-
42
+
43
43
  # Attempts to save the document just like Document#save but will raise a DocumentInvalid exception
44
44
  # instead of returning false if the document is not valid.
45
45
  def save!(options={})
@@ -53,7 +53,7 @@ module MongoModel
53
53
  raise DocumentInvalid.new(self)
54
54
  end
55
55
  end
56
-
56
+
57
57
  protected
58
58
  def perform_validation(options={})
59
59
  perform_validation = options != false && options[:validate] != false
@@ -6,40 +6,40 @@ module MongoModel
6
6
  class UniquenessValidator < ActiveModel::EachValidator
7
7
  def initialize(options)
8
8
  options = options.reverse_merge(:case_sensitive => true)
9
-
9
+
10
10
  ActiveSupport::Deprecation.silence { super }
11
11
  setup!(options[:class]) if options[:class] && !@klass
12
12
  end
13
-
13
+
14
14
  def setup(klass)
15
15
  setup!(klass)
16
16
  end
17
-
17
+
18
18
  def validate_each(record, attribute, value)
19
19
  finder_class = find_finder_class_for(record)
20
20
  unique_scope = finder_class.scoped
21
-
21
+
22
22
  if options[:case_sensitive] || !value.is_a?(String)
23
23
  unique_scope = unique_scope.where(attribute => value)
24
24
  else
25
25
  unique_scope = unique_scope.where("_lowercase_#{attribute}" => value.downcase)
26
26
  end
27
-
27
+
28
28
  Array.wrap(options[:scope]).each do |scope|
29
29
  unique_scope = unique_scope.where(scope => record.send(scope))
30
30
  end
31
-
31
+
32
32
  unique_scope = unique_scope.where(:id.ne => record.id) unless record.new_record?
33
-
33
+
34
34
  if unique_scope.any?
35
35
  record.errors.add(attribute, :taken, :message => options[:message], :value => value)
36
36
  end
37
37
  end
38
-
38
+
39
39
  private
40
40
  def setup!(klass)
41
41
  @klass = klass
42
-
42
+
43
43
  unless options[:index] == false
44
44
  # Create unique indexes to deal with race condition
45
45
  attributes.each do |attr_name|
@@ -53,7 +53,7 @@ module MongoModel
53
53
  end
54
54
  end
55
55
  end
56
-
56
+
57
57
  # The check for an existing value should be run from a class that
58
58
  # isn't abstract. This means working down from the current class
59
59
  # (self), to the first non-abstract class. Since classes don't know
@@ -69,7 +69,7 @@ module MongoModel
69
69
  class_hierarchy.detect { |klass| !klass.abstract_class? }
70
70
  end
71
71
  end
72
-
72
+
73
73
  module ClassMethods
74
74
  # Validates whether the value of the specified attributes are unique across the system. Useful for making sure that only one user
75
75
  # can be named "davidhh".
@@ -3,17 +3,17 @@ module MongoModel
3
3
  def ==(other)
4
4
  self.class == other.class && attributes == other.attributes
5
5
  end
6
-
6
+
7
7
  include Attributes
8
8
  include Properties
9
-
9
+
10
10
  include Translation
11
11
  include Validations
12
12
  include Callbacks
13
13
  include Observing if defined?(ActiveModel::Observing)
14
-
14
+
15
15
  include Associations
16
-
16
+
17
17
  include AttributeMethods
18
18
  include AttributeMethods::Read
19
19
  include AttributeMethods::Write
@@ -24,29 +24,29 @@ module MongoModel
24
24
  include AttributeMethods::Nested
25
25
  include AttributeMethods::MultiParameterAssignment
26
26
  include AttributeMethods::Forbidden if defined?(ActiveModel::ForbiddenAttributesProtection)
27
-
27
+
28
28
  include Logging
29
- include RecordStatus
29
+ prepend RecordStatus
30
30
  include ActiveModelCompatibility
31
31
  include Serialization
32
32
  include Timestamps
33
33
  include PrettyInspect
34
34
  include AbstractClass
35
35
  include DocumentParent
36
-
36
+
37
37
  # Allow Collection class to be used in property definitions
38
38
  Collection = MongoModel::Collection
39
39
  extend Collection::PropertyDefaults
40
-
40
+
41
41
  # Allow Map class to be used in property definitions
42
42
  Map = MongoModel::Map
43
43
  extend Map::PropertyDefaults
44
-
44
+
45
45
  undef_method :type if method_defined?(:type)
46
46
  property :type, String, :as => '_type', :default => lambda { |doc| doc.class.name }, :protected => true
47
-
47
+
48
48
  self.abstract_class = true
49
49
  end
50
50
  end
51
51
 
52
- ActiveSupport::run_load_hooks(:mongomodel, MongoModel::EmbeddedDocument)
52
+ ActiveSupport::run_load_hooks(:mongomodel, MongoModel::EmbeddedDocument)
@@ -52,4 +52,3 @@ en:
52
52
  # user:
53
53
  # login: "Handle"
54
54
  # will translate User attribute "login" as "Handle"
55
-
@@ -17,24 +17,24 @@ module MongoModel
17
17
  super
18
18
  @odd_or_even = false
19
19
  end
20
-
20
+
21
21
  def query(event)
22
22
  self.class.runtime += event.duration
23
23
  return unless logger.debug?
24
-
24
+
25
25
  collection = '%s (%.1fms)' % [event.payload[:collection], event.duration]
26
26
  query = event.payload[:query]
27
-
27
+
28
28
  if odd?
29
29
  collection = color(collection, CYAN, true)
30
30
  query = color(query, nil, true)
31
31
  else
32
32
  collection = color(collection, MAGENTA, true)
33
33
  end
34
-
34
+
35
35
  debug " #{collection} #{query}"
36
36
  end
37
-
37
+
38
38
  def odd?
39
39
  @odd_or_even = !@odd_or_even
40
40
  end
@@ -3,44 +3,44 @@ module MongoModel
3
3
  def self.rescue_responses
4
4
  { 'MongoModel::DocumentNotFound' => :not_found }
5
5
  end
6
-
6
+
7
7
  if config.action_dispatch.rescue_responses
8
8
  config.action_dispatch.rescue_responses.merge!(rescue_responses)
9
9
  end
10
-
11
- config.app_generators.orm :mongo_model, :migration => false
10
+
11
+ config.app_generators.orm :mongo_model, :migration => false
12
12
 
13
13
  config.mongomodel = ActiveSupport::OrderedOptions.new
14
14
 
15
15
  rake_tasks do
16
16
  load "mongomodel/tasks/database.rake"
17
17
  end
18
-
18
+
19
19
  console do
20
20
  MongoModel.logger = Logger.new(STDERR)
21
21
  end
22
-
22
+
23
23
  initializer "mongomodel.logger" do
24
24
  MongoModel.logger ||= ::Rails.logger
25
25
  end
26
-
26
+
27
27
  initializer "mongomodel.rescue_responses" do
28
28
  unless config.action_dispatch.rescue_responses
29
29
  ActionDispatch::ShowExceptions.rescue_responses.update(self.class.rescue_responses)
30
30
  end
31
31
  end
32
-
32
+
33
33
  initializer "mongomodel.database_configuration" do |app|
34
34
  require 'erb'
35
-
35
+
36
36
  config = Rails.root.join("config", "mongomodel.yml")
37
-
37
+
38
38
  if File.exists?(config)
39
39
  mongomodel_configuration = YAML::load(ERB.new(IO.read(config)).result)
40
40
  MongoModel.configuration = mongomodel_configuration[Rails.env]
41
41
  end
42
42
  end
43
-
43
+
44
44
  # Expose database runtime to controller for logging.
45
45
  initializer "mongomodel.log_runtime" do |app|
46
46
  require "mongomodel/railties/controller_runtime"
@@ -48,7 +48,7 @@ module MongoModel
48
48
  include MongoModel::Railties::ControllerRuntime
49
49
  end
50
50
  end
51
-
51
+
52
52
  initializer "mongomodel.passenger_forking" do |app|
53
53
  if defined?(PhusionPassenger)
54
54
  PhusionPassenger.on_event(:starting_worker_process) do |forked|
@@ -56,13 +56,13 @@ module MongoModel
56
56
  end
57
57
  end
58
58
  end
59
-
59
+
60
60
  initializer "mongomodel.observers" do |app|
61
61
  if defined?(ActiveModel::Observing)
62
62
  MongoModel::EmbeddedDocument.observers = app.config.mongomodel.observers || []
63
63
  end
64
64
  end
65
-
65
+
66
66
  # Initialize observer instances if available
67
67
  ActiveSupport.on_load(:mongomodel) do
68
68
  if respond_to?(:instantiate_observers)
@@ -5,84 +5,84 @@ 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 <= Collection
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
  ARRAY_CONVERTER = Types.converter_for(Array)
18
-
18
+
19
19
  class_attribute :type
20
20
  self.type = Object
21
-
21
+
22
22
  include DocumentParent
23
-
23
+
24
24
  def initialize(array=[])
25
25
  super(array.map { |i| convert_for_add(i) })
26
26
  end
27
-
27
+
28
28
  def []=(index, value)
29
29
  super(index, convert_for_add(value))
30
30
  end
31
-
31
+
32
32
  def <<(value)
33
33
  super(convert_for_add(value))
34
34
  end
35
-
35
+
36
36
  def build(value={})
37
37
  value = convert(value)
38
38
  self << value
39
39
  value
40
40
  end
41
-
41
+
42
42
  def +(other)
43
43
  self.class.new(super(other))
44
44
  end
45
-
45
+
46
46
  def concat(values)
47
47
  super(values.map { |v| convert_for_add(v) })
48
48
  end
49
-
49
+
50
50
  def delete(value)
51
51
  super(convert(value))
52
52
  end
53
-
53
+
54
54
  def include?(value)
55
55
  super(convert(value))
56
56
  end
57
-
57
+
58
58
  def index(value)
59
59
  super(convert(value))
60
60
  end
61
-
61
+
62
62
  def insert(index, value)
63
63
  super(index, convert_for_add(value))
64
64
  end
65
-
65
+
66
66
  def push(*values)
67
67
  super(*values.map { |v| convert_for_add(v) })
68
68
  end
69
-
69
+
70
70
  def rindex(value)
71
71
  super(convert(value))
72
72
  end
73
-
73
+
74
74
  def unshift(*values)
75
75
  super(*values.map { |v| convert_for_add(v) })
76
76
  end
77
-
77
+
78
78
  def to_mongo
79
79
  ARRAY_CONVERTER.to_mongo(self)
80
80
  end
81
-
81
+
82
82
  def embedded_documents
83
83
  select { |item| item.is_a?(EmbeddedDocument) }
84
84
  end
85
-
85
+
86
86
  class << self
87
87
  def inspect
88
88
  if type == Object
@@ -91,14 +91,14 @@ module MongoModel
91
91
  "Collection[#{type}]"
92
92
  end
93
93
  end
94
-
94
+
95
95
  # Create a new MongoModel::Collection class with the type set to the specified class.
96
96
  # This allows you declare arrays of embedded documents like:
97
97
  #
98
98
  # class Thing < MongoModel::EmbeddedDocument
99
99
  # property :name, String
100
100
  # end
101
- #
101
+ #
102
102
  # class MyModel < MongoModel::Document
103
103
  # property :things, Collection[Thing]
104
104
  # end
@@ -124,9 +124,9 @@ module MongoModel
124
124
  collection
125
125
  end
126
126
  end
127
-
127
+
128
128
  alias of []
129
-
129
+
130
130
  def cast(value)
131
131
  case value
132
132
  when Array
@@ -138,7 +138,7 @@ module MongoModel
138
138
  new(Array(value))
139
139
  end
140
140
  end
141
-
141
+
142
142
  def from_mongo(value)
143
143
  case value
144
144
  when Array
@@ -147,11 +147,11 @@ module MongoModel
147
147
  from_mongo([value])
148
148
  end
149
149
  end
150
-
150
+
151
151
  def converter
152
152
  @converter ||= Types.converter_for(type)
153
153
  end
154
-
154
+
155
155
  private
156
156
  def instantiate(item)
157
157
  if item.is_a?(Hash) && item['_type']
@@ -161,18 +161,18 @@ module MongoModel
161
161
  end
162
162
  end
163
163
  end
164
-
164
+
165
165
  private
166
166
  def convert(value)
167
167
  converter.cast(value)
168
168
  end
169
-
169
+
170
170
  def convert_for_add(value)
171
171
  result = convert(value)
172
172
  result.parent_document = lambda { parent_document } if result.respond_to?(:parent_document=)
173
173
  result
174
174
  end
175
-
175
+
176
176
  def converter
177
177
  self.class.converter
178
178
  end