Class: Parameters::Param

Inherits:
Object show all
Defined in:
lib/parameters/param.rb

Direct Known Subclasses

ClassParam, InstanceParam

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type = nil, description = nil) ⇒ Param

Creates a new Param object.

Parameters:

  • name (Symbol, String)

    The name of the parameter.

  • type (Class, nil) (defaults to: nil)

    The enforced type of the parameter.

  • description (String, nil) (defaults to: nil)

    The description of the parameter.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/parameters/param.rb', line 29

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

Instance Attribute Details

#descriptionObject (readonly)

Description of parameter



13
14
15
# File 'lib/parameters/param.rb', line 13

def description
  @description
end

#nameObject (readonly)

Name of parameter



7
8
9
# File 'lib/parameters/param.rb', line 7

def name
  @name
end

#typeObject (readonly)

Enforced type of the parameter



10
11
12
# File 'lib/parameters/param.rb', line 10

def type
  @type
end

Instance Method Details

#coerce(value) ⇒ Object

Coerces the value into the param type.

Parameters:

  • value (Object)

    The value to coerce.

Returns:

  • (Object)

    The coerced value.



52
53
54
55
56
57
58
# File 'lib/parameters/param.rb', line 52

def coerce(value)
  if (value.nil? || (@type === value))
    value
  else
    @type.coerce(value)
  end
end