Class: Arrest::OrderedCollection
- Inherits:
-
Object
- Object
- Arrest::OrderedCollection
show all
- Defined in:
- lib/arrest/helper/ordered_collection.rb
Overview
Instance Method Summary
collapse
Constructor Details
#initialize(context, class_or_class_name, base_url, query_params = {}) ⇒ OrderedCollection
Returns a new instance of OrderedCollection.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/arrest/helper/ordered_collection.rb', line 6
def initialize(context, class_or_class_name, base_url, query_params = {})
@collection = nil
@context = context
if class_or_class_name.is_a?(String) || class_or_class_name.is_a?(Symbol)
@clazz_name = (StringUtils.classify(class_or_class_name.to_s))
else
@clazz = class_or_class_name
end
@base_url = base_url
@default_page = 1
@page_hash = {}
@sort_hash = {}
@query_params = query_params
define_filters
define_scopes
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args, &block) ⇒ Object
32
33
34
|
# File 'lib/arrest/helper/ordered_collection.rb', line 32
def method_missing(*args, &block)
collection.send(*args, &block)
end
|
Instance Method Details
#build(attributes = {}) ⇒ Object
27
28
29
|
# File 'lib/arrest/helper/ordered_collection.rb', line 27
def build(attributes = {})
resolved_class.new(@context, attributes)
end
|
#current_page ⇒ Object
68
69
70
|
# File 'lib/arrest/helper/ordered_collection.rb', line 68
def current_page
@page_hash[:page] || @default_page
end
|
#current_page_count ⇒ Object
88
89
90
|
# File 'lib/arrest/helper/ordered_collection.rb', line 88
def current_page_count @page_hash[:page] || @default_page
end
|
#first_page? ⇒ Boolean
80
81
82
|
# File 'lib/arrest/helper/ordered_collection.rb', line 80
def first_page?
current_page == 1
end
|
#inspect ⇒ Object
36
37
38
|
# File 'lib/arrest/helper/ordered_collection.rb', line 36
def inspect
collection.inspect
end
|
84
85
86
|
# File 'lib/arrest/helper/ordered_collection.rb', line 84
def last_page?
current_page >= num_pages
end
|
#limit(count) ⇒ Object
40
41
42
43
|
# File 'lib/arrest/helper/ordered_collection.rb', line 40
def limit(count)
page(count)
self
end
|
#limit_value ⇒ Object
60
61
62
|
# File 'lib/arrest/helper/ordered_collection.rb', line 60
def limit_value @page_hash[:pageSize] || 0
end
|
#num_pages ⇒ Object
72
73
74
75
76
77
78
|
# File 'lib/arrest/helper/ordered_collection.rb', line 72
def num_pages
if @page_hash[:pageSize]
(total_count.to_f / (@page_hash[:pageSize])).ceil
else
1
end
end
|
#offset(count) ⇒ Object
45
46
47
48
49
50
51
52
53
|
# File 'lib/arrest/helper/ordered_collection.rb', line 45
def offset(count)
if @page_hash[:pageSize]
new_page = count / @page_hash[:pageSize]
else
new_page = 1
end
page(new_page)
self
end
|
#offset_value ⇒ Object
64
65
66
|
# File 'lib/arrest/helper/ordered_collection.rb', line 64
def offset_value ((@page_hash[:pageSize] || 0) * (current_page - 1)) || 0
end
|
#order_by(field, order = :asc) ⇒ Object
115
116
117
118
119
120
|
# File 'lib/arrest/helper/ordered_collection.rb', line 115
def order_by(field, order = :asc)
@collection = nil if @sort_hash[:sort] != field.to_sym || @sort_hash[:order] != order.to_sym
@sort_hash = {:sort => field.to_sym, :order => order.to_sym}
self
end
|
#page(num) ⇒ Object
107
108
109
110
111
112
113
|
# File 'lib/arrest/helper/ordered_collection.rb', line 107
def page(num)
@collection = nil unless @page_hash[:page] == num.to_i
num ||= 1
@page_hash[:page] = num.to_i
self
end
|
#per(num) ⇒ Object
92
93
94
95
96
97
98
|
# File 'lib/arrest/helper/ordered_collection.rb', line 92
def per(num)
@collection = nil unless @page_hash[:pageSize] == num.to_i
@page_hash[:pageSize] = num.to_i
@page_hash[:page] ||= @default_page
self
end
|
#start_at(id) ⇒ Object
the id of an object in an ordered collection to start paginating from
102
103
104
105
|
# File 'lib/arrest/helper/ordered_collection.rb', line 102
def start_at(id)
@page_hash[:pageStartId] = id
self
end
|
#total_count ⇒ Object
55
56
57
58
|
# File 'lib/arrest/helper/ordered_collection.rb', line 55
def total_count
collection() @total_count
end
|