Module: SimplyCouch::Model::Finders

Includes:
PaginationOptions
Included in:
ClassMethods
Defined in:
lib/simply_couch/model/finders.rb

Instance Method Summary collapse

Methods included from PaginationOptions

#with_pagination_options

Instance Method Details

#all(*args) ⇒ Object



52
53
54
# File 'lib/simply_couch/model/finders.rb', line 52

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

#count(options = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/simply_couch/model/finders.rb', line 65

def count(options = {})
  options.assert_valid_keys(:with_deleted)
  with_deleted = options[:with_deleted]

  if with_deleted || !soft_deleting_enabled?
    database.view(all_documents(reduce: true))
  else
    database.view(all_documents_without_deleted(reduce: true))
  end
end

#find(*args) ⇒ Object



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?
      with_pagination_options(options) do |options|
        database.view(all_documents(options))
      end
    else
      with_pagination_options(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) # Support for multiple find
      #TODO: extended validation and checking, for array arguments
      raise SimplyCouch::NotImplementedError
    else
      # TODO, this part should be better.
      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

#first(*args) ⇒ Object



56
57
58
# File 'lib/simply_couch/model/finders.rb', line 56

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

#last(*args) ⇒ Object



60
61
62
63
# File 'lib/simply_couch/model/finders.rb', line 60

def last(*args)
  options = args.last.is_a?(Hash) ? args.last : {}
  find(:first, options.update(order: :desc))
end