Class: Templater::ArgumentDescription

### Home Classes Methods

### In Files

- lib/templater/description.rb

### Parent

Templater::Description

### Methods

- #extract
- #valid?

### Files

- History.txt
- Manifest.txt

### Class/Module Index

Quicksearch

- Templater
- Templater::ActionDescription
- Templater::Actions
- Templater::Actions::Action
- Templater::Actions::Directory
- Templater::Actions::EmptyDirectory
- Templater::Actions::File
- Templater::Actions::Template
- Templater::ArgumentDescription
- Templater::CLI
- Templater::CLI::Generator
- Templater::CLI::Manifold
- Templater::Description
- Templater::Discovery
- Templater::Generator
- Templater::InvocationDescription
- Templater::Manifold
- Templater::Spec
- Templater::Spec::Helpers
- Templater::Spec::Helpers::CreateMatcher
- Templater::Spec::Helpers::InvokeMatcher
- Kernel
- String

No matching classes.

# 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.