class Quacks::HashConvertor

Internal: The convertor class to iterate and convert symbol arguments or hashes.

Attributes

conversion_methods[R]

Public Class Methods

new(conversion_methods) click to toggle source

Internal: Initialize a HashConvertor.

conversion_methods - The keywords and conversion methods to be used.

# File lib/quacks/hash_convertor.rb, line 9
def initialize(conversion_methods)
  @conversion_methods = conversion_methods
end

Public Instance Methods

convert!(argument_hash) click to toggle source

Internal: Converts the given symbol arguments with the provided conversion methods.

argument_hash - The hash with arguments to convert.

Examples:

convertor = Quacks::HashConvertor.new(word: :to_s, number: :to_i)
convertor.convert!(word: nil, number: "100")
#=> { word: "", number: 100 }

Returns an Hash with the converted arguments. Raises Quacks::SignatureError if the arguments could not be converted.

# File lib/quacks/hash_convertor.rb, line 26
def convert!(argument_hash)
    conversion_methods
      .each_with_object(argument_hash) do |(name, conversion), args|
    args[name] = Quacks::DefaultConvertor.new(conversion).convert!(args[name])
  end
end