Skip to content
This repository has been archived by the owner on Jun 13, 2018. It is now read-only.

Split up and test Package and PackageItem #435

Merged
merged 1 commit into from
Dec 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/active_shipping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require 'active_shipping/shipping_response'
require 'active_shipping/label_response'
require 'active_shipping/label'
require 'active_shipping/package_item'
require 'active_shipping/package'
require 'active_shipping/location'
require 'active_shipping/rate_estimate'
Expand Down
78 changes: 3 additions & 75 deletions lib/active_shipping/package.rb
Original file line number Diff line number Diff line change
@@ -1,78 +1,4 @@
module ActiveShipping #:nodoc:
# A package item is a unique item(s) that is physically in a package.
# A single package can have many items. This is only required
# for shipping methods (label creation) right now.
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

class Package
include Quantified

Expand Down Expand Up @@ -136,7 +62,9 @@ def cylinder?
end
alias_method :tube?, :cylinder?

def gift?; @gift end
def gift?
@gift
end

def ounces(options = {})
weight(options).in_ounces.amount
Expand Down
72 changes: 72 additions & 0 deletions lib/active_shipping/package_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
module ActiveShipping #:nodoc:
class PackageItem
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing in here changed. Just moved.

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
Loading