Module: MongoODM::Document::Persistence::ClassMethods

Defined in:
lib/mongo_odm/document/persistence.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/mongo_odm/document/persistence.rb', line 131

def method_missing(method_name, *args, &block)
  if cursor.respond_to?(method_name)
    cursor.send(method_name, *args, &block)
  else
    super
  end
end

Instance Method Details

#collectionObject



72
73
74
75
76
77
78
# File 'lib/mongo_odm/document/persistence.rb', line 72

def collection
  @collection ||= if self.superclass.included_modules.include?(MongoODM::Document)
                    self.superclass.collection
                  else
                    MongoODM::Collection.new(name.tableize, MongoODM.database)
                  end
end

#cursorObject



114
115
116
# File 'lib/mongo_odm/document/persistence.rb', line 114

def cursor
  @cursor ||= find.to_cursor
end

#destroy_all(*args) ⇒ Object



118
119
120
121
122
123
# File 'lib/mongo_odm/document/persistence.rb', line 118

def destroy_all(*args)
  documents = find(*args)
  count = documents.count
  documents.each { |doc| doc.destroy }
  count
end

#find(selector = {}, opts = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/mongo_odm/document/persistence.rb', line 91

def find(selector = {}, opts = {})
  if opts.blank? && selector.is_a?(String)
    MongoODM::Criteria.new(self, {:_id => BSON::ObjectId.from_string(selector)})
  elsif opts.blank? && selector.is_a?(BSON::ObjectId)
    MongoODM::Criteria.new(self, {:_id => selector})
  else
    MongoODM::Criteria.new(self, selector, opts)
  end
end

#limit(number_to_return = nil) ⇒ Object



110
111
112
# File 'lib/mongo_odm/document/persistence.rb', line 110

def limit(number_to_return = nil)
  MongoODM::Criteria.new(self, {}, {:limit => number_to_return})
end

#set_collection(name_or_collection) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/mongo_odm/document/persistence.rb', line 80

def set_collection(name_or_collection)
  @collection = case name_or_collection
                when MongoODM::Collection
                  name_or_collection
                when String, Symbol
                  MongoODM::Collection.new(name_or_collection, MongoODM.database)
                else
                  raise "MongoODM::Collection instance or valid collection name required"
                end
end

#skip(number_to_skip = nil) ⇒ Object



106
107
108
# File 'lib/mongo_odm/document/persistence.rb', line 106

def skip(number_to_skip = nil)
  MongoODM::Criteria.new(self, {}, {:skip => number_to_skip})
end

#sort(key_or_list, direction = nil) ⇒ Object



101
102
103
104
# File 'lib/mongo_odm/document/persistence.rb', line 101

def sort(key_or_list, direction = nil)
  order = key_or_list.is_a?(Array) ? key_or_list : direction.nil? ? [key_or_list, :asc] : [key_or_list, direction]
  MongoODM::Criteria.new(self, {}, {:sort => order})
end

#type_cast(value) ⇒ Object



125
126
127
128
129
# File 'lib/mongo_odm/document/persistence.rb', line 125

def type_cast(value)
  return nil if value.nil?
  return value if value.class == self
  new(value)
end