Class: MongoMapperExt::Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/mongomapper_ext/paginator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(total_entries, current_page, per_page = nil) ⇒ Paginator

Returns a new instance of Paginator.



10
11
12
13
14
# File 'lib/mongomapper_ext/paginator.rb', line 10

def initialize(total_entries, current_page, per_page=nil)
  @total_entries    = total_entries.to_i
  self.per_page     = per_page
  self.current_page = current_page
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



49
50
51
# File 'lib/mongomapper_ext/paginator.rb', line 49

def method_missing(name, *args, &block)
  @subject.send(name, *args, &block)
end

Instance Attribute Details

#current_pageObject

Returns the value of attribute current_page.



7
8
9
# File 'lib/mongomapper_ext/paginator.rb', line 7

def current_page
  @current_page
end

#per_pageObject Also known as: limit

Returns the value of attribute per_page.



7
8
9
# File 'lib/mongomapper_ext/paginator.rb', line 7

def per_page
  @per_page
end

#subjectObject

Returns the value of attribute subject.



6
7
8
# File 'lib/mongomapper_ext/paginator.rb', line 6

def subject
  @subject
end

#total_entriesObject (readonly)

Returns the value of attribute total_entries.



7
8
9
# File 'lib/mongomapper_ext/paginator.rb', line 7

def total_entries
  @total_entries
end

Instance Method Details

#===(other) ⇒ Object



45
46
47
# File 'lib/mongomapper_ext/paginator.rb', line 45

def ===(other)
  other === subject
end

#next_pageObject



28
29
30
# File 'lib/mongomapper_ext/paginator.rb', line 28

def next_page
  current_page < total_pages ? (current_page + 1) : nil
end

#out_of_bounds?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/mongomapper_ext/paginator.rb', line 20

def out_of_bounds?
  current_page > total_pages
end

#previous_pageObject



24
25
26
# File 'lib/mongomapper_ext/paginator.rb', line 24

def previous_page
  current_page > 1 ? (current_page - 1) : nil
end

#respond_to?(name, *args, &block) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/mongomapper_ext/paginator.rb', line 53

def respond_to?(name, *args, &block)
  super || @subject.respond_to?(name, *args, &block)
end

#send(method, *args, &block) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/mongomapper_ext/paginator.rb', line 37

def send(method, *args, &block)
  if respond_to?(method)
    super
  else
    subject.send(method, *args, &block)
  end
end

#skipObject Also known as: offset



32
33
34
# File 'lib/mongomapper_ext/paginator.rb', line 32

def skip
  (current_page - 1) * per_page
end

#total_pagesObject



16
17
18
# File 'lib/mongomapper_ext/paginator.rb', line 16

def total_pages
  (total_entries / per_page.to_f).ceil
end