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.



368
369
370
371
# File 'lib/primalize/single.rb', line 368

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

Instance Method Details

#===(value) ⇒ Object



373
374
375
# File 'lib/primalize/single.rb', line 373

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

#coerce(value) ⇒ Object



377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/primalize/single.rb', line 377

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



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

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