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.20.0 lib/haveapi/client/validator.rb
haveapi-client-0.19.3 lib/haveapi/client/validator.rb
haveapi-client-0.19.2 lib/haveapi/client/validator.rb
haveapi-client-0.19.1 lib/haveapi/client/validator.rb
haveapi-client-0.19.0 lib/haveapi/client/validator.rb
haveapi-client-0.18.2 lib/haveapi/client/validator.rb
haveapi-client-0.18.1 lib/haveapi/client/validator.rb
haveapi-client-0.18.0 lib/haveapi/client/validator.rb
haveapi-client-0.17.0 lib/haveapi/client/validator.rb
haveapi-client-0.16.3 lib/haveapi/client/validator.rb
haveapi-client-0.16.2 lib/haveapi/client/validator.rb
haveapi-client-0.16.1 lib/haveapi/client/validator.rb
haveapi-client-0.16.0 lib/haveapi/client/validator.rb
haveapi-client-0.15.1 lib/haveapi/client/validator.rb
haveapi-client-0.15.0 lib/haveapi/client/validator.rb
haveapi-client-0.14.2 lib/haveapi/client/validator.rb
haveapi-client-0.14.1 lib/haveapi/client/validator.rb
haveapi-client-0.14.0 lib/haveapi/client/validator.rb
haveapi-client-0.13.3 lib/haveapi/client/validator.rb
haveapi-client-0.13.2 lib/haveapi/client/validator.rb