Class: Muster::Strategies::JoinsExpression
- Defined in:
- lib/muster/strategies/joins_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 = {}) ⇒ JoinsExpression
constructor
Create a new Hash parsing strategy.
-
#make_nested_hash(value) ⇒ String, Array
protected
Converts the array that represents the value to a nested hash.
-
#parse(query_string) ⇒ Muster::Results
Processes a query string and returns an array of hashes that represent an ActiveRecord joins expression.
Methods inherited from Hash
Methods inherited from Rack
#fields_to_parse, #parse_query_string
Constructor Details
#initialize(options = {}) ⇒ JoinsExpression
Create a new Hash parsing strategy
35 36 37 38 39 40 41 |
# File 'lib/muster/strategies/joins_expression.rb', line 35 def initialize( = {}) super @expression_separator = .fetch(:expression_separator, /,\s*/) @field_separator = .fetch(:field_separator, '.') @unique_values = .fetch(:unique_values, true) 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.
14 15 16 |
# File 'lib/muster/strategies/joins_expression.rb', line 14 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.
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
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
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 |