Sha256: 2b2e5685b7db39d189e824d58e29c9974ca244970bd0f826f92270283ed08581

Contents?: true

Size: 1.6 KB

Versions: 8

Compression:

Stored size: 1.6 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)
        super() { |h, k|  h[k] = [] ; h[k] }
        @base = base
      end

      def count
        size
      end

      def empty?
        all? { |k, v| v && v.empty? }
      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] << 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|
          next if messages.blank?
          if key == 'base'
            result << "#{messages.first}"
          else
            result << "#{key.to_s.humanize} #{messages.first}"
          end
        end

        result
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 5 rubygems

Version Path
activemerchant-1.17.0 lib/active_merchant/common/validateable.rb
yetanothernguyen-activemerchant-1.16.0 lib/active_merchant/common/validateable.rb
activemerchant-1.16.0 lib/active_merchant/common/validateable.rb
gonow-activemerchant-1.15.0 lib/active_merchant/common/validateable.rb
bitfluent-activemerchant-1.15.1 lib/active_merchant/common/validateable.rb
activemerchant-kiddy-1.15.0.kiddy.1 lib/active_merchant/common/validateable.rb
activemerchant-kiddy-1.15.0.kiddy lib/active_merchant/common/validateable.rb
activemerchant-1.15.0 lib/active_merchant/common/validateable.rb