class Templater::ArgumentDescription

Public Instance Methods

extract(argument) click to toggle source
# File lib/templater/description.rb, line 47
def extract(argument)
  case options[:as]
  when :hash
    if argument.is_a?(String)
      return argument.split(',').inject({}) do |h, pair|
        key, value = pair.strip.split(':')
        raise Templater::MalformattedArgumentError, "Expected '#{argument.inspect}' to be a key/value pair" unless key and value
        h[key] = value
        h
      end
    end
  when :array
    return argument.split(',') if argument.is_a?(String)
  end
  return argument
end
valid?(argument) click to toggle source

Checks if the given argument is valid according to this description

Parameters

argument<Object>

Checks if the given argument is valid.

Returns

Boolean

Validity of the argument

# File lib/templater/description.rb, line 29
def valid?(argument)
  if argument.nil? and options[:required]
    raise Templater::TooFewArgumentsError
  elsif not argument.nil?
    if options[:as] == :hash and not argument.is_a?(Hash)
      raise Templater::MalformattedArgumentError, "Expected the argument to be a Hash, but was '#{argument.inspect}'"
    elsif options[:as] == :array and not argument.is_a?(Array)
      raise Templater::MalformattedArgumentError, "Expected the argument to be an Array, but was '#{argument.inspect}'"
    end
       
    invalid = catch :invalid do
      block.call(argument) if block
      throw :invalid, :not_invalid
    end
    raise Templater::ArgumentError, invalid unless invalid == :not_invalid
  end
end