Sha256: 74066d247e342ee4b5ec69f6f0a9e6b925e99e920ef66f21520d1c5e4e8bcf34

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

require 'parameters/types'

module Parameters
  class Param

    # Name of parameter
    attr_reader :name

    # Enforced type of the parameter
    attr_reader :type

    # Description of parameter
    attr_reader :description

    #
    # Creates a new Param object.
    #
    # @param [Symbol, String] name
    #   The name of the parameter.
    #
    # @param [Class] type
    #   The enforced type of the parameter.
    #
    # @param [String, nil] description
    #   The description of the parameter.
    #
    def initialize(name,type=nil,description=nil)
      @name = name.to_sym
      @type = case type
              when Types::Type
                type
              when Class
                if type < Types::Type
                  type
                else
                  Types[type]
                end
              else
                Types[type]
              end

      @description = description
    end

    protected

    #
    # Coerces the value into the param type.
    #
    # @param [Object] value
    #   The value to coerce.
    #
    # @return [Object]
    #   The coerced value.
    #
    def coerce(value)
      if @type === value 
        value
      else
        @type.coerce(value)
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
parameters-0.3.0 lib/parameters/param.rb