Class: Muster::Strategies::JoinsExpression

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

Overview

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

Examples:


strategy = Muster::Strategies::JoinsExpression.new
results  = strategy.parse('joins=author.name,activity')  #=>  { 'joins' => [{'author' => 'name'}, 'activity'] }

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 Hash

#separate_values

Methods inherited from Rack

#fields_to_parse, #parse_query_string

Constructor Details

#initialize(options = {}) ⇒ JoinsExpression

Create a new Hash parsing strategy

Examples:


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

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

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

    when true, ensures field values do not contain duplicates



35
36
37
38
39
40
41
# File 'lib/muster/strategies/joins_expression.rb', line 35

def initialize(options = {})
  super

  @expression_separator = options.fetch(:expression_separator, /,\s*/)
  @field_separator      = options.fetch(:field_separator, '.')
  @unique_values        = options.fetch(:unique_values, true)
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



14
15
16
# File 'lib/muster/strategies/joins_expression.rb', line 14

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



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

def field_separator
  @field_separator
end

Instance Method Details

#make_nested_hash(value) ⇒ String, Array (protected)

Converts the array that represents the value to a nested hash

Examples:


value = self.make_nested_hash('activity,author.country.name')  #=> ['activity', {'author' => {'country' => 'name'}}]

Parameters:

  • value (Array)

    the value to convert

Returns:

  • (String, Array)

    An array of nested a Hash / Hashes



72
73
74
75
76
77
78
# File 'lib/muster/strategies/joins_expression.rb', line 72

def make_nested_hash(value)
  expressions = value.split(expression_separator)
  expressions.map do |expression|
    fields = expression.split(field_separator)
    fields[0..-2].reverse.reduce(fields.last) { |a, e| { e => a } }
  end
end

#parse(query_string) ⇒ Muster::Results

Processes a query string and returns an array of hashes that represent an ActiveRecord joins expression

Examples:


results = strategy.parse('joins=author.name,activity')  #=>  { 'joins' => [{'author' => 'name'}, 'activity'] }

Parameters:

  • query_string (String)

    the query string to parse

Returns:



52
53
54
55
56
57
58
59
# File 'lib/muster/strategies/joins_expression.rb', line 52

def parse(query_string)
  parameters = Muster::Results.new(fields_to_parse(query_string))

  parameters.each do |key, value|
    value = value.uniq.first if unique_values == true && value.instance_of?(Array)
    parameters[key] = make_nested_hash(value)
  end
end