Sha256: fd1ce9585b8d7d0c58846021b7627cd151f1b64b9e18623136d19ed19da4e592

Contents?: true

Size: 1.02 KB

Versions: 8

Compression:

Stored size: 1.02 KB

Contents

module JFoundry
  module Validator
    class << self
      def value_matches?(val, type)
        return true if val.nil?

        case type
        when Class
          val.is_a?(type)
        when Regexp
          val.is_a?(String) && !!(val =~ type)
        when :url
          value_matches?(val, URI::regexp(%w(http https)))
        when :https_url
          value_matches?(val, URI::regexp("https"))
        when :boolean
          val.is_a?(TrueClass) || val.is_a?(FalseClass)
        when Array
          val.all? do |x|
            value_matches?(x, type.first)
          end
        when Hash
          val.is_a?(Hash) &&
            type.all? { |name, subtype|
              val.key?(name) && value_matches?(val[name], subtype)
            }
        when nil
          true
        else
          val.is_a?(Object.const_get(type.to_s.capitalize))
        end
      end

      def validate_type(val, type)
        unless value_matches?(val, type)
          raise JFoundry::Mismatch.new(type, val)
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
jfoundry-0.1.7 lib/jfoundry/validator.rb
jfoundry-0.1.6 lib/jfoundry/validator.rb
jfoundry-0.1.4 lib/jfoundry/validator.rb
jfoundry-0.1.3 lib/jfoundry/validator.rb
jfoundry-0.1.2 lib/jfoundry/validator.rb
jfoundry-0.1.1 lib/jfoundry/validator.rb
jfoundry-0.1.0.pre lib/jfoundry/validator.rb
jfoundry-0.1.0 lib/jfoundry/validator.rb