Class: Muster::Strategies::Hash
- Defined in:
- lib/muster/strategies/hash.rb
Overview
Query string parsing strategy with additional value handling options for separating values and uniqueness
Direct Known Subclasses
Instance Attribute Summary collapse
-
#unique_values ⇒ Boolean
readonly
When specified, ensures a fields values do not contain duplicates.
-
#value_separator ⇒ String, RegEx
readonly
When specified, each field value will be split into multiple values using the specified separator.
Attributes inherited from Rack
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Hash
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_values(values_string) ⇒ String, Array
protected
Separates values into an Array of values using :values_separator.
Methods inherited from Rack
#fields_to_parse, #parse_query_string
Constructor Details
#initialize(options = {}) ⇒ Hash
Create a new Hash parsing strategy
35 36 37 38 39 40 |
# File 'lib/muster/strategies/hash.rb', line 35 def initialize( = {}) super @unique_values = self..fetch(:unique_values, true) @value_separator = self..fetch(:value_separator, /,\s*/) end |
Instance Attribute Details
#unique_values ⇒ Boolean (readonly)
Returns when specified, ensures a fields values do not contain duplicates.
20 21 22 |
# File 'lib/muster/strategies/hash.rb', line 20 def unique_values @unique_values end |
#value_separator ⇒ String, RegEx (readonly)
Returns when specified, each field value will be split into multiple values using the specified separator.
16 17 18 |
# File 'lib/muster/strategies/hash.rb', line 16 def value_separator @value_separator end |
Instance Method Details
#parse(query_string) ⇒ Muster::Results
Processes a query string and returns a hash of its fields/values
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/muster/strategies/hash.rb', line 51 def parse(query_string) parameters = super parameters.each do |key, value| if value_separator.present? parameters[key] = separate_values(value) end if unique_values == true && value.instance_of?(Array) parameters[key].uniq! end end parameters end |
#separate_values(values_string) ⇒ String, Array (protected)
Separates values into an Array of values using :values_separator
79 80 81 82 83 84 85 86 87 |
# File 'lib/muster/strategies/hash.rb', line 79 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 |