class MongoModel::Collection

Constants

ARRAY_CONVERTER

Public Class Methods

[](type) click to toggle source

Create a new MongoModel::Collection class with the type set to the specified class. This allows you declare arrays of embedded documents like:

class Thing < MongoModel::EmbeddedDocument
  property :name, String
end

class MyModel < MongoModel::Document
  property :things, Collection[Thing]
end

If you don't declare a default on a property that has a Collection type, the default will be automatically set to an empty Collection.

This method is aliased as of, so you can use the alternative syntax:

property :things, Collection.of(Thing)

Examples:

model = MyModel.new
model.things # => []
model.things << {:name => "Thing One"}
model.things # => [#<Thing name: "Thing One">]
model.things = [{:name => "Thing Two"}] # => [#<Thing name: "Thing Two">]
# File lib/mongomodel/support/collection.rb, line 119
def [](type)
  @collection_class_cache ||= {}
  @collection_class_cache[type] ||= begin
    collection = Class.new(self)
    collection.type = type
    collection
  end
end
Also aliased as: of
cast(value) click to toggle source
# File lib/mongomodel/support/collection.rb, line 130
def cast(value)
  case value
  when Array
    new(value)
  when Hash
    value.stringify_keys!
    value['_collection'] ? cast(value['items']) : new([value])
  else
    new(Array(value))
  end
end
converter() click to toggle source
# File lib/mongomodel/support/collection.rb, line 151
def converter
  @converter ||= Types.converter_for(type)
end
from_mongo(value) click to toggle source
# File lib/mongomodel/support/collection.rb, line 142
def from_mongo(value)
  case value
  when Array
    new(value.map { |i| instantiate(i) })
  else
    from_mongo([value])
  end
end
inspect() click to toggle source
# File lib/mongomodel/support/collection.rb, line 87
def inspect
  if type == Object
    "Collection"
  else
    "Collection[#{type}]"
  end
end
new(array=[]) click to toggle source
Calls superclass method
# File lib/mongomodel/support/collection.rb, line 24
def initialize(array=[])
  super(array.map { |i| convert_for_add(i) })
end
of(type)
Alias for: []

Private Class Methods

instantiate(item) click to toggle source
# File lib/mongomodel/support/collection.rb, line 156
def instantiate(item)
  if item.is_a?(Hash) && item['_type']
    item['_type'].constantize.from_mongo(item)
  else
    converter.from_mongo(item)
  end
end

Public Instance Methods

+(other) click to toggle source
Calls superclass method
# File lib/mongomodel/support/collection.rb, line 42
def +(other)
  self.class.new(super(other))
end
<<(value) click to toggle source
Calls superclass method
# File lib/mongomodel/support/collection.rb, line 32
def <<(value)
  super(convert_for_add(value))
end
[]=(index, value) click to toggle source
Calls superclass method
# File lib/mongomodel/support/collection.rb, line 28
def []=(index, value)
  super(index, convert_for_add(value))
end
build(value={}) click to toggle source
# File lib/mongomodel/support/collection.rb, line 36
def build(value={})
  value = convert(value)
  self << value
  value
end
concat(values) click to toggle source
Calls superclass method
# File lib/mongomodel/support/collection.rb, line 46
def concat(values)
  super(values.map { |v| convert_for_add(v) })
end
delete(value) click to toggle source
Calls superclass method
# File lib/mongomodel/support/collection.rb, line 50
def delete(value)
  super(convert(value))
end
embedded_documents() click to toggle source
# File lib/mongomodel/support/collection.rb, line 82
def embedded_documents
  select { |item| item.is_a?(EmbeddedDocument) }
end
include?(value) click to toggle source
Calls superclass method
# File lib/mongomodel/support/collection.rb, line 54
def include?(value)
  super(convert(value))
end
index(value) click to toggle source
Calls superclass method
# File lib/mongomodel/support/collection.rb, line 58
def index(value)
  super(convert(value))
end
insert(index, value) click to toggle source
Calls superclass method
# File lib/mongomodel/support/collection.rb, line 62
def insert(index, value)
  super(index, convert_for_add(value))
end
push(*values) click to toggle source
Calls superclass method
# File lib/mongomodel/support/collection.rb, line 66
def push(*values)
  super(*values.map { |v| convert_for_add(v) })
end
rindex(value) click to toggle source
Calls superclass method
# File lib/mongomodel/support/collection.rb, line 70
def rindex(value)
  super(convert(value))
end
to_mongo() click to toggle source
# File lib/mongomodel/support/collection.rb, line 78
def to_mongo
  ARRAY_CONVERTER.to_mongo(self)
end
unshift(*values) click to toggle source
Calls superclass method
# File lib/mongomodel/support/collection.rb, line 74
def unshift(*values)
  super(*values.map { |v| convert_for_add(v) })
end

Private Instance Methods

convert(value) click to toggle source
# File lib/mongomodel/support/collection.rb, line 166
def convert(value)
  converter.cast(value)
end
convert_for_add(value) click to toggle source
# File lib/mongomodel/support/collection.rb, line 170
def convert_for_add(value)
  result = convert(value)
  result.parent_document = lambda { parent_document } if result.respond_to?(:parent_document=)
  result
end
converter() click to toggle source
# File lib/mongomodel/support/collection.rb, line 176
def converter
  self.class.converter
end