Sha256: 571ed44a2f1a27bf4edd725c90aa4ecb22940158c531b93beab4f8b965f9f5cd

Contents?: true

Size: 977 Bytes

Versions: 8

Compression:

Stored size: 977 Bytes

Contents

module CFoundry
  module Validator
    class << self
      def value_matches?(val, type)
        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)
            }
        else
          val.is_a?(Object.const_get(type.to_s.capitalize))
        end
      end

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
cfoundry-0.4.16 lib/cfoundry/validator.rb
cfoundry-0.4.15 lib/cfoundry/validator.rb
cfoundry-0.4.14 lib/cfoundry/validator.rb
cfoundry-0.4.13 lib/cfoundry/validator.rb
cfoundry-0.4.12 lib/cfoundry/validator.rb
cfoundry-0.4.11 lib/cfoundry/validator.rb
cfoundry-0.4.10 lib/cfoundry/validator.rb
cfoundry-0.4.9 lib/cfoundry/validator.rb