Module: Mongo::Object
- Defined in:
- lib/mongo/object/object.rb
Instance Attribute Summary collapse
-
#_id ⇒ Object
Returns the value of attribute _id.
-
#_parent ⇒ Object
Returns the value of attribute _parent.
Class Method Summary collapse
Instance Method Summary collapse
- #_id? ⇒ Boolean
- #create_object(collection, options) ⇒ Object
- #delete_object(collection, options) ⇒ Object
-
#generate_id ⇒ Object
Override it to generate Your custom ids.
- #inspect ⇒ Object (also: #to_s)
- #new? ⇒ Boolean (also: #new_record?)
-
#persistent_instance_variable_names(*args) ⇒ Object
Skipping variables starting with @_, usually they have specific meaning and used for things like cache.
- #save_object(collection, options) ⇒ Object
-
#to_mongo ⇒ Object
(also: #to_hash)
Convert object to document (with nested documents & arrays).
- #update_object(collection, options) ⇒ Object
Instance Attribute Details
#_id ⇒ Object
Returns the value of attribute _id.
2 3 4 |
# File 'lib/mongo/object/object.rb', line 2 def _id @_id end |
#_parent ⇒ Object
Returns the value of attribute _parent.
2 3 4 |
# File 'lib/mongo/object/object.rb', line 2 def _parent @_parent end |
Class Method Details
.build(doc) ⇒ Object
83 84 85 |
# File 'lib/mongo/object/object.rb', line 83 def build doc doc && _build(doc, nil) end |
Instance Method Details
#_id? ⇒ Boolean
4 |
# File 'lib/mongo/object/object.rb', line 4 def _id?; !!_id end |
#create_object(collection, options) ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/mongo/object/object.rb', line 8 def create_object collection, doc = to_mongo # Generating custom id if option enabled. doc['_id'] = generate_id if Mongo.defaults[:generate_id] id = collection.create doc, self._id = id id end |
#delete_object(collection, options) ⇒ Object
25 26 27 28 |
# File 'lib/mongo/object/object.rb', line 25 def delete_object collection, id = _id || "can't delete object without _id (#{self})!" collection.delete({_id: id}, ) end |
#generate_id ⇒ Object
Override it to generate Your custom ids.
63 64 65 |
# File 'lib/mongo/object/object.rb', line 63 def generate_id generate_random_string_id end |
#inspect ⇒ Object Also known as: to_s
67 68 69 70 71 |
# File 'lib/mongo/object/object.rb', line 67 def inspect h = to_hash h.delete '_class' "#<#{self.class}:#{h.inspect}>" end |
#new? ⇒ Boolean Also known as: new_record?
5 |
# File 'lib/mongo/object/object.rb', line 5 def new?; !_id end |
#persistent_instance_variable_names(*args) ⇒ Object
Skipping variables starting with @_, usually they have specific meaning and used for things like cache.
40 41 42 |
# File 'lib/mongo/object/object.rb', line 40 def persistent_instance_variable_names *args instance_variables(*args).select{|n| n !~ /^@_/} end |
#save_object(collection, options) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/mongo/object/object.rb', line 30 def save_object collection, if _id update_object collection, else create_object collection, end end |
#to_mongo ⇒ Object Also known as: to_hash
Convert object to document (with nested documents & arrays).
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/mongo/object/object.rb', line 45 def to_mongo {}.tap do |h| # Copy instance variables. persistent_instance_variable_names.each do |iv_name| k = iv_name.to_s[1..-1] v = instance_variable_get iv_name h[k] = v.to_mongo end # Adding _id & _class. h['_id'] = _id if _id h['_class'] = self.class.name || \ raise("unknow class name for model #{h.inspect}!") end end |
#update_object(collection, options) ⇒ Object
19 20 21 22 23 |
# File 'lib/mongo/object/object.rb', line 19 def update_object collection, id = _id || "can't update object without _id (#{self})!" doc = to_mongo collection.update({_id: id}, doc, ) end |