Sha256: 2d631bc09bcb0b1455695e3ce066e67ceb794fd0c29c9943df9520bacc3918b7

Contents?: true

Size: 1.54 KB

Versions: 130

Compression:

Stored size: 1.54 KB

Contents

module ActiveMerchant #: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 < HashWithIndifferentAccess

      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 << "#{key.to_s.humanize} #{messages.first}"
          end
        end

        result
      end   
    end  
  end
end

Version data entries

130 entries across 130 versions & 31 rubygems

Version Path
smulube-activemerchant-1.7.0.1 lib/active_merchant/common/validateable.rb
johnreitano-activemerchant-1.5.7 lib/active_merchant/lib/validateable.rb
smulube-activemerchant-1.7.0 lib/active_merchant/common/validateable.rb
active_fulfillment-1.0.0 lib/active_merchant/common/validateable.rb
activemerchant-1.7.0 lib/active_merchant/common/validateable.rb
active_fulfillment-0.10.0 lib/active_merchant/common/validateable.rb
activemerchant-1.6.0 lib/active_merchant/common/validateable.rb
active_shipping-0.9.1 lib/active_merchant/common/validateable.rb
bitfluent-activemerchant-1.5.1.1 lib/active_merchant/common/validateable.rb
johnreitano-activemerchant-1.5.5 lib/active_merchant/lib/validateable.rb
johnreitano-activemerchant-1.5.4 lib/active_merchant/lib/validateable.rb
smulube-activemerchant-1.5.1.4 lib/active_merchant/lib/validateable.rb
smulube-activemerchant-1.5.1.3 lib/active_merchant/lib/validateable.rb
johnreitano-activemerchant-1.5.3 lib/active_merchant/lib/validateable.rb
johnreitano-activemerchant-1.5.2 lib/active_merchant/lib/validateable.rb
active_shipping-0.1.4 lib/active_shipping/lib/validateable.rb
smulube-activemerchant-1.5.1.2 lib/active_merchant/lib/validateable.rb
smulube-activemerchant-1.5.1.1 lib/active_merchant/lib/validateable.rb
mysmallidea-activemerchant-1.5.1 lib/active_merchant/lib/validateable.rb
mcmire-activemerchant-1.5.2 lib/active_merchant/lib/validateable.rb