lib/active_shipping/package.rb in active_shipping-1.14.2 vs lib/active_shipping/package.rb in active_shipping-2.0.0

- old
+ new

@@ -1,44 +1,40 @@ module ActiveShipping #:nodoc: class Package - include Quantified - cattr_accessor :default_options attr_reader :options, :value, :currency # 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(Measured::Weight.new(100, :g), [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 @dimensions = [dimensions].flatten.reject(&:nil?) - imperial = (options[:units] == :imperial) || - ([grams_or_ounces, *dimensions].all? { |m| m.respond_to?(:unit) && m.unit.to_sym == :imperial }) + imperial = (options[:units] == :imperial) weight_imperial = dimensions_imperial = imperial if options.include?(:units) if options.include?(:weight_units) - weight_imperial = (options[:weight_units] == :imperial) || - (grams_or_ounces.respond_to?(:unit) && m.unit.to_sym == :imperial) + weight_imperial = (options[:weight_units] == :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 }) + dimensions_imperial = (options[:dim_units] == :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, Mass, @weight_unit_system, :grams, :ounces) + @weight = attribute_from_metric_or_imperial(grams_or_ounces, Measured::Weight, @weight_unit_system, :grams, :ounces) if @dimensions.blank? - @dimensions = [Length.new(0, (dimensions_imperial ? :inches : :centimetres))] * 3 + zero_length = Measured::Length.new(0, (dimensions_imperial ? :inches : :centimetres)) + @dimensions = [zero_length] * 3 else process_dimensions end @value = Package.cents_from(options[:value]) @@ -65,51 +61,51 @@ def gift? @gift end def ounces(options = {}) - weight(options).in_ounces.amount + weight(options).convert_to(:oz).value.to_f end alias_method :oz, :ounces def grams(options = {}) - weight(options).in_grams.amount + weight(options).convert_to(:g).value.to_f end alias_method :g, :grams def pounds(options = {}) - weight(options).in_pounds.amount + weight(options).convert_to(:lb).value.to_f end alias_method :lb, :pounds alias_method :lbs, :pounds def kilograms(options = {}) - weight(options).in_kilograms.amount + weight(options).convert_to(:kg).value.to_f end alias_method :kg, :kilograms alias_method :kgs, :kilograms def inches(measurement = nil) - @inches ||= @dimensions.map { |m| m.in_inches.amount } + @inches ||= @dimensions.map { |m| m.convert_to(:in).value.to_f } measurement.nil? ? @inches : measure(measurement, @inches) end alias_method :in, :inches def centimetres(measurement = nil) - @centimetres ||= @dimensions.map { |m| m.in_centimetres.amount } + @centimetres ||= @dimensions.map { |m| m.convert_to(:cm).value.to_f } measurement.nil? ? @centimetres : measure(measurement, @centimetres) end alias_method :cm, :centimetres 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) - @weight_unit_system == :imperial ? m.in_ounces : m + 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 end @@ -141,11 +137,11 @@ end end def measure(measurement, ary) case measurement - when Fixnum then ary[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]) @@ -154,10 +150,10 @@ end end def process_dimensions @dimensions = @dimensions.map do |l| - attribute_from_metric_or_imperial(l, Length, @dimensions_unit_system, :centimetres, :inches) + 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|