class RSchema::Schemas::Sum

A schema that represents a “sum type”

Values must conform to one of the subschemas.

@example A schema that matches both Integers and Strings

schema = RSchema.define { either(_String, _Integer) }
schema.valid?("hello") #=> true
schema.valid?(5) #=> true

Attributes

subschemas[R]

Public Class Methods

new(subschemas) click to toggle source
# File lib/rschema/schemas/sum.rb, line 18
def initialize(subschemas)
  @subschemas = subschemas
end

Public Instance Methods

call(value, options) click to toggle source
# File lib/rschema/schemas/sum.rb, line 22
def call(value, options)
  suberrors = []

  @subschemas.each do |ss|
    result = ss.call(value, options)
    return result if result.valid?
    suberrors << result.error
  end

  Result.failure(suberrors)
end
with_wrapped_subschemas(wrapper) click to toggle source
# File lib/rschema/schemas/sum.rb, line 34
def with_wrapped_subschemas(wrapper)
  wrapped_subschemas = subschemas.map { |ss| wrapper.wrap(ss) }
  self.class.new(wrapped_subschemas)
end