Class: Primalize::Single::Any

Inherits:
Object
  • Object
show all
Defined in:
lib/primalize/single.rb

Instance Method Summary collapse

Constructor Details

#initialize(types, &coercion) ⇒ Any

Returns a new instance of Any.



402
403
404
405
# File 'lib/primalize/single.rb', line 402

def initialize types, &coercion
  @types = types
  @coercion = coercion
end

Instance Method Details

#===(value) ⇒ Object



407
408
409
# File 'lib/primalize/single.rb', line 407

def === value
  @types.empty? || @types.any? { |type| type === value }
end

#coerce(value) ⇒ Object



411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/primalize/single.rb', line 411

def coerce value
  if @coercion
    return @coercion.call(value)
  end

  type = @types.find { |type| type === value }
  if type.respond_to? :coerce
    type.coerce(value)
  else
    value
  end
end

#inspectObject



424
425
426
427
# File 'lib/primalize/single.rb', line 424

def inspect
  params = "(#{@types.map(&:inspect).join(', ')})"
  "any#{@types.empty? ? nil : params}"
end