Class: Muster::Strategies::Pagination
- Defined in:
- lib/muster/strategies/pagination.rb
Overview
Query string parsing strategy with logic to handle pagination options
Instance Attribute Summary collapse
-
#default_page_size ⇒ Fixnum
When specified, will override the default page size of 30 when no page_size is parsed.
Attributes inherited from Rack
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Pagination
constructor
Create a new Pagination parsing strategy.
-
#parse(query_string) ⇒ Hash
Processes a query string and returns a hash of its fields/values.
-
#parse_page(parameters) ⇒ Fixnum
protected
Returns the current page for the current query string.
-
#parse_page_size(parameters) ⇒ Fixnum
protected
Returns the page size for the current query string.
Methods inherited from Rack
#fields_to_parse, #parse_query_string
Constructor Details
#initialize(options = {}) ⇒ Pagination
Create a new Pagination parsing strategy
33 34 35 36 37 38 39 40 |
# File 'lib/muster/strategies/pagination.rb', line 33 def initialize( ={} ) super self.default_page_size = self.[:default_page_size].to_i if self.default_page_size < 1 self.default_page_size = 30 end end |
Instance Attribute Details
#default_page_size ⇒ Fixnum
Returns when specified, will override the default page size of 30 when no page_size is parsed.
17 18 19 |
# File 'lib/muster/strategies/pagination.rb', line 17 def default_page_size @default_page_size end |
Instance Method Details
#parse(query_string) ⇒ Hash
Processes a query string and returns a hash of its fields/values
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/muster/strategies/pagination.rb', line 51 def parse( query_string ) parameters = self.parse_query_string(query_string) page = self.parse_page(parameters) page_size = self.parse_page_size(parameters) offset = (page - 1) * page_size offset = nil if offset < 1 parameters = parameters.merge(:pagination => {:page => page, :per_page => page_size}, :limit => page_size, :offset => offset) if self.fields.present? parameters = parameters.slice(*self.fields).with_indifferent_access end parameters end |
#parse_page(parameters) ⇒ Fixnum (protected)
Returns the current page for the current query string.
If page is not specified, or is not a positive number, 1 will be returned instead.
84 85 86 87 88 |
# File 'lib/muster/strategies/pagination.rb', line 84 def parse_page( parameters ) page = parameters.delete(:page).to_i page = 1 unless page > 0 page end |
#parse_page_size(parameters) ⇒ Fixnum (protected)
Returns the page size for the current query string.
If per_page or page_size is not specified, or is not a positive number, :default_page_size will be returned instead.
103 104 105 106 107 |
# File 'lib/muster/strategies/pagination.rb', line 103 def parse_page_size( parameters ) page_size = (parameters.delete(:page_size) || parameters.delete(:per_page)).to_i page_size = self.default_page_size unless page_size > 0 page_size end |