Sha256: 50966c5a1df7977e60d78bb12adf76b6e79e0b68ebc23a947785e0620cce9ae2

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

module SimpleShipping
  # Represents a shipment.
  #
  # == Attributes:
  # * _shipper_ (an instance of {SimpleShipping::Party}
  # * _recipient_ (an instance of {SimpleShipping::Party}
  # * _package_ (an instance of {SimpleShipping::Package}
  # * _payor_  (:shipper, :recipient). Default value is :shipper
  class Shipment < Abstract::Model
    attr_accessor :shipper,
                  :recipient,
                  :package,
                  :payor

    set_default_values :payor => :shipper

    validates_presence_of :shipper, :recipient, :package, :payor
    validates_inclusion_of :payor, :in => [:shipper, :recipient]
    validates_submodel :shipper  , :as => SimpleShipping::Party
    validates_submodel :recipient, :as => SimpleShipping::Party
    validates_submodel :package  , :as => SimpleShipping::Package
    validate :validate_payor_account_number

    # Account number of payor.
    def payor_account_number
      case payor
      when :shipper
        shipper.account_number   if shipper.respond_to?(:account_number)
      when :recipient
        recipient.account_number if recipient.respond_to?(:account_number)
      end
    end

    # Validate presence of payor account number.
    #
    # @return [void]
    def validate_payor_account_number
      errors.add(:abstract, "Payor account number is missing") unless payor_account_number
    end
    private :validate_payor_account_number
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simple_shipping-0.4.7 lib/simple_shipping/shipment.rb
simple_shipping-0.4.6 lib/simple_shipping/shipment.rb