Sha256: 32fd1ac0d9e53584947f66ac533fea8fc301cc264009000b50a6482659288446

Contents?: true

Size: 1.26 KB

Versions: 50

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

require 'set'

module Ree::Contracts
  class Validators
    FORBIDDEN_CONTRACTS = Set.new([
      ArgContracts::None, ArgContracts::Kwargs,
      ArgContracts::Block, ArgContracts::Optblock,
      ArgContracts::SplatOf, ArgContracts::Splat
    ])

    class << self
      def fetch_for(contract)
        validators[contract.object_id] ||= build(contract)
      end

      private

      def build(contract)
        if FORBIDDEN_CONTRACTS.include?(contract)
          name = contract.name.split("::").last
          raise Ree::Error.new("#{name} is not supported arg validator", :invalid_dsl_usage)
        end

        return ProcValidator.new(contract) if contract.is_a?(Proc)
        return ArrayValidator.new(contract) if contract.is_a?(Array)
        return HashValidator.new(contract) if contract.is_a?(Hash)
        return RangeValidator.new(contract) if contract.is_a?(Range)
        return RegexpValidator.new(contract) if contract.is_a?(Regexp)
        return ValidValidator.new(contract) if contract.respond_to?(:valid?)
        return ClassValidator.new(contract) if contract.is_a?(Class) || contract.is_a?(Module)
        
        DefaultValidator.new(contract)
      end

      def validators
        @validators ||= {}
      end
    end
  end
end

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
ree-1.1.1 lib/ree/contracts/validators.rb
ree-1.1.0 lib/ree/contracts/validators.rb
ree-1.0.47 lib/ree/contracts/validators.rb
ree-1.0.46 lib/ree/contracts/validators.rb
ree-1.0.45 lib/ree/contracts/validators.rb
ree-1.0.44 lib/ree/contracts/validators.rb
ree-1.0.43 lib/ree/contracts/validators.rb
ree-1.0.42 lib/ree/contracts/validators.rb
ree-1.0.41 lib/ree/contracts/validators.rb
ree-1.0.40 lib/ree/contracts/validators.rb
ree-1.0.39 lib/ree/contracts/validators.rb
ree-1.0.38 lib/ree/contracts/validators.rb
ree-1.0.37 lib/ree/contracts/validators.rb
ree-1.0.36 lib/ree/contracts/validators.rb
ree-1.0.35 lib/ree/contracts/validators.rb
ree-1.0.34 lib/ree/contracts/validators.rb
ree-1.0.33 lib/ree/contracts/validators.rb
ree-1.0.32 lib/ree/contracts/validators.rb
ree-1.0.31 lib/ree/contracts/validators.rb
ree-1.0.30 lib/ree/contracts/validators.rb