Sha256: a1c84af539f16bf1a3614c3c3c3f8d9a029c2ec0ccf2e9ba2600dc68f9c41e15

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 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!(@object) if !@object.kind_of?(String)
    end

    private

    def fail!(object)
      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!(@regexp, @object) if !@regexp.match(@object)
    end

    private

    def fail!(regexp, object)
      emsg = "Value #{object} doesn't match regexp #{regexp.inspect}"
      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/regexp.rb
membrane-1.0.0 lib/membrane/schemas/regexp.rb
membrane-0.0.5 lib/membrane/schemas/regexp.rb