Sha256: 210bfaf71a80ffafb7d1126736754ed1823f7349a8011a410a2f8d9f8a6c4533

Contents?: true

Size: 1.13 KB

Versions: 62

Compression:

Stored size: 1.13 KB

Contents

module Workarea
  class ShippingOption
    include GuardNegativePrice

    attr_reader :carrier, :name, :service_code, :tax_code
    attr_writer :price_adjustments

    def self.from_rate_estimate(rate)
      new(
        carrier: rate.carrier,
        name: rate.service_name,
        service_code: rate.service_code,
        price: Money.new(rate.price, rate.currency),
        tax_code: Shipping::Service.find_tax_code(
          rate.carrier,
          rate.service_name
        )
      )
    end

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

    def base_price
      (@price || 0).to_m
    end

    def price_adjustments
      @price_adjustments || []
    end

    def price
      guard_negative_price do
        base_price + price_adjustments.map(&:amount).sum.to_m
      end
    end

    def to_h
      {
        carrier: carrier,
        name: name,
        service_code: service_code,
        price: price,
        base_price: base_price,
        tax_code: tax_code
      }
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/models/workarea/shipping_option.rb
workarea-core-3.5.26 app/models/workarea/shipping_option.rb
workarea-core-3.4.45 app/models/workarea/shipping_option.rb
workarea-core-3.5.25 app/models/workarea/shipping_option.rb
workarea-core-3.5.23 app/models/workarea/shipping_option.rb
workarea-core-3.4.44 app/models/workarea/shipping_option.rb
workarea-core-3.5.22 app/models/workarea/shipping_option.rb
workarea-core-3.4.43 app/models/workarea/shipping_option.rb
workarea-core-3.5.21 app/models/workarea/shipping_option.rb
workarea-core-3.4.42 app/models/workarea/shipping_option.rb
workarea-core-3.5.20 app/models/workarea/shipping_option.rb
workarea-core-3.4.41 app/models/workarea/shipping_option.rb
workarea-core-3.5.19 app/models/workarea/shipping_option.rb
workarea-core-3.4.40 app/models/workarea/shipping_option.rb
workarea-core-3.5.18 app/models/workarea/shipping_option.rb
workarea-core-3.4.39 app/models/workarea/shipping_option.rb
workarea-core-3.5.17 app/models/workarea/shipping_option.rb
workarea-core-3.4.38 app/models/workarea/shipping_option.rb
workarea-core-3.5.16 app/models/workarea/shipping_option.rb
workarea-core-3.4.37 app/models/workarea/shipping_option.rb