OSDN Git Service

change page status
[pettanr/pettanr.git] / lib / locmare / list_group / lib / page_status.rb
1 module Locmare
2   module ListGroupModule
3     module LibModule
4       class PageStatus
5         
6         class Default
7           attr :total, :options
8           
9           def initialize list, total, options
10             @list = list
11             @total = total
12             @options = options
13           end
14           
15           def to_hash
16             {
17               :type => :default, :total => @total, 
18               :page => self.page, :page_size => self.limit
19             }
20           end
21           
22           def pager
23             @pager ||= ::Locmare::ListGroupModule::LibModule::Pager::Default.new(self, @options)
24           end
25           
26           def offset
27             return @offset if @offset
28             @offset = (self.page - 1) * self.limit
29           end
30           
31           def limit
32             return @limit if @limit
33             @limit = PageStatus.page_size(@options) || self.default_page_size
34             if @list.limited?
35               @limit = self.default_page_size if @limit < 0
36               @limit = self.max_page_size if @limit > self.max_page_size
37             else  # unlimited api
38               @limit = -1 if @limit < 0
39             end
40             @limit = @total if @total and (@limit > @total)
41             @limit
42           end
43           
44           def page
45             return @page if @page
46             @page = PageStatus.page(@options).to_i
47             @page = 1 if @page < 1
48             @page
49           end
50           
51           def default_page_size
52             @list.default_page_size
53           end
54           
55           def max_page_size
56             @list.max_page_size
57           end
58           
59           def total_page
60             (@total / self.limit) + 1
61           end
62           
63           def pageable?
64             self.total_page > 1
65           end
66           
67         end
68         
69         class Offset
70           attr :total, :options
71           
72           def initialize list, total, options
73             @list = list
74             @total = total
75             @options = options
76           end
77           
78           def to_hash
79             {
80               :type => :offset, :total => @total, 
81               :offset => self.offset, :count => self.limit
82             }
83           end
84           
85           def paginate
86             c = @total ? @total.to_i : 0
87           end
88           
89           def offset
90             return @offset if @offset
91             @offset = PageStatus.offset(@options).to_i
92             @offset = self.count - 1 if @offset >= self.count
93             @offset = self.count - @offset.abs if @offset < 0
94             @offset = 0 if @offset < 0
95             @offset
96           end
97           
98           def limit
99             return @limit if @limit
100             @limit = self.count || self.default_page_size
101             if @list.limited?
102               @limit = self.default_page_size if @limit < 0
103               @limit = self.max_page_size if @limit > self.max_page_size
104             else  # unlimited api
105               @limit = -1 if @limit < 0
106             end
107             @limit = @total if @total and (@limit > @total)
108             @limit
109           end
110           
111           def count
112             @count ||= PageStatus.count(@options)
113           end
114           
115         end
116         
117         class Unlimited
118           attr :total, :options
119           
120           def initialize list, total, options
121             @list = list
122             @total = total
123             @options = options
124           end
125           
126           def to_hash
127             {
128               :type => :none
129             }
130           end
131           
132           def paginate
133             nil
134           end
135           
136           def offset
137             0
138           end
139           
140           def limit
141             -1
142           end
143           
144         end
145         
146         def self.load list, total, options
147           ps = if self.offset(options) or self.count(options)
148             Offset.new list, total, options
149           else
150             Default.new list, total, options
151           end
152           ps = Unlimited.new(list, total, options) if ps.limit < 0
153           ps
154         end
155         
156         def self.page options
157           n = options['page'] || options[:page]
158           n ? n.to_i : nil
159         end
160         
161         def self.page_size options
162           n = options['page_size'] || options[:page_size]
163           n ? n.to_i : nil
164         end
165         
166         def self.offset options
167           n = options['offset'] || options[:offset]
168           n ? n.to_i : nil
169         end
170         
171         def self.count options
172           n = options['count'] || options[:count]
173           n ? n.to_i : nil
174         end
175         
176       end
177       
178     end
179   end
180 end