Class: Arrest::OrderedCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/arrest/helper/ordered_collection.rb

Overview

< BasicObject

Instance Method Summary collapse

Constructor Details

#initialize(context, class_or_class_name, base_url) ⇒ OrderedCollection

Returns a new instance of OrderedCollection.



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/arrest/helper/ordered_collection.rb', line 4

def initialize(context,class_or_class_name, base_url)
  @context = context
  if class_or_class_name.is_a?(String) || class_or_class_name.is_a?(Symbol)
    @clazz_name = (StringUtils.classify(class_name.to_s))
  else
    @clazz = class_or_class_name
  end
  @base_url = base_url
  @collection = nil
  @page = 1
  @page_size = nil
  @per_page = 5
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



18
19
20
# File 'lib/arrest/helper/ordered_collection.rb', line 18

def method_missing(*args, &block)
   collection.send(*args, &block)
end

Instance Method Details

#current_pageObject



59
60
61
# File 'lib/arrest/helper/ordered_collection.rb', line 59

def current_page
  @page
end

#current_page_countObject

:nodoc:



79
80
81
# File 'lib/arrest/helper/ordered_collection.rb', line 79

def current_page_count #:nodoc:
  @page
end

#first_page?Boolean

Returns:



71
72
73
# File 'lib/arrest/helper/ordered_collection.rb', line 71

def first_page?
  current_page == 1
end

#last_page?Boolean

Returns:



75
76
77
# File 'lib/arrest/helper/ordered_collection.rb', line 75

def last_page?
  current_page >= num_pages
end

#limit(count) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/arrest/helper/ordered_collection.rb', line 22

def limit(count)
  if count != @page_size
    @collection = nil
  end
  @page_size = count
  self
end

#limit_valueObject

for kaminari

TODO: move to external module



51
52
53
# File 'lib/arrest/helper/ordered_collection.rb', line 51

def limit_value #:nodoc:
  @page_size || 0
end

#num_pagesObject



63
64
65
66
67
68
69
# File 'lib/arrest/helper/ordered_collection.rb', line 63

def num_pages
  if @page_size
    (total_count.to_f / @page_size).ceil
  else
    1
  end
end

#offset(count) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/arrest/helper/ordered_collection.rb', line 30

def offset(count)
  if @page_size
    new_page = count / @page_size
  else
    new_page = 1
  end
  if new_page != @page 
    @collection = nil
  end
  @page = new_page
  self
end

#offset_valueObject

:nodoc:



55
56
57
# File 'lib/arrest/helper/ordered_collection.rb', line 55

def offset_value #:nodoc:
  ((@page_size || 0) * (@page - 1)) || 0
end

#page(num) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/arrest/helper/ordered_collection.rb', line 88

def page(num)
  num ||= 1
  if @page_size != @per_page || @page != num
    @collection = nil
  end
  @page_size = @per_page
  @page = num.to_i
  self
end

#per(num) ⇒ Object



83
84
85
86
# File 'lib/arrest/helper/ordered_collection.rb', line 83

def per(num)
  @per_page = num.to_i
  self
end

#total_countObject



43
44
45
46
# File 'lib/arrest/helper/ordered_collection.rb', line 43

def total_count
  collection() # make sure request was made before
  @total_count
end