Class: Muster::Strategies::SortExpression
- Defined in:
- lib/muster/strategies/sort_expression.rb
Overview
Query string parsing strategy with additional value handling for sort orders
Instance Attribute Summary
Attributes inherited from Hash
#unique_values, #value_separator
Attributes inherited from Rack
Instance Method Summary collapse
-
#parse(query_string) ⇒ Hash
Processes a query string and returns a hash of its fields/values.
-
#parse_direction(direction) ⇒ String
protected
Parse and normalize the sot expression direction.
-
#parse_sort_expression(value) ⇒ String, Arrary
protected
Separates the values into their field and direction.
Methods inherited from Hash
Methods inherited from Rack
#fields_to_parse, #initialize, #parse_query_string
Constructor Details
This class inherits a constructor from Muster::Strategies::Hash
Instance Method Details
#parse(query_string) ⇒ Hash
Processes a query string and returns a hash of its fields/values
26 27 28 29 30 31 32 |
# File 'lib/muster/strategies/sort_expression.rb', line 26 def parse( query_string ) parameters = super parameters.each do |key, value| parameters[key] = self.parse_sort_expression(value) end end |
#parse_direction(direction) ⇒ String (protected)
Parse and normalize the sot expression direction
69 70 71 |
# File 'lib/muster/strategies/sort_expression.rb', line 69 def parse_direction( direction ) direction.to_s.match(/^desc/i) ? 'desc' : 'asc' end |
#parse_sort_expression(value) ⇒ String, Arrary (protected)
Separates the values into their field and direction
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/muster/strategies/sort_expression.rb', line 46 def parse_sort_expression( value ) values = Array.wrap(value) values = values.map do |value| name, direction = value.split(':', 2) direction = self.parse_direction(direction) "#{name} #{direction}" end.flatten return (values.size > 1) ? values : values.first end |