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.



384
385
386
387
# File 'lib/primalize/single.rb', line 384

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

Instance Method Details

#===(value) ⇒ Object



389
390
391
# File 'lib/primalize/single.rb', line 389

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

#coerce(value) ⇒ Object



393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/primalize/single.rb', line 393

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



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

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