Sha256: 5c4b02ef3fcdf2e42ff282963e22b48cd566a2b2d37577bedef0a27a84c981d8

Contents?: true

Size: 1.2 KB

Versions: 7

Compression:

Stored size: 1.2 KB

Contents

module SimpleModel

  module Errors
    include ErrorHelpers

    def errors
      @errors ||= ErrorsHash.new
      @errors
    end

    def valid?
      self.errors.clear if errors
      validate
      self.errors.blank? || self.errors.empty?
    end

 
    def errors_on(attr)
      self.valid?
      [self.errors.on(attr.to_s)].flatten.compact
    end

    alias :error_on :errors_on

    def validate
      # Override to implement validation
    end

    class ErrorsHash
      attr_accessor :errors
      def initialize
        errors
      end

      def errors
        @errors ||= {}
        @errors
      end

      def clear
        self.errors = {}
      end

      def add(attr, message)
        errors[attr.to_s] ||= []
        errors[attr.to_s] << message
      end
      def count
        errors.length
      end
      def empty?
        errors.empty?
      end
      def full_messages
        full_messages = []
        errors.each do |error|
          error[1].each do |message|
            full_messages << "#{error[0].to_s} #{message}"
          end
        end
        full_messages
      end


      def on(attr)
        errors[attr.to_s] if errors[attr.to_s]
      end

      alias :[] :on
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
simple_model-0.2.0 lib/simple_model/errors.rb
simple_model-0.1.9 lib/simple_model/errors.rb
simple_model-0.1.8 lib/simple_model/errors.rb
simple_model-0.1.7 lib/simple_model/errors.rb
simple_model-0.1.6 lib/simple_model/errors.rb
simple_model-0.1.5 lib/simple_model/errors.rb
simple_model-0.1.4 lib/simple_model/errors.rb