Class: Muster::Strategies::FilterExpression

Inherits:
Hash
  • Object
show all
Defined in:
lib/muster/strategies/filter_expression.rb

Overview

Query string parsing strategy with additional value handling options for separating filtering expressions

Examples:


strategy = Muster::Strategies::FilterExpression.new
results  = strategy.parse('where=id:1&name:Bob')  #=>  { 'where' => {'id' => '1', 'name' => 'Bob'} }

Instance Attribute Summary collapse

Attributes inherited from Hash

#unique_values, #value_separator

Attributes inherited from Rack

#fields, #options

Instance Method Summary collapse

Methods inherited from Rack

#fields_to_parse, #parse_query_string

Constructor Details

#initialize(options = {}) ⇒ FilterExpression

Create a new Hash parsing strategy

Examples:


strategy = Muster::Strategies::FilterExpression.new
strategy = Muster::Strategies::FilterExpression.new(:unique_values => false)

Parameters:

  • options (Hash) (defaults to: {})

    the options available for this method

Options Hash (options):

  • :fields (optional, Array<Symbol>)

    when specified, only parse the specified fields You may also use :field if you only intend to pass one field

  • :expression_separator (optional, String, RegEx) — default: /, \s*/

    when specified, splits the query string value into multiple expressions You may pass the separator as a string or a regular expression

  • :field_separator (optional, String, RegEx) — default: :

    when specified, splits the expression value into multiple field/values You may pass the separator as a string or a regular expression

  • :value_separator (optional, String, RegEx) — default: |

    when specified, splits the field value into multiple values You may pass the separator as a string or a regular expression

  • :unique_values (optional, Boolean) — default: true

    when true, ensures field values do not contain duplicates



39
40
41
42
43
44
45
# File 'lib/muster/strategies/filter_expression.rb', line 39

def initialize(options = {})
  super

  @expression_separator = self.options.fetch(:expression_separator, /,\s*/)
  @field_separator = self.options.fetch(:field_separator, ':')
  @value_separator = self.options.fetch(:value_separator, '|')
end

Instance Attribute Details

#expression_separatorString, RegEx (readonly)

Returns when specified, each field value will be split into multiple expressions using the specified separator.

Returns:

  • (String, RegEx)

    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_separatorString, RegEx (readonly)

Returns when specified, each expression will be split into multiple field/values using the specified separator.

Returns:

  • (String, RegEx)

    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.

Examples:


value = ensure_unique_values('1')    #=> '1'
value = ensure_unique_values(['1', '2'])  #=> ['1', '2']
value = ensure_unique_values(['1', '1'])  #=> ['1']

Parameters:

  • values (String, Array)

    the expressions filter values to ensure are unique

Returns:

  • (String, Array)

    String if a single value exists, Array otherwise



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

Examples:


results = strategy.parse('where=id:1&name:Bob')  #=>  { 'where' => {'id' => '1', 'name' => 'Bob'} }

Parameters:

  • query_string (String)

    the query string to parse

Returns:



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

Examples:


value = separate_values('where=id:1')       #=> {'where' => 'id:1'}
value = separate_values('where=id:1,id:2')  #=> {'where' => ['id:1', 'id:2']}

Parameters:

  • expression (String, Array)

    the original query string field value to separate

Returns:

  • (String, Array)

    String if a single value exists, Array otherwise



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

Examples:


value = separate_fields('id:1')    #=> {'id' => '1'}
value = separate_values('id:1|2')  #=> {'id' => '1|2'}

Parameters:

  • values_string (String, Array)

    the expressions field value to separate

Returns:



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

Examples:


value = separate_values('1')    #=> '1'
value = separate_values('1|2')  #=> ['1', '2']

Parameters:

  • values_string (String, Array)

    the expressions filter value to separate

Returns:

  • (String, Array)

    String if a single value exists, Array otherwise



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