5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/simply_couch/model/finders.rb', line 5
def find(*args)
what = args.shift
options = args.last.is_a?(Hash) ? args.last : {}
if options && order = options.delete(:order)
options[:descending] = true if order == :desc
end
with_deleted = options.delete(:with_deleted)
result = case what
when :all
if options.has_key?(:page)
options[:total_entries] = count
end
if with_deleted || !soft_deleting_enabled?
(options) do |options|
database.view(all_documents(options))
end
else
(options) do |options|
database.view(all_documents_without_deleted(options.update(include_docs: true)))
end
end
when :first
if with_deleted || !soft_deleting_enabled?
database.view(all_documents(options.update(limit: 1, include_docs: true))).first
else
database.view(all_documents_without_deleted(options.update(limit: 1, include_docs: true))).first
end
else
raise SimplyCouch::Error, "Can't load record without an id" if what.nil?
document = database.load_document(what)
if what.is_a?(Array) raise SimplyCouch::NotImplementedError
else
raise(SimplyCouch::RecordNotFound, "#{self.name} could not be found with #{what.inspect}") unless document.present?
raise(SimplyCouch::RecordNotFound, "#{self.name} could not be found with #{what.inspect} — got #{document.class.name}") unless document.is_a?(self)
if document.deleted? && !with_deleted
raise(SimplyCouch::RecordNotFound, "#{self.name} could not be found with #{what.inspect}")
end
end
document
end
end
|