Sha256: f1860c822c2c42bc5e81dc3b2f221b19dc6ba66b0985be4d7fbc51bcf96dc727

Contents?: true

Size: 1003 Bytes

Versions: 3

Compression:

Stored size: 1003 Bytes

Contents

require "membrane/errors"
require "membrane/schemas/base"

module Membrane
  module Schema
  end
end

class Membrane::Schemas::Enum < Membrane::Schemas::Base
  attr_reader :elem_schemas

  def initialize(*elem_schemas)
    @elem_schemas = elem_schemas
  end

  def validate(object)
    EnumValidator.new(@elem_schemas, object).validate
  end

  class EnumValidator
    def initialize(elem_schemas, object)
      @elem_schemas = elem_schemas
      @object = object
    end

    def validate
      @elem_schemas.each do |schema|
        begin
          schema.validate(@object)
          return nil
        rescue Membrane::SchemaValidationError
        end
      end

      fail!(@elem_schemas, @object)
    end

    private

    def fail!(elem_schemas, object)
      elem_schema_str = elem_schemas.map { |s| s.to_s }.join(", ")

      emsg = "Object #{object} doesn't validate" \
           + " against any of #{elem_schema_str}"
      raise Membrane::SchemaValidationError.new(emsg)
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
membrane-1.1.0 lib/membrane/schemas/enum.rb
membrane-1.0.0 lib/membrane/schemas/enum.rb
membrane-0.0.5 lib/membrane/schemas/enum.rb