Sha256: 52a890ad980f4b972f11c7c2f92c7d6b302996ee313bdd5091c17c54ae85a2d3

Contents?: true

Size: 1.02 KB

Versions: 40

Compression:

Stored size: 1.02 KB

Contents

module HaveAPI::Client
  module Validators ; end

  class Validator
    class << self
      def name(v)
        Validator.register(v, self)
      end
      
      def register(name, klass)
        @validators ||= {}
        @validators[name] = klass
      end

      def validate(validators, param, other_params)
        ret = []
        
        validators.each do |name, desc|
          fail "unsupported validator '#{name}'" if @validators[name].nil?

          v = @validators[name].new(desc, param, other_params)
          ret.concat(v.errors) unless v.valid?
        end

        ret.empty? ? true : ret
      end
    end

    attr_reader :value, :params

    def initialize(opts, value, other_params)
      @opts = opts
      @value = value
      @params = other_params.params
    end

    def errors
      @errors || [opts[:message] % { value: value }]
    end

    def valid?
      raise NotImplementedError
    end

    protected
    def opts
      @opts
    end

    def error(e)
      @errors ||= []
      @errors << e
    end
  end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
haveapi-client-0.13.1 lib/haveapi/client/validator.rb
haveapi-client-0.13.0 lib/haveapi/client/validator.rb
haveapi-client-0.12.1 lib/haveapi/client/validator.rb
haveapi-client-0.12.0 lib/haveapi/client/validator.rb
haveapi-client-0.11.1 lib/haveapi/client/validator.rb
haveapi-client-0.11.0 lib/haveapi/client/validator.rb
haveapi-client-0.10.0 lib/haveapi/client/validator.rb
haveapi-client-0.9.0 lib/haveapi/client/validator.rb
haveapi-client-0.8.0 lib/haveapi/client/validator.rb
haveapi-client-0.7.1 lib/haveapi/client/validator.rb
haveapi-client-0.7.0 lib/haveapi/client/validator.rb
haveapi-client-0.6.0 lib/haveapi/client/validator.rb
haveapi-client-0.5.4 lib/haveapi/client/validator.rb
haveapi-client-0.5.3 lib/haveapi/client/validator.rb
haveapi-client-0.5.2 lib/haveapi/client/validator.rb
haveapi-client-0.5.1 lib/haveapi/client/validator.rb
haveapi-client-0.5.0 lib/haveapi/client/validator.rb
haveapi-client-0.4.2 lib/haveapi/client/validator.rb
haveapi-client-0.4.1 lib/haveapi/client/validator.rb
haveapi-client-0.4.0 lib/haveapi/client/validator.rb