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, try_strict_coerce, 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)


504
505
506
# File 'lib/finch-api/base_model.rb', line 504

def ==(other)
  other.is_a?(Module) && other.singleton_class.ancestors.include?(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)


495
496
497
498
499
# File 'lib/finch-api/base_model.rb', line 495

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

#coerce(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)


513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/finch-api/base_model.rb', line 513

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

  matches = []

  known_variants.each do |_, variant_fn|
    variant = variant_fn.call

    case FinchAPI::Converter.try_strict_coerce(variant, value)
    in [true, coerced, _]
      return coerced
    in [false, true, score]
      matches << [score, variant]
    in [false, false, _]
      nil
    end
  end

  _, variant = matches.sort! { _2.first <=> _1.first }.find { |score,| !score.zero? }
  variant.nil? ? value : FinchAPI::Converter.coerce(variant, value)
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)


542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/finch-api/base_model.rb', line 542

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

  known_variants.each do |_, variant_fn|
    variant = variant_fn.call
    if variant === value
      return FinchAPI::Converter.dump(variant, value)
    end
  end
  value
end

#try_strict_coerce(value) ⇒ Array(true, Object, nil), Array(false, Boolean, Integer)

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:

  • (Array(true, Object, nil), Array(false, Boolean, Integer))


561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/finch-api/base_model.rb', line 561

def try_strict_coerce(value)
  # TODO(ruby) this will result in super linear decoding behaviour for nested unions
  # follow up with a decoding context that captures current strictness levels
  if (variant = resolve_variant(value))
    return Converter.try_strict_coerce(variant, value)
  end

  coercible = false
  max_score = 0

  known_variants.each do |_, variant_fn|
    variant = variant_fn.call

    case FinchAPI::Converter.try_strict_coerce(variant, value)
    in [true, coerced, score]
      return [true, coerced, score]
    in [false, true, score]
      coercible = true
      max_score = [max_score, score].max
    in [false, false, _]
      nil
    end
  end

  [false, coercible, max_score]
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>)


425
426
427
# File 'lib/finch-api/base_model.rb', line 425

def variants
  derefed_variants.map(&:last)
end