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) ⇒ Muster::Results
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
32 33 34 35 36 37 38 39 40 |
# File 'lib/muster/strategies/pagination.rb', line 32 def initialize( = {}) super self.default_page_size = [:default_page_size].to_i return unless default_page_size < 3 self.default_page_size = 30 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.
16 17 18 |
# File 'lib/muster/strategies/pagination.rb', line 16 def default_page_size @default_page_size end |
Instance Method Details
#parse(query_string) ⇒ Muster::Results
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 |
# File 'lib/muster/strategies/pagination.rb', line 51 def parse(query_string) parameters = parse_query_string(query_string) page = parse_page(parameters) page_size = 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 fields.present? parameters = parameters.slice(*fields) end Muster::Results.new(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.
83 84 85 86 87 |
# File 'lib/muster/strategies/pagination.rb', line 83 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.
102 103 104 105 106 |
# File 'lib/muster/strategies/pagination.rb', line 102 def parse_page_size(parameters) page_size = (parameters.delete(:page_size) || parameters.delete(:per_page)).to_i page_size = default_page_size unless page_size > 0 page_size end |