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
-
#ensure_unique_values(values) ⇒ String, Array
protected
Ensures that if an Array is given, the values are unique if unique_values is set.
-
#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(expression) ⇒ String, Array
protected
Separates values into an Array of expressions using :expression_separator.
-
#separate_fields(values_string) ⇒ Hash
protected
Separates expression field values into an Hash of expression filters using :field_separator.
-
#separate_values(values_string) ⇒ 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
39 40 41 42 43 44 45 |
# File 'lib/muster/strategies/filter_expression.rb', line 39 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.
16 17 18 |
# File 'lib/muster/strategies/filter_expression.rb', line 16 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.
20 21 22 |
# File 'lib/muster/strategies/filter_expression.rb', line 20 def field_separator @field_separator end |
Instance Method Details
#ensure_unique_values(values) ⇒ String, Array (protected)
Ensures that if an Array is given, the values are unique if unique_values is set.
147 148 149 150 151 152 153 |
# File 'lib/muster/strategies/filter_expression.rb', line 147 def ensure_unique_values(values) if unique_values && values.instance_of?(Array) values.uniq else values end end |
#parse(query_string) ⇒ Muster::Results
Processes a query string and returns a hash of its fields/values
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/muster/strategies/filter_expression.rb', line 56 def parse(query_string) parameters = Muster::Results.new(fields_to_parse(query_string)) parameters.each do |key, value| parameters[key] = separate_expressions(value) parameters[key] = separate_fields(parameters[key]) end parameters end |
#separate_expressions(expression) ⇒ String, Array (protected)
Separates values into an Array of expressions using :expression_separator
79 80 81 82 83 84 85 86 87 |
# File 'lib/muster/strategies/filter_expression.rb', line 79 def separate_expressions(expression) values = Array.wrap(expression) values = values.map do |value| value.split(expression_separator) end.flatten (values.size > 1) ? values : values.first end |
#separate_fields(values_string) ⇒ Hash (protected)
Separates expression field values into an Hash of expression filters using :field_separator
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/muster/strategies/filter_expression.rb', line 99 def separate_fields(values_string) values = Array.wrap(values_string) filters = {} values.each do |value| name, value = value.split(field_separator, 2) value = separate_values(value) filters[name] = filters.key?(name) ? [filters[name], value].flatten : value filters[name] = ensure_unique_values(filters[name]) end filters end |
#separate_values(values_string) ⇒ String, Array (protected)
Separates expression filter values into an Array of expression filter values using :value_separator
126 127 128 129 130 131 132 133 134 |
# File 'lib/muster/strategies/filter_expression.rb', line 126 def separate_values(values_string) values = Array.wrap(values_string) values = values.map do |value| value.split(value_separator) end.flatten (values.size > 1) ? values : values_string end |