Class: Muster::Strategies::FilterExpression
- Defined in:
- lib/muster/strategies/filter_expression.rb
Overview
Query string parsing strategy with additional value handling options for separating filtering expressions
Instance Attribute Summary collapse
-
#expression_separator ⇒ String, RegEx
readonly
When specified, each field value will be split into multiple expressions using the specified separator.
-
#field_separator ⇒ String, RegEx
readonly
When specified, each expression will be split into multiple field/values using the specified separator.
Attributes inherited from Hash
#unique_values, #value_separator
Attributes inherited from Rack
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ FilterExpression
constructor
Create a new Hash parsing strategy.
-
#parse(query_string) ⇒ Muster::Results
Processes a query string and returns a hash of its fields/values.
-
#separate_expressions(value) ⇒ String, Array
protected
Separates values into an Array of expressions using :expression_separator.
-
#separate_fields(value) ⇒ Hash
protected
Separates expression field values into an Hash of expression filters using :field_separator.
-
#separate_values(value) ⇒ String, Array
protected
Separates expression filter values into an Array of expression filter values using :value_separator.
Methods inherited from Rack
#fields_to_parse, #parse_query_string
Constructor Details
#initialize(options = {}) ⇒ FilterExpression
Create a new Hash parsing strategy
41 42 43 44 45 46 47 |
# File 'lib/muster/strategies/filter_expression.rb', line 41 def initialize( ={} ) super @expression_separator = self..fetch(:expression_separator, /,\s*/) @field_separator = self..fetch(:field_separator, ':') @value_separator = self..fetch(:value_separator, '|') end |
Instance Attribute Details
#expression_separator ⇒ String, RegEx (readonly)
Returns when specified, each field value will be split into multiple expressions using the specified separator.
18 19 20 |
# File 'lib/muster/strategies/filter_expression.rb', line 18 def expression_separator @expression_separator end |
#field_separator ⇒ String, RegEx (readonly)
Returns when specified, each expression will be split into multiple field/values using the specified separator.
22 23 24 |
# File 'lib/muster/strategies/filter_expression.rb', line 22 def field_separator @field_separator end |
Instance Method Details
#parse(query_string) ⇒ Muster::Results
Processes a query string and returns a hash of its fields/values
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/muster/strategies/filter_expression.rb', line 58 def parse( query_string ) parameters = Muster::Results.new( self.fields_to_parse(query_string) ) parameters.each do |key, value| parameters[key] = self.separate_expressions(value) parameters[key] = self.separate_fields(parameters[key]) end return parameters end |
#separate_expressions(value) ⇒ String, Array (protected)
Separates values into an Array of expressions using :expression_separator
81 82 83 84 85 86 87 88 89 |
# File 'lib/muster/strategies/filter_expression.rb', line 81 def separate_expressions( value ) values = Array.wrap(value) values = values.map do |value| value.split(self.expression_separator) end.flatten return (values.size > 1) ? values : values.first end |
#separate_fields(value) ⇒ Hash (protected)
Separates expression field values into an Hash of expression filters using :field_separator
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/muster/strategies/filter_expression.rb', line 101 def separate_fields( value ) values = Array.wrap(value) filters = {} values.each do |value| name, value = value.split(self.field_separator, 2) if self.value_separator.present? value = self.separate_values(value) end filters[name] = filters.has_key?(name) ? [filters[name], value].flatten : value if self.unique_values == true && filters[name].instance_of?(Array) filters[name].uniq! end end return filters end |
#separate_values(value) ⇒ String, Array (protected)
Separates expression filter values into an Array of expression filter values using :value_separator
133 134 135 136 137 138 139 140 141 |
# File 'lib/muster/strategies/filter_expression.rb', line 133 def separate_values( value ) values = Array.wrap(value) values = values.map do |value| value.split(self.value_separator) end.flatten return (values.size > 1) ? values : value end |