module Parameters::Types

@since 0.3.0

@api private

Public Class Methods

[](type) click to toggle source

Maps a Hash, Set, Array, Proc or Class to a Type.

@param [Hash, Set, Array, Proc, Class] type

The Ruby Class or Hash, Set, Array, Proc to map to a Type.

@return [Types::Type]

The mapped type.

@raise [TypeError]

The Ruby Type could not be mapped to a Parameter Type.
# File lib/parameters/types/types.rb, line 63
def self.[](type)
  case type
  when ::Hash
    key_type, value_type = type.entries[0]

    Hash.new(self[key_type],self[value_type])
  when ::Set
    Set.new(self[type.entries[0]])
  when ::Array
    Array.new(self[type[0]])
  when ::Proc
    Proc.new(type)
  when true
    Boolean
  when nil
    Object
  when ::Class
    if type_defined?(type.name)
      type_get(type.name)
    else
      Class.new(type)
    end
  when ::Module
    if type_defined?(type.name)
      type_get(type.name)
    else
      raise(TypeError,"unknown parameter type: #{type.inspect}")
    end
  else
    raise(TypeError,"invalid parameter type: #{type.inspect}")
  end
end
type_defined?(name) click to toggle source

Determines if a Type is defined.

@param [Symbol, String] name

The name of the type.

@return [Boolean]

Specifies whether the type was defined within {Types}.
# File lib/parameters/types/types.rb, line 25
def self.type_defined?(name)
  const_defined?(name) && (const_get(name) < Type)
end
type_get(name) click to toggle source

Looks up a Type.

@param [Symbol, String] name

The name of the type.

@return [Type]

The type within {Types}.

@raise [NameError]

The type could not be found.
# File lib/parameters/types/types.rb, line 41
def self.type_get(name)
  type = const_get(name)

  unless type < Type
    raise(NameError,"unknown Parameter Type: #{name}")
  end

  return type
end