Sha256: 9d033aa0993926572f58a2121bc17600d0c5fcdd6f844b889a4473cd824a8eee

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

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

module Membrane
  module Schema
  end
end

class Membrane::Schemas::Regexp < Membrane::Schemas::Base
  attr_reader :regexp

  def initialize(regexp)
    @regexp = regexp
  end

  def validate(object)
    StringValidator.new(object).validate
    MatchValidator.new(@regexp, object).validate

    nil
  end

  class StringValidator

    def initialize(object)
      @object = object
    end

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

    private

    def fail!
      emsg = "Expected instance of String, given instance of #{@object.class}"
      raise Membrane::SchemaValidationError.new(emsg)
    end

  end

  class MatchValidator

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

    def validate
      fail! if !@regexp.match(@object)
    end

    private

    def fail!
      emsg = "Value #{@object} doesn't match regexp #{@regexp}"
      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/regexp.rb