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) ⇒ Hash
Processes a query string and returns a hash of its fields/values.
-
#separate_values(value) ⇒ 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
37 38 39 40 41 42 |
# File 'lib/muster/strategies/hash.rb', line 37 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.
22 23 24 |
# File 'lib/muster/strategies/hash.rb', line 22 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.
18 19 20 |
# File 'lib/muster/strategies/hash.rb', line 18 def value_separator @value_separator end |
Instance Method Details
#parse(query_string) ⇒ Hash
Processes a query string and returns a hash of its fields/values
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/muster/strategies/hash.rb', line 53 def parse( query_string ) parameters = super parameters.each do |key, value| if self.value_separator.present? parameters[key] = self.separate_values(value) end if self.unique_values == true && value.instance_of?(Array) parameters[key].uniq! end end return parameters end |
#separate_values(value) ⇒ String, Array (protected)
Separates values into an Array of values using :values_separator
81 82 83 84 85 86 87 88 89 |
# File 'lib/muster/strategies/hash.rb', line 81 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 |