class Primalize::Single::Array

Public Class Methods

new(types, &coercion) click to toggle source
# File lib/primalize/single.rb, line 227
def initialize types, &coercion
  @types = types
  @coercion = coercion
end

Public Instance Methods

===(value) click to toggle source
# File lib/primalize/single.rb, line 232
def === value
  return false unless ::Array === value
  value.all? do |item|
    @types.any? { |type| type === item }
  end
end
coerce(array) click to toggle source
# File lib/primalize/single.rb, line 239
def coerce array
  if @coercion
    return @coercion.call(array)
  end

  if array.respond_to? :map
    array.map do |item|
      type = @types.find { |type| type === item }
      if type.respond_to? :coerce
        type.coerce(item)
      else
        item
      end
    end
  else
    array
  end
end
inspect() click to toggle source
# File lib/primalize/single.rb, line 258
def inspect
  "array(#{@types.map(&:inspect).join(', ')})"
end