Sha256: fdb7c54d821b05eb2a9d7c801ae9b13f3defaf4034ff023832e9f8dcacca7675

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

module MerbMerchant #:nodoc:  
  module Validateable #:nodoc:
    def valid?
      errors.clear

      before_validate if respond_to?(:before_validate, true)
      validate if respond_to?(:validate, true)

      errors.empty?
    end  

    def initialize(attributes = {})
      self.attributes = attributes
    end

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

    private

    def attributes=(attributes)
      unless attributes.nil?
        for key, value in attributes
          send("#{key}=", value )            
        end
      end
    end  

    # This hash keeps the errors of the object
    class Errors < Mash

      def initialize(base)
        @base = base
      end
      
      def count
        size
      end

      # returns a specific fields error message. 
      # if more than one error is available we will only return the first. If no error is available 
      # we return an empty string
      def on(field)
        self[field].to_a.first
      end

      def add(field, error)
        self[field] ||= []
        self[field] << error
      end    
      
      def add_to_base(error)
        add(:base, error)
      end

      def each_full
        full_messages.each { |msg| yield msg }      
      end

      def full_messages
        result = []

        self.each do |key, messages| 
          if key == 'base'
            result << "#{messages.first}"
          else
            result << "#{MerbMerchant::Inflector.humanize(key.to_s)} #{messages.first}"
          end
        end

        result
      end   
    end  
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
merb_merchant-1.4.1 lib/merb_merchant/lib/validateable.rb