Module: MongoDoc::Document

Defined in:
lib/mongodoc/document.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mongodoc/document.rb', line 16

def self.included(klass)
  klass.class_eval do
    include Attributes
    extend ClassMethods
    extend Finders
    extend NamedScope
    include ::Validatable
    extend Validations::Macros

    alias :id :_id
  end
end

Instance Method Details

#==(other) ⇒ Object



33
34
35
36
# File 'lib/mongodoc/document.rb', line 33

def ==(other)
  return false unless self.class === other
  self.class._attributes.all? {|var| self.send(var) == other.send(var)}
end

#attributesObject



38
39
40
41
42
43
44
# File 'lib/mongodoc/document.rb', line 38

def attributes
  hash = {}
  self.class._attributes.each do |attr|
    hash[attr] = send(attr)
  end
  hash
end

#attributes=(attrs) ⇒ Object



46
47
48
49
50
# File 'lib/mongodoc/document.rb', line 46

def attributes=(attrs)
  attrs.each do |key, value|
    send("#{key}=", value)
  end
end

#initialize(attrs = {}) ⇒ Object



29
30
31
# File 'lib/mongodoc/document.rb', line 29

def initialize(attrs = {})
  self.attributes = attrs
end

#new_record?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/mongodoc/document.rb', line 52

def new_record?
  _id.nil?
end

#removeObject



56
57
58
59
# File 'lib/mongodoc/document.rb', line 56

def remove
  raise UnsupportedOperation.new('Document#remove is not supported for embedded documents') if _root
  remove_document
end

#remove_documentObject



61
62
63
64
# File 'lib/mongodoc/document.rb', line 61

def remove_document
  return _root.remove_document if _root
  _remove
end

#save(validate = true) ⇒ Object



66
67
68
69
70
# File 'lib/mongodoc/document.rb', line 66

def save(validate = true)
  return _root.save(validate) if _root
  return _save(false) unless validate and not valid?
  false
end

#save!Object



72
73
74
75
76
# File 'lib/mongodoc/document.rb', line 72

def save!
  return _root.save! if _root
  raise DocumentInvalidError unless valid?
  _save(true)
end

#to_bson(*args) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/mongodoc/document.rb', line 78

def to_bson(*args)
  {MongoDoc::BSON::CLASS_KEY => self.class.name}.tap do |bson_hash|
    bson_hash['_id'] = _id unless new_record?
    self.class._attributes.each do |name|
      bson_hash[name.to_s] = send(name).to_bson(args)
    end
  end
end

#to_paramObject



87
88
89
# File 'lib/mongodoc/document.rb', line 87

def to_param
  _id.to_s
end

#update_attributes(attrs) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mongodoc/document.rb', line 91

def update_attributes(attrs)
  strict = attrs.delete(:__strict__)
  self.attributes = attrs
  return save if new_record?
  return false unless valid?
  if strict
    _strict_update_attributes(_path_to_root(self, attrs), false)
  else
    _naive_update_attributes(_path_to_root(self, attrs), false)
  end
end

#update_attributes!(attrs) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/mongodoc/document.rb', line 103

def update_attributes!(attrs)
  strict = attrs.delete(:__strict__)
  self.attributes = attrs
  return save! if new_record?
  raise DocumentInvalidError unless valid?
  if strict
    _strict_update_attributes(_path_to_root(self, attrs), true)
  else
    _naive_update_attributes(_path_to_root(self, attrs), true)
  end
end