Module: FinchAPI::Union Private

Includes:
Converter
Included in:
Models::HRIS::DocumentRetreiveResponse, Models::WebhookEvent
Defined in:
lib/finch-api/base_model.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Examples:

# `document_retreive_response` is a `FinchAPI::Models::HRIS::DocumentRetreiveResponse`
case document_retreive_response
when FinchAPI::Models::HRIS::W42020
  puts(document_retreive_response.data)
when FinchAPI::Models::HRIS::W42005
  puts(document_retreive_response.type)
else
  puts(document_retreive_response)
end
case document_retreive_response
in {type: :w4_2020, data: data, year: year}
  puts(data)
in {type: :w4_2005, data: data, year: year}
  puts(year)
else
  puts(document_retreive_response)
end

Instance Method Summary collapse

Methods included from Converter

coerce, dump, type_info

Instance Method Details

#==(other) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


521
522
523
# File 'lib/finch-api/base_model.rb', line 521

def ==(other)
  other.is_a?(Module) && other.singleton_class <= FinchAPI::Union && other.derefed_variants == derefed_variants
end

#===(other) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


512
513
514
515
516
# File 'lib/finch-api/base_model.rb', line 512

def ===(other)
  known_variants.any? do |_, variant_fn|
    variant_fn.call === other
  end
end

#coerce(value, state:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • value (Object)
  • state (Hash{Symbol=>Object})

    .

    @option state [Boolean, :strong] :strictness

    @option state [HashSymbol=>Object] :exactness

    @option state [Integer] :branched

Returns:

  • (Object)


538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/finch-api/base_model.rb', line 538

def coerce(value, state:)
  if (target = resolve_variant(value))
    return FinchAPI::Converter.coerce(target, value, state: state)
  end

  strictness = state.fetch(:strictness)
  exactness = state.fetch(:exactness)
  state[:strictness] = strictness == :strong ? true : strictness

  alternatives = []
  known_variants.each do |_, variant_fn|
    target = variant_fn.call
    exact = state[:exactness] = {yes: 0, no: 0, maybe: 0}
    state[:branched] += 1

    coerced = FinchAPI::Converter.coerce(target, value, state: state)
    yes, no, maybe = exact.values
    if (no + maybe).zero? || (!strictness && yes.positive?)
      exact.each { exactness[_1] += _2 }
      state[:exactness] = exactness
      return coerced
    elsif maybe.positive?
      alternatives << [[-yes, -maybe, no], exact, coerced]
    end
  end

  case alternatives.sort_by(&:first)
  in []
    exactness[:no] += 1
    if strictness == :strong
      message = "no possible conversion of #{value.class} into a variant of #{target.inspect}"
      raise ArgumentError.new(message)
    end
    value
  in [[_, exact, coerced], *]
    exact.each { exactness[_1] += _2 }
    coerced
  end
    .tap { state[:exactness] = exactness }
ensure
  state[:strictness] = strictness
end

#dump(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • value (Object)

Returns:

  • (Object)


586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/finch-api/base_model.rb', line 586

def dump(value)
  if (target = resolve_variant(value))
    return FinchAPI::Converter.dump(target, value)
  end

  known_variants.each do
    target = _2.call
    return FinchAPI::Converter.dump(target, value) if target === value
  end

  super
end

#variantsArray<Object>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

All of the specified variants for this union.

Returns:

  • (Array<Object>)


446
# File 'lib/finch-api/base_model.rb', line 446

def variants = derefed_variants.map(&:last)