Class: Sinatra::Pagers::Pager

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra/pagers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePager

Returns a new instance of Pager.



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/sinatra/pagers.rb', line 104

def initialize
  @page=1
  @query=nil
  @extra={}
  @cpp=20
  @max_page=nil
  @n_records=nil
  @order=nil
  @order_col=nil
  @order_dir=nil
end

Instance Attribute Details

#cppObject

Number of object on each page



95
96
97
# File 'lib/sinatra/pagers.rb', line 95

def cpp
  @cpp
end

#extraObject

Extra parameters



103
104
105
# File 'lib/sinatra/pagers.rb', line 103

def extra
  @extra
end

#max_pageObject

Maximum number of pages



97
98
99
# File 'lib/sinatra/pagers.rb', line 97

def max_page
  @max_page
end

#n_recordsObject (readonly)

Number of records



101
102
103
# File 'lib/sinatra/pagers.rb', line 101

def n_records
  @n_records
end

#orderObject

String, with forma “<col>__<direction>”. Direction could be desc (default) or asc



99
100
101
# File 'lib/sinatra/pagers.rb', line 99

def order
  @order
end

#pageObject

Current page



91
92
93
# File 'lib/sinatra/pagers.rb', line 91

def page
  @page
end

#queryObject

dataset string to be processed



93
94
95
# File 'lib/sinatra/pagers.rb', line 93

def query
  @query
end

Instance Method Details

#adapt_ads_cds(ads, cds_pre, no_query: false) ⇒ Object

Adapt the cds_pre dataset, using decisions made by a user

Parameters:



126
127
128
129
130
# File 'lib/sinatra/pagers.rb', line 126

def adapt_ads_cds(ads, cds_pre, no_query:false)
  paqa=PagerAdsQueryAdapter.new(self, ads, cds_pre, no_query: no_query)
  @max_page=(paqa.cds_out.count/self.cpp.to_f).ceil
  adjust_page_order(paqa.cds_out)
end

#adapt_cds(cds_pre) ⇒ Object

Get an Dataset of CanonicalDocuments and returns a new one, with all pager settings applied Including using ‘dataset’ to select using title or author ‘order’



118
119
120
121
122
# File 'lib/sinatra/pagers.rb', line 118

def adapt_cds(cds_pre)
  pcqa=PagerCdQueryAdapter.new(self, cds_pre)
  @max_page=(pcqa.cds_out.count/self.cpp.to_f).ceil
  adjust_page_order(pcqa.cds_out)
end

#adjust_page_order(dataset) ⇒ Object

Retrieves the records to be shown, adjusting by page and order



152
153
154
155
156
157
158
159
160
# File 'lib/sinatra/pagers.rb', line 152

def adjust_page_order(dataset)
  @n_records=dataset.count
  dataset=dataset.offset((@page-1)*@cpp).limit(@cpp)
  if @order
    order_o= (@order_dir=='asc') ? @order_col.to_sym : Sequel.desc(@order_col.to_sym)
    dataset=dataset.order(order_o)
  end
  dataset
end

#current_first_recordObject



162
163
164
# File 'lib/sinatra/pagers.rb', line 162

def current_first_record
  1+(@page-1)*@cpp
end

#current_last_recordObject



165
166
167
168
169
170
171
# File 'lib/sinatra/pagers.rb', line 165

def current_last_record
  if @page==@max_page
    @n_records
  else
    @page*@cpp
  end
end

#DELETE element_to_deleteObject



184
# File 'lib/sinatra/pagers.rb', line 184

hash_to_encode.delete(element_to_delete)

#uri_encode(element_to_delete = nil) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/sinatra/pagers.rb', line 173

def uri_encode(element_to_delete=nil)
  hash_to_encode=self.extra.inject({}) {|ac,v|
    if(v[1].is_a? Array)
      ac["#{v[0]}[]"]=v[1]
    else
      ac[v[0]]=v[1]
    end
    ac
  }
  hash_to_encode[:query]    = self.query
  hash_to_encode[:order]    = self.order
  hash_to_encode.delete(element_to_delete) unless element_to_delete.nil?
  "&"+URI.encode_www_form(hash_to_encode)
end