class Yarrow::Schema::Types::Union

Public Class Methods

new(*type_opts) click to toggle source
Calls superclass method Yarrow::Schema::Types::TypeClass::new
# File lib/yarrow/schema/types.rb, line 219
def initialize(*type_opts)
  @options = type_opts
  super()
end
of(*unit_opts) click to toggle source
# File lib/yarrow/schema/types.rb, line 214
def self.of(*unit_opts)
  instances = unit_opts.map { |unit| Instance.of(unit) }
  new(*instances)
end

Public Instance Methods

check(input) click to toggle source
# File lib/yarrow/schema/types.rb, line 228
def check(input)
  failed_checks = []
  @options.each do |opt|
    begin
      opt.check(input)
    rescue CastError => err
      failed_checks << err
    end
  end

  if failed_checks.size == @options.size
    raise CastError.union_member(input.class, @options.map { |opt| opt.class })
  end

  input
end
|(rhs_opt) click to toggle source
# File lib/yarrow/schema/types.rb, line 224
def |(rhs_opt)
  @options << rhs_opt
end