Sha256: ece3663de615ca02c09bb8113df30227f5984b37ed8676efefa1307adb21a85a

Contents?: true

Size: 1.59 KB

Versions: 62

Compression:

Stored size: 1.59 KB

Contents

module ActiveShipping #:nodoc:
  class PackageItem
    attr_reader :sku, :hs_code, :value, :name, :weight, :quantity, :options

    def initialize(name, grams_or_ounces, value, quantity, options = {})
      @name = name

      imperial = (options[:units] == :imperial)

      @unit_system = imperial ? :imperial : :metric

      @weight = grams_or_ounces
      @weight = Measured::Weight.new(grams_or_ounces, (@unit_system == :imperial ? :oz : :g)) unless @weight.is_a?(Measured::Weight)

      @value = Package.cents_from(value)
      @quantity = quantity > 0 ? quantity : 1

      @sku = options[:sku]
      @hs_code = options[:hs_code]
      @options = options
    end

    def weight(options = {})
      case options[:type]
      when nil, :actual
        @weight
      when :volumetric, :dimensional
        @volumetric_weight ||= begin
          m = Measured::Weight.new((centimetres(:box_volume) / 6.0), :grams)
          @unit_system == :imperial ? m.in_ounces : m
        end
      when :billable
        [weight, weight(type: :volumetric)].max
      end
    end
    alias_method :mass, :weight

    def ounces(options = {})
      weight(options).convert_to(:oz).value
    end
    alias_method :oz, :ounces

    def grams(options = {})
      weight(options).convert_to(:g).value
    end
    alias_method :g, :grams

    def pounds(options = {})
      weight(options).convert_to(:lb).value
    end
    alias_method :lb, :pounds
    alias_method :lbs, :pounds

    def kilograms(options = {})
      weight(options).convert_to(:kg).value
    end
    alias_method :kg, :kilograms
    alias_method :kgs, :kilograms
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

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