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.



219
220
221
222
# File 'lib/yarrow/schema/types.rb', line 219

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

Class Method Details

.of(*unit_opts) ⇒ Object



214
215
216
217
# File 'lib/yarrow/schema/types.rb', line 214

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

Instance Method Details

#check(input) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# 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) ⇒ Object



224
225
226
# File 'lib/yarrow/schema/types.rb', line 224

def |(rhs_opt)
  @options << rhs_opt
end