Sha256: c369128b0c7cfdef3ec677ab52687ae2b272d319ef42b9c67729e4e51aa63dbc

Contents?: true

Size: 1.83 KB

Versions: 13

Compression:

Stored size: 1.83 KB

Contents

module Ohm
  module Validations
    class Presenter
      class UnhandledErrors < StandardError
        attr :errors

        def initialize(errors)
          @errors = errors
        end

        def message
          "Unhandled errors: #{errors.inspect}"
        end
      end

      def initialize(errors)
        @errors = errors
        @unhandled = errors.dup
        @output = []
      end

      def on(error, message = (block_given? ? yield : raise(ArgumentError)))
        handle(error) do
          @output << message
        end
      end

      def ignore(error)
        handle(error)
      end

      def present
        yield(self)
        raise UnhandledErrors.new(@unhandled) unless @unhandled.empty?
        @output
      end

    protected

      def handle(error)
        if (errors = @errors.select {|e| error === e }).any?
          @unhandled -= errors
          yield(errors) if block_given?
        end
      end
    end

    class Errors < Array
      attr_accessor :model

      def initialize(model)
        @model = model
      end

      def present(presenter = Presenter, &block)
        presenter.new(model.errors).present(&block)
      end
    end

    def valid?
      errors.clear
      validate
      errors.empty?
    end

    def validate
    end

    def errors
      @errors ||= Errors.new(self)
    end

  protected

    def assert_format(att, format, error = [att, :format])
      if assert_present(att, error)
        assert(send(att).to_s.match(format), error)
      end
    end

    def assert_present(att, error = [att, :not_present])
      assert(!send(att).to_s.empty?, error)
    end

    def assert_numeric(att, error = [att, :not_numeric])
      if assert_present(att, error)
        assert_format(att, /^\d+$/, error)
      end
    end

    def assert(value, error)
      value or errors.push(error) && false
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ohm-0.0.21 lib/ohm/validations.rb
ohm-0.0.20 lib/ohm/validations.rb
ohm-0.0.19 lib/ohm/validations.rb
ohm-0.0.18 lib/ohm/validations.rb
ohm-0.0.17 lib/ohm/validations.rb
ohm-0.0.9 lib/ohm/validations.rb
ohm-0.0.10 lib/ohm/validations.rb
ohm-0.0.11 lib/ohm/validations.rb
ohm-0.0.12 lib/ohm/validations.rb
ohm-0.0.13 lib/ohm/validations.rb
ohm-0.0.14 lib/ohm/validations.rb
ohm-0.0.15 lib/ohm/validations.rb
ohm-0.0.16 lib/ohm/validations.rb