Class: Uinit::Type::ArrayOf

Inherits:
Generic show all
Defined in:
lib/uinit/type/array_of.rb

Instance Attribute Summary

Attributes inherited from Generic

#type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Generic

#initialize, #inspect

Methods inherited from Base

#==, [], #inspect, #is!, #to_s, #trace!, #type_error!

Methods included from Operators

#&, #|

Constructor Details

This class inherits a constructor from Uinit::Type::Generic

Class Method Details

.from(array) ⇒ Object



10
11
12
# File 'lib/uinit/type/array_of.rb', line 10

def self.from(array)
  new(array.first) if from?(array)
end

.from?(array) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/uinit/type/array_of.rb', line 6

def self.from?(array)
  array.is_a?(Array)
end

Instance Method Details

#check!(value, depth) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/uinit/type/array_of.rb', line 20

def check!(value, depth)
  type_error!("#{value.inspect} must be an Array", depth) unless value.is_a?(Array)

  value.each_with_index do |val, index|
    type.check!(val, depth + 1)
  rescue Error => e
    trace!(e, "[#{index}]")
  end

  value
end

#is?(value) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
# File 'lib/uinit/type/array_of.rb', line 14

def is?(value)
  return false unless value.is_a?(Array)

  value.all? { |val| type.is?(val) }
end