Module: SimplyStored::Couch::Finders

Included in:
ClassMethods
Defined in:
lib/simply_stored/couch/finders.rb

Instance Method Summary collapse

Instance Method Details

#all(*args) ⇒ Object



36
37
38
# File 'lib/simply_stored/couch/finders.rb', line 36

def all(*args)
  find(:all, *args)
end

#count(options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/simply_stored/couch/finders.rb', line 48

def count(options = {})
  options.assert_valid_keys(:with_deleted)
  with_deleted = options[:with_deleted]
  
  if with_deleted || !soft_deleting_enabled?
    CouchPotato.database.view(all_documents(:reduce => true))
  else
    CouchPotato.database.view(all_documents_without_deleted(:reduce => true))
  end
end

#find(*args) ⇒ Object



4
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
# File 'lib/simply_stored/couch/finders.rb', line 4

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)
  
  case what
  when :all
    if with_deleted || !soft_deleting_enabled?
      CouchPotato.database.view(all_documents(*args))
    else
      CouchPotato.database.view(all_documents_without_deleted(options.update(:include_docs => true)))
    end
  when :first
    if with_deleted || !soft_deleting_enabled?
      CouchPotato.database.view(all_documents(:limit => 1, :include_docs => true)).first
    else
      CouchPotato.database.view(all_documents_without_deleted(:limit => 1, :include_docs => true)).first
    end
  else          
    raise SimplyStored::Error, "Can't load record without an id" if what.nil?
    document = CouchPotato.database.load_document(what)
    if document.nil? or !document.is_a?(self) or (document.deleted? && !with_deleted)
      raise(SimplyStored::RecordNotFound, "#{self.name} could not be found with #{what.inspect}")
    end
    document
  end
end

#first(*args) ⇒ Object



40
41
42
# File 'lib/simply_stored/couch/finders.rb', line 40

def first(*args)
  find(:first, *args)
end

#last(*args) ⇒ Object



44
45
46
# File 'lib/simply_stored/couch/finders.rb', line 44

def last(*args)
  find(:first, {:order => :desc}, *args)
end