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.4.27 app/models/workarea/shipping_option.rb
workarea-core-3.5.4 app/models/workarea/shipping_option.rb
workarea-core-3.4.26 app/models/workarea/shipping_option.rb
workarea-core-3.5.3 app/models/workarea/shipping_option.rb
workarea-core-3.4.25 app/models/workarea/shipping_option.rb
workarea-core-3.5.2 app/models/workarea/shipping_option.rb
workarea-core-3.4.24 app/models/workarea/shipping_option.rb
workarea-core-3.5.1 app/models/workarea/shipping_option.rb
workarea-core-3.4.23 app/models/workarea/shipping_option.rb
workarea-core-3.4.22 app/models/workarea/shipping_option.rb
workarea-core-3.5.0 app/models/workarea/shipping_option.rb
workarea-core-3.4.21 app/models/workarea/shipping_option.rb
workarea-core-3.5.0.beta.1 app/models/workarea/shipping_option.rb
workarea-core-3.4.20 app/models/workarea/shipping_option.rb
workarea-core-3.4.19 app/models/workarea/shipping_option.rb
workarea-core-3.4.18 app/models/workarea/shipping_option.rb
workarea-core-3.4.17 app/models/workarea/shipping_option.rb
workarea-core-3.4.16 app/models/workarea/shipping_option.rb
workarea-core-3.4.15 app/models/workarea/shipping_option.rb
workarea-core-3.4.14 app/models/workarea/shipping_option.rb