Module: FinchAPI::Type::Union Private

Includes:
Converter
Included in:
Models::HRIS::DocumentRetreiveResponse, Models::WebhookEvent
Defined in:
lib/finch-api/type/union.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)


122
123
124
# File 'lib/finch-api/type/union.rb', line 122

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)


113
114
115
116
117
# File 'lib/finch-api/type/union.rb', line 113

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)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/finch-api/type/union.rb', line 139

def coerce(value, state:)
  if (target = resolve_variant(value))
    return FinchAPI::Type::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::Type::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)


187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/finch-api/type/union.rb', line 187

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

  known_variants.each do
    target = _2.call
    return FinchAPI::Type::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>)


47
# File 'lib/finch-api/type/union.rb', line 47

def variants = derefed_variants.map(&:last)