Sha256: 456d4ec1801ce084dcf5e908fe05c6417fda7a010973324b1d7589b52dcb6715

Contents?: true

Size: 749 Bytes

Versions: 3

Compression:

Stored size: 749 Bytes

Contents

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

class Membrane::Schemas::Class < Membrane::Schemas::Base
  attr_reader :klass

  def initialize(klass)
    @klass = klass
  end

  # Validates whether or not the supplied object is derived from klass
  def validate(object)
    ClassValidator.new(@klass, object).validate
  end

  class ClassValidator

    def initialize(klass, object)
      @klass = klass
      @object = object
    end

    def validate
      fail!(@klass, @object) if !@object.kind_of?(@klass)
    end

    private

    def fail!(klass, object)
      emsg = "Expected instance of #{klass}," \
             + " given an instance of #{object.class}"
      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/class.rb
membrane-1.0.0 lib/membrane/schemas/class.rb
membrane-0.0.5 lib/membrane/schemas/class.rb