Sha256: 3f663cb5d927484fe6279ee09dfcfe95250c27505686821db54cd2f007471cb2

Contents?: true

Size: 987 Bytes

Versions: 1

Compression:

Stored size: 987 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)
    end

    private

    def fail!(elem_schemas)
      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

1 entries across 1 versions & 1 rubygems

Version Path
membrane-0.0.4 lib/membrane/schemas/enum.rb