Class: Parameters::Types::Array

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

Direct Known Subclasses

Set

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Object

===, to_ruby

Methods inherited from Type

===, to_ruby

Constructor Details

#initialize(element_type) ⇒ Array

Initializes the Array type.

Parameters:

  • element_type (Type, nil)

    Optional type for the elements of the Array.



16
17
18
# File 'lib/parameters/types/array.rb', line 16

def initialize(element_type)
  @element_type = element_type
end

Instance Attribute Details

#element_typeObject (readonly)

The type to coerce all Array elements with



8
9
10
# File 'lib/parameters/types/array.rb', line 8

def element_type
  @element_type
end

Class Method Details

.coerce(value) ⇒ ::Array

Coerces a value into an Array.

Parameters:

  • value (#to_a, ::Object)

    The value to coerce.

Returns:

  • (::Array)

    The coerced Array.



29
30
31
32
33
34
35
36
37
# File 'lib/parameters/types/array.rb', line 29

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

Instance Method Details

#===(value) ⇒ ::Boolean

Determines if the value is an Array.

Parameters:

  • value (::Object)

    The value to inspect.

Returns:

  • (::Boolean)


57
58
59
60
61
# File 'lib/parameters/types/array.rb', line 57

def ===(value)
  (self.class === value) && value.all? { |element|
    @element_type === element
  }
end

#coerce(value) ⇒ ::Array

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

Parameters:

  • value (#to_a, ::Object)

    The value to coerce.

Returns:

  • (::Array)

    The coerced Array.

See Also:



74
75
76
77
78
79
# File 'lib/parameters/types/array.rb', line 74

def coerce(value)
  array = super(value)
  array.map! { |element| @element_type.coerce(element) }

  return array
end

#to_rubyArray<Class>

The Ruby Type for the Array Type instance.

Returns:

  • (Array<Class>)

    A singleton Array containing the element-type.



45
46
47
# File 'lib/parameters/types/array.rb', line 45

def to_ruby
  self.class.to_ruby[@element_type.to_ruby]
end