class Quacks::DefaultConvertor

Internal: The default convertor class to be used in order to convert a single argument.

Attributes

conversion_method[R]

Public Class Methods

new(conversion_method) click to toggle source

Internal: Initialize a DefaultConvertor.

conversion_method - The method to call that does the conversion

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

Public Instance Methods

convert!(argument) click to toggle source

Internal: Converts the given argument with the conversion method given in the initializer.

argument - Any object to convert.

Returns the converted argument. Raises Quacks::SignatureError if the argument could not be converted.

# File lib/quacks/default_convertor.rb, line 20
def convert!(argument)
  return argument.public_send(conversion_method) if convertable?(argument)
  raise(Quacks::SignatureError,
        "`#{argument}` must respond to `#{conversion_method}`")
end

Private Instance Methods

convertable?(argument) click to toggle source

Internal: Tells whether the provided argument can be converted or not.

Returns a Bool.

# File lib/quacks/default_convertor.rb, line 31
def convertable?(argument)
  argument.respond_to? conversion_method
end