Sha256: 5432eb49e6b85f7f419fcd9444dae9891c51c81f0b24b6e969b599a71b1156a6

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

module Templater
  
  class Description
    attr_accessor :name, :options, :block
    
    def initialize(name, options={}, &block)
      @name = name
      @options = options
      @block = block
    end    
  end
  
  class ActionDescription < Description
    
    def compile(generator)
      @block.call(generator)
    end
    
  end
  
  class ArgumentDescription < Description
    
    # 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
    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
    
  end
  
  class InvocationDescription < Description
    
    def get(generator)
      klass = generator.class.manifold.generator(name)
      if klass and block
        generator.instance_exec(klass, &block)
      elsif klass
        klass.new(generator.destination_root, generator.options, *generator.arguments)
      end
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
templater-0.2 lib/templater/description.rb