Sha256: 4767c4a8fd454881fd7ebf7f1aaa2ed22b8408a54f1a3b4b79b45ec55af91637

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

# encoding: utf-8
module SEPA
  class Transaction
    include ActiveModel::Validations
    extend Converter

    DEFAULT_REQUESTED_DATE = Date.new(1999, 1, 1).freeze

    attr_accessor :name, :iban, :bic, :amount, :instruction, :reference, :remittance_information, :requested_date, :batch_booking, :currency, :debtor_address

    convert :name, :instruction, :reference, :remittance_information, to: :text
    convert :amount, to: :decimal

    validates_length_of :name, within: 1..70
    validates_length_of :currency, is: 3
    validates_length_of :instruction, within: 1..35, allow_nil: true
    validates_length_of :reference, within: 1..35, allow_nil: true
    validates_length_of :remittance_information, within: 1..140, allow_nil: true
    validates_numericality_of :amount, greater_than: 0
    validates_presence_of :requested_date
    validates_inclusion_of :batch_booking, :in => [true, false]
    validates_with BICValidator, IBANValidator, message: "%{value} is invalid"

    def initialize(attributes = {})
      attributes.each do |name, value|
        send("#{name}=", value)
      end

      self.requested_date ||= DEFAULT_REQUESTED_DATE
      self.reference ||= 'NOTPROVIDED'
      self.batch_booking = true if self.batch_booking.nil?
      self.currency ||= 'EUR'
    end

    protected

    def validate_requested_date_after(min_requested_date)
      return unless requested_date.is_a?(Date)

      if requested_date != DEFAULT_REQUESTED_DATE && requested_date < min_requested_date
        errors.add(:requested_date, "must be greater or equal to #{min_requested_date}, or nil")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sepa_king-0.11.0 lib/sepa_king/transaction.rb