Sha256: 01dda9084d7420f9ff0befea73f5c358c228313f9b1e9f115979ef23e0437ce0

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

module Kosher

  # An offer by a book seller.
  class Offer < Struct.new(:id, :venue, :item, :seller, :shipping, :readable)

    include Comparable

    class << self

      # The base currency.
      attr_accessor :base_currency
    end

    self.base_currency = 'EUR'

    # Compares offer with another offer.
    #
    # A kosher offer is better than an unkosher offer. If both offers are
    # kosher or unkosher, a lower-priced offer is better.
    def <=>(other)
      if kosher? != other.kosher?
        kosher? ? -1 : 1
      else
        exchanged_price <=> other.exchanged_price
      end
    end

    # The price exchanged to the base currency.
    def exchanged_price
      price.exchange_to(base_currency)
    end

    # Returns whether the offer is kosher.
    def kosher?
      item.kosher? && seller.kosher? && shipping.kosher?
    end

    # The total price, including the shipping cost.
    def price
      item.price + shipping.cost
    end

    private

    def base_currency
      self.class.base_currency
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kosher-0.2.24 lib/kosher/offer.rb
kosher-0.2.23 lib/kosher/offer.rb
kosher-0.2.22 lib/kosher/offer.rb