Module: MongoDoc::Document

Defined in:
lib/mongo_doc/document.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mongo_doc/document.rb', line 24

def self.included(klass)
  klass.class_eval do
    include Attributes
    include Root
    extend Associations
    extend ClassMethods
    extend Criteria
    extend Finders
    extend Index
    extend Scope
    include ::ActiveModel::Validations
    extend ::ActiveModel::Naming
    extend Validations

    alias id _id
  end
end

Instance Method Details

#==(other) ⇒ Object



50
51
52
53
# File 'lib/mongo_doc/document.rb', line 50

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

#_collectionObject



42
43
44
# File 'lib/mongo_doc/document.rb', line 42

def _collection
  _root and _root._collection or self.class.collection
end

#destroyed?Boolean

Returns:



55
56
57
# File 'lib/mongo_doc/document.rb', line 55

def destroyed?
  _id.nil?
end

#initialize(attrs = {}) ⇒ Object



46
47
48
# File 'lib/mongo_doc/document.rb', line 46

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

#new_record?Boolean

Returns:



59
60
61
# File 'lib/mongo_doc/document.rb', line 59

def new_record?
  _id.nil?
end

#removeObject



63
64
65
66
# File 'lib/mongo_doc/document.rb', line 63

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

#remove_documentObject



68
69
70
71
# File 'lib/mongo_doc/document.rb', line 68

def remove_document
  return _root.remove_document if _root
  _remove
end

#save(validate = true) ⇒ Object



73
74
75
76
77
# File 'lib/mongo_doc/document.rb', line 73

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

#save!Object



79
80
81
82
83
# File 'lib/mongo_doc/document.rb', line 79

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

#to_bson(*args) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/mongo_doc/document.rb', line 85

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



94
95
96
# File 'lib/mongo_doc/document.rb', line 94

def to_param
  _id.to_s
end

#update_attributes(attrs) ⇒ Object



98
99
100
101
102
103
# File 'lib/mongo_doc/document.rb', line 98

def update_attributes(attrs)
  self.attributes = attrs
  return save if new_record?
  return false unless valid?
  _update({}, attrs, false)
end

#update_attributes!(attrs) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/mongo_doc/document.rb', line 105

def update_attributes!(attrs)
  strict = attrs.delete(:__strict__)
  self.attributes = attrs
  return save! if new_record?
  raise DocumentInvalidError unless valid?
  _update({}, attrs, true)
end