Sha256: e40bdbd7b6f552a6941c426e28f1e1d3c8f02b07b0a25ad659f8d76fe2ef3349

Contents?: true

Size: 1.86 KB

Versions: 16

Compression:

Stored size: 1.86 KB

Contents

module ActiveShipping #:nodoc:
  class PackageItem
    include Quantified

    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) ||
                 (grams_or_ounces.respond_to?(:unit) && m.unit.to_sym == :imperial)

      @unit_system = imperial ? :imperial : :metric

      @weight = attribute_from_metric_or_imperial(grams_or_ounces, Mass, :grams, :ounces)

      @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 = Mass.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).in_ounces.amount
    end
    alias_method :oz, :ounces

    def grams(options = {})
      weight(options).in_grams.amount
    end
    alias_method :g, :grams

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

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

    private

    def attribute_from_metric_or_imperial(obj, klass, metric_unit, imperial_unit)
      if obj.is_a?(klass)
        return value
      else
        return klass.new(obj, (@unit_system == :imperial ? imperial_unit : metric_unit))
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
active_shipping-1.14.2 lib/active_shipping/package_item.rb
active_shipping-1.14.1 lib/active_shipping/package_item.rb
active_shipping-1.14.0 lib/active_shipping/package_item.rb
active_shipping-1.13.4 lib/active_shipping/package_item.rb
active_shipping-1.13.3 lib/active_shipping/package_item.rb
active_shipping-1.13.2 lib/active_shipping/package_item.rb
active_shipping-1.13.1 lib/active_shipping/package_item.rb
active_shipping-1.13.0 lib/active_shipping/package_item.rb
active_shipping-1.12.1 lib/active_shipping/package_item.rb
active_shipping-1.12.0 lib/active_shipping/package_item.rb
active_shipping-1.11.1 lib/active_shipping/package_item.rb
active_shipping-1.11.0 lib/active_shipping/package_item.rb
active_shipping-1.10.1 lib/active_shipping/package_item.rb
active_shipping-1.9.2 lib/active_shipping/package_item.rb
active_shipping-1.9.1 lib/active_shipping/package_item.rb
active_shipping-1.9.0 lib/active_shipping/package_item.rb