Class: Quacks::HashConvertor

Inherits:
Object
  • Object
show all
Defined in:
lib/quacks/hash_convertor.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conversion_methods) ⇒ HashConvertor

Internal: Initialize a HashConvertor.

conversion_methods - The keywords and conversion methods to be used.



9
10
11
# File 'lib/quacks/hash_convertor.rb', line 9

def initialize(conversion_methods)
  @conversion_methods = conversion_methods
end

Instance Attribute Details

#conversion_methodsObject (readonly)

Returns the value of attribute conversion_methods.



4
5
6
# File 'lib/quacks/hash_convertor.rb', line 4

def conversion_methods
  @conversion_methods
end

Instance Method Details

#convert!(argument_hash) ⇒ Object

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.



26
27
28
29
30
31
# 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