Module: FinchAPI::Internal::Type::Union Private

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::HRIS::W42020
  puts(document_retreive_response.data)
when FinchAPI::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, inspect, type_info

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:



127
128
129
# File 'lib/finch_api/internal/type/union.rb', line 127

def ==(other)
  FinchAPI::Internal::Type::Union === other && other.derefed_variants == derefed_variants
end

#===(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:



116
117
118
119
120
# File 'lib/finch_api/internal/type/union.rb', line 116

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)


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
181
182
183
184
185
186
187
188
189
190
# File 'lib/finch_api/internal/type/union.rb', line 149

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

Returns:

  • (Object)


201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/finch_api/internal/type/union.rb', line 201

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

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

  super
end

#hashInteger

Returns:

  • (Integer)


134
# File 'lib/finch_api/internal/type/union.rb', line 134

def hash = variants.hash

#inspect(depth: 0) ⇒ String

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:

  • depth (Integer) (defaults to: 0)

Returns:

  • (String)


222
223
224
225
226
227
228
229
230
231
# File 'lib/finch_api/internal/type/union.rb', line 222

def inspect(depth: 0)
  if depth.positive?
    return is_a?(Module) ? super() : self.class.name
  end

  members = variants.map { FinchAPI::Internal::Type::Converter.inspect(_1, depth: depth.succ) }
  prefix = is_a?(Module) ? name : self.class.name

  "#{prefix}[#{members.join(' | ')}]"
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>)


48
# File 'lib/finch_api/internal/type/union.rb', line 48

def variants = derefed_variants.map(&:last)