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



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

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



18
19
20
# File 'lib/muster/strategies/filter_expression.rb', line 18

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



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

Examples:


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

Parameters:

  • query_string (String)

    the query string to parse

Returns:



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

Examples:


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

Parameters:

  • value (String, Array)

    the original query string field value to separate

Returns:

  • (String, Array)

    String if a single value exists, Array otherwise



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

Examples:


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

Parameters:

  • value (String, Array)

    the expressions field value to separate

Returns:



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

Examples:


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

Parameters:

  • value (String, Array)

    the expressions filter value to separate

Returns:

  • (String, Array)

    String if a single value exists, Array otherwise



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