Class: Shipping::Package

Inherits:
Object
  • Object
show all
Defined in:
app/services/shipping/package.rb

Overview

Service object: package.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grams_or_ounces, dimensions, options = {}) ⇒ Package

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))



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/shipping/package.rb', line 12

def initialize(grams_or_ounces, dimensions, options = {})
  options = self.class.default_options.update(options) if self.class.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 }

  @unit_system = imperial ? :imperial : :metric

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

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

  @value = Package.dollars_from(options[:value])
  @currency = options[:currency] || (options[:value].currency if options[:value].respond_to?(:currency))
  @cylinder = options[:cylinder] || options[:tube] ? true : false
  @pallet = options[:pallet] ? true : false
  @crate = options[:crate] ? true : false
  @container_code = options[:container_code]
  @shipment_id = options[:shipment_id]
  @nmfc_code = options[:nmfc_code]
  @flat_rate_package_type = options[:flat_rate_package_type]
end

Instance Attribute Details

#container_codeObject (readonly)

Returns the value of attribute container_code.



7
8
9
# File 'app/services/shipping/package.rb', line 7

def container_code
  @container_code
end

#currencyObject (readonly)

Returns the value of attribute currency.



7
8
9
# File 'app/services/shipping/package.rb', line 7

def currency
  @currency
end

#flat_rate_package_typeObject (readonly)

Returns the value of attribute flat_rate_package_type.



7
8
9
# File 'app/services/shipping/package.rb', line 7

def flat_rate_package_type
  @flat_rate_package_type
end

#nmfc_codeObject (readonly)

Returns the value of attribute nmfc_code.



7
8
9
# File 'app/services/shipping/package.rb', line 7

def nmfc_code
  @nmfc_code
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'app/services/shipping/package.rb', line 7

def options
  @options
end

#shipment_idObject (readonly)

Returns the value of attribute shipment_id.



7
8
9
# File 'app/services/shipping/package.rb', line 7

def shipment_id
  @shipment_id
end

#valueObject (readonly)

Returns the value of attribute value.



7
8
9
# File 'app/services/shipping/package.rb', line 7

def value
  @value
end

Class Method Details

.dollars_from(money) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'app/services/shipping/package.rb', line 105

def self.dollars_from(money)
  return nil if money.nil?

  if money.respond_to?(:dollars)
    money.dollars
  else
    money.to_f.round(2)
  end
end

Instance Method Details

#centimetres(measurement = nil) ⇒ Object Also known as: cm



84
85
86
87
# File 'app/services/shipping/package.rb', line 84

def centimetres(measurement = nil)
  @centimetres ||= @dimensions.map { |m| m.in_centimetres.amount }
  measurement.nil? ? @centimetres : measure(measurement, @centimetres)
end

#crate?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'app/services/shipping/package.rb', line 47

def crate?
  @crate
end

#cylinder?Boolean Also known as: tube?

Returns:

  • (Boolean)


51
52
53
# File 'app/services/shipping/package.rb', line 51

def cylinder?
  @cylinder
end

#grams(options = {}) ⇒ Object Also known as: g



61
62
63
# File 'app/services/shipping/package.rb', line 61

def grams(options = {})
  weight(options).in_grams.amount
end

#inches(measurement = nil) ⇒ Object Also known as: in



78
79
80
81
# File 'app/services/shipping/package.rb', line 78

def inches(measurement = nil)
  @inches ||= @dimensions.map { |m| m.in_inches.amount }
  measurement.nil? ? @inches : measure(measurement, @inches)
end

#kilograms(options = {}) ⇒ Object Also known as: kg, kgs



72
73
74
# File 'app/services/shipping/package.rb', line 72

def kilograms(options = {})
  weight(options).in_kilograms.amount
end

#ounces(options = {}) ⇒ Object Also known as: oz



56
57
58
# File 'app/services/shipping/package.rb', line 56

def ounces(options = {})
  weight(options).in_ounces.amount
end

#pallet?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'app/services/shipping/package.rb', line 43

def pallet?
  @pallet
end

#pounds(options = {}) ⇒ Object Also known as: lb, lbs



66
67
68
# File 'app/services/shipping/package.rb', line 66

def pounds(options = {})
  weight(options).in_pounds.amount
end

#weight(options = {}) ⇒ Object Also known as: mass



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/services/shipping/package.rb', line 90

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