Module: Mongo::Object

Defined in:
lib/mongo/object/object.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_idObject

Returns the value of attribute _id.



2
3
4
# File 'lib/mongo/object/object.rb', line 2

def _id
  @_id
end

#_not_newObject

Returns the value of attribute _not_new.



2
3
4
# File 'lib/mongo/object/object.rb', line 2

def _not_new
  @_not_new
end

#_parentObject

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

Returns:

  • (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 options = {}
  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, options
  doc = to_mongo

  # Generating custom id if option enabled.
  doc['_id'] ||= generate_id if Mongo.defaults[:generate_id]

  id = collection.create doc, options

  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, options
  id = _id || "can't delete object without _id (#{self})!"
  collection.delete({_id: id}, options)
end

#generate_idObject

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

#inspectObject 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

Returns:

  • (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, options
  if _not_new
    update_object collection, options
  else
    create_object collection, options
  end
end

#to_hashObject



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_mongoObject

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, options
  id = _id || "can't update object without _id (#{self})!"
  doc = to_mongo
  collection.update({_id: id}, doc, options)
end