Sha256: 2ad78ae532372301e0fc520ed4b407dfe2bb39f25717e8cc631503a015cfb0d4

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 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, nil] type
    #   The enforced type of the parameter.
    #
    # @param [String, nil] description
    #   The description of the parameter.
    #
    # @api semipublic
    #
    def initialize(name,type=nil,description=nil)
      @name = name.to_sym
      @type = if (type.kind_of?(Types::Type)) ||
                 (type.kind_of?(Class) && (type < Types::Type))
                type
              else
                Types[type]
              end

      @description = description
    end

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

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
parameters-0.4.4 lib/parameters/param.rb
parameters-0.4.3 lib/parameters/param.rb
parameters-0.4.2 lib/parameters/param.rb
parameters-0.4.0 lib/parameters/param.rb