Module: Mongo::Object
- Defined in:
- lib/mongo/object/object.rb
Instance Attribute Summary collapse
-
#_id ⇒ Object
Returns the value of attribute _id.
-
#_not_new ⇒ Object
Returns the value of attribute _not_new.
-
#_parent ⇒ Object
Returns the value of attribute _parent.
Class Method Summary collapse
Instance Method Summary collapse
- #_id? ⇒ Boolean
- #_to_mongo(options = {}) ⇒ Object
- #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
-
#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_hash ⇒ Object
-
#to_mongo ⇒ Object
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 |
#_not_new ⇒ Object
Returns the value of attribute _not_new.
2 3 4 |
# File 'lib/mongo/object/object.rb', line 2 def _not_new @_not_new 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
.constantize(class_name) ⇒ Object
110 111 112 113 114 115 116 117 |
# File 'lib/mongo/object/object.rb', line 110 def constantize class_name @constantize_cache ||= {} unless klass = @constantize_cache[class_name] klass = eval class_name, TOPLEVEL_BINDING, __FILE__, __LINE__ @constantize_cache[class_name] = klass end klass end |
.from_mongo(doc) ⇒ Object
106 107 108 |
# File 'lib/mongo/object/object.rb', line 106 def from_mongo doc _from_mongo doc, nil end |
Instance Method Details
#_id? ⇒ Boolean
4 |
# File 'lib/mongo/object/object.rb', line 4 def _id?; !!_id end |
#_to_mongo(options = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mongo/object/object.rb', line 52 def _to_mongo = {} 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] = convert_object 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}!") h end |
#create_object(collection, options) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# 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 self._not_new = true id end |
#delete_object(collection, options) ⇒ Object
28 29 30 31 |
# File 'lib/mongo/object/object.rb', line 28 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.
73 74 75 |
# File 'lib/mongo/object/object.rb', line 73 def generate_id generate_random_string_id end |
#inspect ⇒ Object Also known as: to_s
77 78 79 80 81 |
# File 'lib/mongo/object/object.rb', line 77 def inspect h = to_hash h.delete '_class' "#<#{self.class}:#{h.inspect}>" end |
#new? ⇒ Boolean
6 |
# File 'lib/mongo/object/object.rb', line 6 def new?; !_not_new end |
#persistent_instance_variable_names(*args) ⇒ Object
Skipping variables starting with @_, usually they have specific meaning and used for things like cache.
43 44 45 |
# File 'lib/mongo/object/object.rb', line 43 def persistent_instance_variable_names *args instance_variables(*args).select{|n| n !~ /^@_/} end |
#save_object(collection, options) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/mongo/object/object.rb', line 33 def save_object collection, if _not_new update_object collection, else create_object collection, end end |
#to_hash ⇒ Object
68 69 70 |
# File 'lib/mongo/object/object.rb', line 68 def to_hash Hash.respond_to?(:symbolize) ? Hash.symbolize(to_mongo) : to_mongo end |
#to_mongo ⇒ Object
Convert object to document (with nested documents & arrays).
48 49 50 |
# File 'lib/mongo/object/object.rb', line 48 def to_mongo convert_object self, :_to_mongo end |
#update_object(collection, options) ⇒ Object
22 23 24 25 26 |
# File 'lib/mongo/object/object.rb', line 22 def update_object collection, id = _id || "can't update object without _id (#{self})!" doc = to_mongo collection.update({_id: id}, doc, ) end |