class Parameters::Types::Array

Attributes

element_type[R]

The type to coerce all Array elements with

Public Class Methods

coerce(value) click to toggle source

Coerces a value into an Array.

@param [#to_a, ::Object] value

The value to coerce.

@return [::Array]

The coerced Array.
# File lib/parameters/types/array.rb, line 41
def self.coerce(value)
  if value.respond_to?(:to_a)
    value.to_a
  elsif value.respond_to?(:to_ary)
    value.to_ary
  else
    [value]
  end
end
element_type() click to toggle source

The element type of the Array type.

@return [Object]

The default element type.

@since 0.4.0

# File lib/parameters/types/array.rb, line 28
def self.element_type
  Object
end
new(element_type) click to toggle source

Initializes the Array type.

@param [Type, nil] element_type

Optional type for the elements of the Array.
# File lib/parameters/types/array.rb, line 16
def initialize(element_type)
  @element_type = element_type
end

Public Instance Methods

==(other) click to toggle source

Compares the instance type with another type.

@param [Array, Type] other

The other type to compare against.

@return [::Boolean]

Specificies whether the instance type has the same element type
as the other Array instance type.

@since 0.4.0

Calls superclass method
# File lib/parameters/types/array.rb, line 73
def ==(other)
  super(other) && (@element_type == other.element_type)
end
===(value) click to toggle source

Determines if the value is an Array.

@param [::Object] value

The value to inspect.

@return [::Boolean]

# File lib/parameters/types/array.rb, line 85
def ===(value)
  (self.class === value) && value.all? { |element|
    @element_type === element
  }
end
coerce(value) click to toggle source

Coerces a value into an Array, and coerces the elements of the Array.

@param [#to_a, ::Object] value

The value to coerce.

@return [::Array]

The coerced Array.

@see coerce

Calls superclass method
# File lib/parameters/types/array.rb, line 102
def coerce(value)
  array = super(value)
  array.map! { |element| @element_type.coerce(element) }

  return array
end
to_ruby() click to toggle source

The Ruby Type for the Array Type instance.

@return [Array<Class>]

A singleton Array containing the element-type.
# File lib/parameters/types/array.rb, line 57
def to_ruby
  self.class.to_ruby[@element_type.to_ruby]
end