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

Change Package interface for ActiveShipping 2.0 #469

Closed
wants to merge 2 commits into from
Closed
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
162 changes: 36 additions & 126 deletions lib/active_shipping/package.rb
Original file line number Diff line number Diff line change
@@ -1,118 +1,63 @@
module ActiveShipping #:nodoc:
class Package
cattr_accessor :default_options
attr_reader :options, :value, :currency
self.default_options = { shape: 'box' }

# Package.new(100, [10, 20, 30], :units => :metric)
# Package.new(Mass.new(100, :grams), [10, 20, 30].map {|m| Length.new(m, :centimetres)})
# Package.new(100.grams, [10, 20, 30].map(&:centimetres))
def initialize(grams_or_ounces, dimensions, options = {})
options = @@default_options.update(options) if @@default_options
options.symbolize_keys!
@options = options
CYLINDER_ALIASES = %w(cylinder tube).freeze

@dimensions = [dimensions].flatten.reject(&:nil?)
attr_reader :weight, :length, :width, :height, :options, :shape, :value, :currency

imperial = (options[:units] == :imperial) ||
([grams_or_ounces, *dimensions].all? { |m| m.respond_to?(:unit) && m.unit.to_sym == :imperial })
alias_attribute :mass, :weight

weight_imperial = dimensions_imperial = imperial if options.include?(:units)
def initialize(weight:, length:, width:, height: nil, options: {})
@options = @@default_options.merge(options).symbolize_keys
@shape = @options[:shape].to_s

if options.include?(:weight_units)
weight_imperial = (options[:weight_units] == :imperial) ||
(grams_or_ounces.respond_to?(:unit) && m.unit.to_sym == :imperial)
end

if options.include?(:dim_units)
dimensions_imperial = (options[:dim_units] == :imperial) ||
(dimensions && dimensions.all? { |m| m.respond_to?(:unit) && m.unit.to_sym == :imperial })
end

@weight_unit_system = weight_imperial ? :imperial : :metric
@dimensions_unit_system = dimensions_imperial ? :imperial : :metric

@weight = attribute_from_metric_or_imperial(grams_or_ounces, Measured::Weight, @weight_unit_system, :grams, :ounces)

if @dimensions.blank?
zero_length = Measured::Length.new(0, (dimensions_imperial ? :inches : :centimetres))
@dimensions = [zero_length] * 3
else
process_dimensions
end

@value = Package.cents_from(options[:value])
@currency = options[:currency] || (options[:value].currency if options[:value].respond_to?(:currency))
@cylinder = (options[:cylinder] || options[:tube]) ? true : false
@gift = options[:gift] ? true : false
@oversized = options[:oversized] ? true : false
@unpackaged = options[:unpackaged] ? true : false
end
raise ArgumentError, "Weight needs to be a Measured::Measurable object" unless weight.is_a?(Measured::Measurable)
raise ArgumentError, "Length needs to be a Measured::Measurable object" unless length.is_a?(Measured::Measurable)
raise ArgumentError, "Width needs to be a Measured::Measurable object" unless width.is_a?(Measured::Measurable)
raise ArgumentError, "Height needs to be a Measured::Measurable object" unless height.nil? || length.is_a?(Measured::Measurable)

def unpackaged?
@unpackaged
end
@weight = weight

def oversized?
@oversized
end

def cylinder?
@cylinder
end
alias_method :tube?, :cylinder?

def gift?
@gift
end
@length = length
@width = width
@height = height || ( cylinder? ? width : Measured::Length.new(0, length.unit) )

def ounces(options = {})
weight(options).convert_to(:oz).value
@value = self.class.cents_from(options[:value])
@currency = options[:value].try(:currency) || options[:currency]
end
alias_method :oz, :ounces

def grams(options = {})
weight(options).convert_to(:g).value
def dimensions
@dimensions ||= [@length, @width, @height]
end
alias_method :g, :grams

def pounds(options = {})
weight(options).convert_to(:lb).value
def girth
@girth ||= cylinder ? width.scale(Math::PI) : (width + height).scale(2)
end
alias_method :lb, :pounds
alias_method :lbs, :pounds
alias_method :circumference, :girth
alias_method :around, :girth

def kilograms(options = {})
weight(options).convert_to(:kg).value
def volume
@volume ||= if cylinder?
Math::PI * width.scale(0.5).value * height.scale(0.5).value * length.value
else
length.value * width.value * height.value
end
end
alias_method :kg, :kilograms
alias_method :kgs, :kilograms

def inches(measurement = nil)
@inches ||= @dimensions.map { |m| m.convert_to(:in).value }
measurement.nil? ? @inches : measure(measurement, @inches)
def box?
@box ||= @shape == 'box'
end
alias_method :in, :inches

def centimetres(measurement = nil)
@centimetres ||= @dimensions.map { |m| m.convert_to(:cm).value }
measurement.nil? ? @centimetres : measure(measurement, @centimetres)
def cylinder?
@cylinder ||= @shape.in?(CYLINDER_ALIASES)
end
alias_method :cm, :centimetres
alias_method :tube?, :cylinder?

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)
@weight_unit_system == :imperial ? m.convert_to(:oz) : m
end
when :billable
[weight, weight(:type => :volumetric)].max
end
def envelope?
@envelope ||= @shape == 'envelope'
end
alias_method :mass, :weight

def self.cents_from(money)
return nil if money.nil?
Expand All @@ -129,40 +74,5 @@ def self.cents_from(money)
end
end
end

private

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

def measure(measurement, ary)
case measurement
when Integer then ary[measurement]
when :x, :max, :length, :long then ary[2]
when :y, :mid, :width, :wide then ary[1]
when :z, :min, :height, :depth, :high, :deep then ary[0]
when :girth, :around, :circumference
self.cylinder? ? (Math::PI * (ary[0] + ary[1]) / 2) : (2 * ary[0]) + (2 * ary[1])
when :volume then self.cylinder? ? (Math::PI * (ary[0] + ary[1]) / 4)**2 * ary[2] : measure(:box_volume, ary)
when :box_volume then ary[0] * ary[1] * ary[2]
end
end

def process_dimensions
@dimensions = @dimensions.map do |l|
attribute_from_metric_or_imperial(l, Measured::Length, @dimensions_unit_system, :centimetres, :inches)
end.sort
# [1,2] => [1,1,2]
# [5] => [5,5,5]
# etc..
2.downto(@dimensions.length) do |_n|
@dimensions.unshift(@dimensions[0])
end
end
end
end
Loading