Sha256: 2b07ab6d4c4bd2872f0e666077838e16acf7e4fcc54a87f8501386f747356210

Contents?: true

Size: 1016 Bytes

Versions: 1

Compression:

Stored size: 1016 Bytes

Contents

# Internal: The default convertor class to be used in order to convert a single
# argument.
class Quacks::DefaultConvertor
  attr_reader :conversion_method

  # Internal: Initialize a DefaultConvertor.
  #
  # conversion_method - The method to call that does the conversion
  def initialize(conversion_method)
    @conversion_method = conversion_method
  end

  # 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.
  def convert!(argument)
    return argument.public_send(conversion_method) if convertable?(argument)
    raise(Quacks::SignatureError,
          "`#{argument}` must respond to `#{conversion_method}`")
  end

  private

  # Internal: Tells whether the provided argument can be converted or not.
  #
  # Returns a Bool.
  def convertable?(argument)
    argument.respond_to? conversion_method
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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