Class: Yarrow::Schema::Types::Union

Inherits:
TypeClass
  • Object
show all
Defined in:
lib/yarrow/schema/types.rb

Instance Attribute Summary

Attributes inherited from TypeClass

#accepts, #unit

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TypeClass

#accept, #cast, #check_instance_of!, #check_kind_of!, #check_respond_to_all!, #check_respond_to_any!, #coerce, #should_coerce?

Constructor Details

#initialize(*type_opts) ⇒ Union

Returns a new instance of Union.



249
250
251
252
# File 'lib/yarrow/schema/types.rb', line 249

def initialize(*type_opts)
  @options = type_opts
  super()
end

Class Method Details

.of(*unit_opts) ⇒ Object



244
245
246
247
# File 'lib/yarrow/schema/types.rb', line 244

def self.of(*unit_opts)
  instances = unit_opts.map { |unit| Instance.of(unit) }
  new(*instances)
end

Instance Method Details

#check(input) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/yarrow/schema/types.rb', line 258

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) ⇒ Object



254
255
256
# File 'lib/yarrow/schema/types.rb', line 254

def |(rhs_opt)
  @options << rhs_opt
end