Class: Quacks::DefaultConvertor

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conversion_method) ⇒ DefaultConvertor

Internal: Initialize a DefaultConvertor.

conversion_method - The method to call that does the conversion



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

def initialize(conversion_method)
  @conversion_method = conversion_method
end

Instance Attribute Details

#conversion_methodObject (readonly)

Returns the value of attribute conversion_method.



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

def conversion_method
  @conversion_method
end

Instance Method Details

#convert!(argument) ⇒ Object

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.



20
21
22
23
24
# 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