class Necromancer::HashConverters::StringToHashConverter

An object that converts a String to a Hash

Constants

DEFAULT_CONVERSION

Public Instance Methods

call(value, strict: config.strict, value_converter: DEFAULT_CONVERSION) click to toggle source

Convert string value to hash

@example

converter.call("a:1 b:2 c:3")
# => {a: "1", b: "2", c: "3"}

@example

converter.call("a=1 & b=3 & c=3")
# => {a: "1", b: "2", c: "3"}

@api public

# File lib/necromancer/converters/hash.rb, line 24
def call(value, strict: config.strict, value_converter: DEFAULT_CONVERSION)
  values = value.split(/\s*[& ]\s*/)
  values.each_with_object({}) do |pair, pairs|
    key, value = pair.split(/[=:]/, 2)
    value_converted = value_converter.(value, strict: strict)
    if current = pairs[key.to_sym]
      pairs[key.to_sym] = Array(current) << value_converted
    else
      pairs[key.to_sym] = value_converted
    end
    pairs
  end
end