Class: MongoModel::Collection

Inherits:
Array
  • Object
show all
Includes:
DocumentParent
Defined in:
lib/mongomodel/support/collection.rb

Defined Under Namespace

Modules: PropertyDefaults

Constant Summary

ARRAY_CONVERTER =
Types.converter_for(Array)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DocumentParent

#parent_document, #parent_document=

Constructor Details

#initialize(array = []) ⇒ Collection



24
25
26
# File 'lib/mongomodel/support/collection.rb', line 24

def initialize(array=[])
  super(array.map { |i| convert_for_add(i) })
end

Class Method Details

.[](type) ⇒ Object Also known as: of

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">]


119
120
121
122
123
124
125
126
# 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

.cast(value) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
# 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

.converterObject



151
152
153
# File 'lib/mongomodel/support/collection.rb', line 151

def converter
  @converter ||= Types.converter_for(type)
end

.from_mongo(value) ⇒ Object



142
143
144
145
146
147
148
149
# 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

.inspectObject



87
88
89
90
91
92
93
# File 'lib/mongomodel/support/collection.rb', line 87

def inspect
  if type == Object
    "Collection"
  else
    "Collection[#{type}]"
  end
end

Instance Method Details

#+(other) ⇒ Object



42
43
44
# File 'lib/mongomodel/support/collection.rb', line 42

def +(other)
  self.class.new(super(other))
end

#<<(value) ⇒ Object



32
33
34
# File 'lib/mongomodel/support/collection.rb', line 32

def <<(value)
  super(convert_for_add(value))
end

#[]=(index, value) ⇒ Object



28
29
30
# File 'lib/mongomodel/support/collection.rb', line 28

def []=(index, value)
  super(index, convert_for_add(value))
end

#build(value = {}) ⇒ Object



36
37
38
39
40
# File 'lib/mongomodel/support/collection.rb', line 36

def build(value={})
  value = convert(value)
  self << value
  value
end

#concat(values) ⇒ Object



46
47
48
# File 'lib/mongomodel/support/collection.rb', line 46

def concat(values)
  super(values.map { |v| convert_for_add(v) })
end

#delete(value) ⇒ Object



50
51
52
# File 'lib/mongomodel/support/collection.rb', line 50

def delete(value)
  super(convert(value))
end

#embedded_documentsObject



82
83
84
# File 'lib/mongomodel/support/collection.rb', line 82

def embedded_documents
  select { |item| item.is_a?(EmbeddedDocument) }
end

#include?(value) ⇒ Boolean



54
55
56
# File 'lib/mongomodel/support/collection.rb', line 54

def include?(value)
  super(convert(value))
end

#index(value) ⇒ Object



58
59
60
# File 'lib/mongomodel/support/collection.rb', line 58

def index(value)
  super(convert(value))
end

#insert(index, value) ⇒ Object



62
63
64
# File 'lib/mongomodel/support/collection.rb', line 62

def insert(index, value)
  super(index, convert_for_add(value))
end

#push(*values) ⇒ Object



66
67
68
# File 'lib/mongomodel/support/collection.rb', line 66

def push(*values)
  super(*values.map { |v| convert_for_add(v) })
end

#rindex(value) ⇒ Object



70
71
72
# File 'lib/mongomodel/support/collection.rb', line 70

def rindex(value)
  super(convert(value))
end

#to_mongoObject



78
79
80
# File 'lib/mongomodel/support/collection.rb', line 78

def to_mongo
  ARRAY_CONVERTER.to_mongo(self)
end

#unshift(*values) ⇒ Object



74
75
76
# File 'lib/mongomodel/support/collection.rb', line 74

def unshift(*values)
  super(*values.map { |v| convert_for_add(v) })
end