Class: MongoDoc::Document
Instance Attribute Summary collapse
-
#_id ⇒ Object
(also: #id, #to_param)
Returns the value of attribute _id.
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Attributes
_attributes, extended, has_many, has_one, key, type_name_with_module
Constructor Details
#initialize(attrs = {}) ⇒ Document
Returns a new instance of Document.
18
19
20
|
# File 'lib/mongodoc/document.rb', line 18
def initialize(attrs = {})
self.attributes = attrs
end
|
Instance Attribute Details
#_id ⇒ Object
Also known as:
id, to_param
Returns the value of attribute _id.
14
15
16
|
# File 'lib/mongodoc/document.rb', line 14
def _id
@_id
end
|
Class Method Details
._create(instance, safe) ⇒ Object
133
134
135
|
# File 'lib/mongodoc/document.rb', line 133
def _create(instance, safe)
instance._id = collection.insert(instance, :safe => safe)
end
|
.bson_create(bson_hash, options = {}) ⇒ Object
71
72
73
74
75
76
77
|
# File 'lib/mongodoc/document.rb', line 71
def bson_create(bson_hash, options = {})
new.tap do |obj|
bson_hash.each do |name, value|
obj.send("#{name}=", MongoDoc::BSON.decode(value, options))
end
end
end
|
.collection ⇒ Object
79
80
81
|
# File 'lib/mongodoc/document.rb', line 79
def collection
@collection ||= MongoDoc::Collection.new(collection_name)
end
|
.collection_name ⇒ Object
83
84
85
|
# File 'lib/mongodoc/document.rb', line 83
def collection_name
self.to_s.tableize.gsub('/', '.')
end
|
87
88
89
|
# File 'lib/mongodoc/document.rb', line 87
def count
collection.count
end
|
.create(attrs = {}) ⇒ Object
91
92
93
94
95
|
# File 'lib/mongodoc/document.rb', line 91
def create(attrs = {})
instance = new(attrs)
_create(instance, false) if instance.valid?
instance
end
|
.create!(attrs = {}) ⇒ Object
97
98
99
100
101
102
|
# File 'lib/mongodoc/document.rb', line 97
def create!(attrs = {})
instance = new(attrs)
raise MongoDoc::DocumentInvalidError unless instance.valid?
_create(instance, true)
instance
end
|
104
105
106
|
# File 'lib/mongodoc/document.rb', line 104
def criteria
Criteria.new(self)
end
|
.find_one(id) ⇒ Object
108
109
110
|
# File 'lib/mongodoc/document.rb', line 108
def find_one(id)
MongoDoc::BSON.decode(collection.find_one(id))
end
|
Instance Method Details
#==(other) ⇒ Object
22
23
24
25
|
# File 'lib/mongodoc/document.rb', line 22
def ==(other)
return false unless self.class === other
self.class._attributes.all? {|var| self.send(var) == other.send(var)}
end
|
#attributes=(attrs) ⇒ Object
27
28
29
30
31
|
# File 'lib/mongodoc/document.rb', line 27
def attributes=(attrs)
attrs.each do |key, value|
send("#{key}=", value)
end
end
|
#new_record? ⇒ Boolean
33
34
35
|
# File 'lib/mongodoc/document.rb', line 33
def new_record?
_id.nil?
end
|
#save(validate = true) ⇒ Object
37
38
39
40
41
|
# File 'lib/mongodoc/document.rb', line 37
def save(validate = true)
return _root.save(validate) if _root
return _save(false) unless validate and not valid?
false
end
|
43
44
45
46
47
|
# File 'lib/mongodoc/document.rb', line 43
def save!
return _root.save! if _root
raise DocumentInvalidError unless valid?
_save(true)
end
|
#to_bson(*args) ⇒ Object
49
50
51
52
53
54
55
56
|
# File 'lib/mongodoc/document.rb', line 49
def to_bson(*args)
{MongoDoc::BSON::CLASS_KEY => self.class.name}.tap do |bson_hash|
bson_hash['_id'] = _id unless _id.nil?
self.class._attributes.each do |name|
bson_hash[name.to_s] = send(name).to_bson(args)
end
end
end
|
#update_attributes(attrs) ⇒ Object
58
59
60
61
62
|
# File 'lib/mongodoc/document.rb', line 58
def update_attributes(attrs)
self.attributes = attrs
return _propose_update_attributes(self, path_to_root(attrs), false) if valid?
false
end
|
#update_attributes!(attrs) ⇒ Object
64
65
66
67
68
|
# File 'lib/mongodoc/document.rb', line 64
def update_attributes!(attrs)
self.attributes = attrs
raise DocumentInvalidError unless valid?
_propose_update_attributes(self, path_to_root(attrs), true)
end
|