Sha256: 0f2204a72fc828b192b3edff3e4eaa4bdc09aeed1eb7fa8e6f57e3c452bbe120

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

# Internal: The convertor class to iterate and convert symbol arguments or
# hashes.
class Quacks::HashConvertor
  attr_reader :conversion_methods

  # Internal: Initialize a HashConvertor.
  #
  # conversion_methods - The keywords and conversion methods to be used.
  def initialize(conversion_methods)
    @conversion_methods = conversion_methods
  end

  # 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.
  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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
quacks-0.1.0 lib/quacks/hash_convertor.rb