Class: MongoModel::Collection
- Inherits:
-
Array
- Object
- Array
- MongoModel::Collection
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)
#parent_document, #parent_document=
Constructor Details
- (Collection) initialize(array = [])
A new instance of 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
+ (Object) [](type)
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 model.things = [{: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
|
+ (Object) cast(value)
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
|
+ (Object) converter
151
152
153
|
# File 'lib/mongomodel/support/collection.rb', line 151
def converter
@converter ||= Types.converter_for(type)
end
|
+ (Object) from_mongo(value)
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
|
+ (Object) inspect
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
- (Object) +(other)
42
43
44
|
# File 'lib/mongomodel/support/collection.rb', line 42
def +(other)
self.class.new(super(other))
end
|
- (Object) <<(value)
32
33
34
|
# File 'lib/mongomodel/support/collection.rb', line 32
def <<(value)
super(convert_for_add(value))
end
|
- (Object) []=(index, value)
28
29
30
|
# File 'lib/mongomodel/support/collection.rb', line 28
def []=(index, value)
super(index, convert_for_add(value))
end
|
- (Object) build(value = {})
36
37
38
39
40
|
# File 'lib/mongomodel/support/collection.rb', line 36
def build(value={})
value = convert(value)
self << value
value
end
|
- (Object) concat(values)
46
47
48
|
# File 'lib/mongomodel/support/collection.rb', line 46
def concat(values)
super(values.map { |v| convert_for_add(v) })
end
|
- (Object) delete(value)
50
51
52
|
# File 'lib/mongomodel/support/collection.rb', line 50
def delete(value)
super(convert(value))
end
|
- (Object) embedded_documents
82
83
84
|
# File 'lib/mongomodel/support/collection.rb', line 82
def embedded_documents
select { |item| item.is_a?(EmbeddedDocument) }
end
|
- (Boolean) include?(value)
54
55
56
|
# File 'lib/mongomodel/support/collection.rb', line 54
def include?(value)
super(convert(value))
end
|
- (Object) index(value)
58
59
60
|
# File 'lib/mongomodel/support/collection.rb', line 58
def index(value)
super(convert(value))
end
|
- (Object) insert(index, value)
62
63
64
|
# File 'lib/mongomodel/support/collection.rb', line 62
def insert(index, value)
super(index, convert_for_add(value))
end
|
- (Object) push(*values)
66
67
68
|
# File 'lib/mongomodel/support/collection.rb', line 66
def push(*values)
super(*values.map { |v| convert_for_add(v) })
end
|
- (Object) rindex(value)
70
71
72
|
# File 'lib/mongomodel/support/collection.rb', line 70
def rindex(value)
super(convert(value))
end
|
- (Object) to_mongo
78
79
80
|
# File 'lib/mongomodel/support/collection.rb', line 78
def to_mongo
ARRAY_CONVERTER.to_mongo(self)
end
|
- (Object) unshift(*values)
74
75
76
|
# File 'lib/mongomodel/support/collection.rb', line 74
def unshift(*values)
super(*values.map { |v| convert_for_add(v) })
end
|