Class: FreightKit::Package
- Inherits:
-
Object
- Object
- FreightKit::Package
- Defined in:
- lib/freight_kit/package.rb
Constant Summary collapse
- VALID_FREIGHT_CLASSES =
[55, 60, 65, 70, 77.5, 85, 92.5, 100, 110, 125, 150, 175, 200, 250, 300, 400].freeze
Instance Attribute Summary collapse
-
#currency ⇒ Object
readonly
Returns the value of attribute currency.
- #declared_freight_class ⇒ Object
-
#description ⇒ Object
Returns the value of attribute description.
-
#hazmat ⇒ Object
Returns the value of attribute hazmat.
-
#nmfc ⇒ Object
Returns the value of attribute nmfc.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#packaging ⇒ Object
readonly
Returns the value of attribute packaging.
-
#quantity ⇒ Object
Returns the value of attribute quantity.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #calculated_freight_class ⇒ Object
- #centimetres(measurement = nil) ⇒ Object (also: #cm)
- #cubic_ft(each_or_total) ⇒ Object
- #cylinder? ⇒ Boolean (also: #tube?)
- #density ⇒ Object
- #dim_weight ⇒ Object
- #each_weight(options = {}) ⇒ Object
- #freight_class ⇒ Object
- #gift? ⇒ Boolean
- #grams(options = {}) ⇒ Object (also: #g)
- #hazmat? ⇒ Boolean
- #height(unit) ⇒ Object
- #inches(measurement = nil) ⇒ Object (also: #in)
-
#initialize(total_grams_or_ounces, dimensions, packaging_type, options = {}) ⇒ Package
constructor
Package.new(100, [10, 20, 30], ‘pallet’, :units => :metric) Package.new(Measured::Weight.new(100, :g), ‘box’, [10, 20, 30].map {|m| Length.new(m, :centimetres)}) Package.new(100.grams, [10, 20, 30].map(&:centimetres)).
- #kilograms(options = {}) ⇒ Object (also: #kg, #kgs)
- #length(unit) ⇒ Object
- #ounces(options = {}) ⇒ Object (also: #oz)
- #oversized? ⇒ Boolean
- #pounds(args) ⇒ Object (also: #lb, #lbs)
- #total_weight(options = {}) ⇒ Object
- #unpackaged? ⇒ Boolean
- #width(unit) ⇒ Object
Constructor Details
#initialize(total_grams_or_ounces, dimensions, packaging_type, options = {}) ⇒ Package
Package.new(100, [10, 20, 30], ‘pallet’, :units => :metric) Package.new(Measured::Weight.new(100, :g), ‘box’, [10, 20, 30].map {|m| Length.new(m, :centimetres)}) Package.new(100.grams, [10, 20, 30].map(&:centimetres))
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/freight_kit/package.rb', line 34 def initialize(total_grams_or_ounces, dimensions, packaging_type, = {}) = .update() if .symbolize_keys! = raise ArgumentError, 'Package#new: packaging_type is required' unless packaging_type raise ArgumentError, 'Package#new: quantity is required' unless [:quantity] # For backward compatibility if dimensions.is_a?(Array) @dimensions = [dimensions].flatten.reject(&:nil?) else @dimensions = [dimensions.dig(:height), dimensions.dig(:width), dimensions.dig(:length)] @dimensions = [@dimensions].flatten.reject(&:nil?) end @description = [:description] @hazmat = [:hazmat] == true @nmfc = [:nmfc].presence imperial = ([:units] == :imperial) weight_imperial = dimensions_imperial = imperial if .include?(:units) weight_imperial = ([:weight_units] == :imperial) if .include?(:weight_units) dimensions_imperial = ([:dim_units] == :imperial) if .include?(:dim_units) @weight_unit_system = weight_imperial ? :imperial : :metric @dimensions_unit_system = dimensions_imperial ? :imperial : :metric @quantity = [:quantity] || 1 @total_weight = attribute_from_metric_or_imperial( total_grams_or_ounces, Measured::Weight, @weight_unit_system, :grams, :ounces, ) @each_weight = attribute_from_metric_or_imperial( total_grams_or_ounces / @quantity.to_f, 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 # Overriding ReactiveShipping's protected process_dimensions which sorts # them making it confusing for ReactiveFreight carrier API's that expect # the H x W x L order. Since H x W x L is nonstandard in the freight # industry ReactiveFreight introduces explicit functions for each @dimensions = @dimensions.map do |l| attribute_from_metric_or_imperial(l, Measured::Length, @dimensions_unit_system, :centimetres, :inches) end 2.downto(@dimensions.length) do |_n| @dimensions.unshift(@dimensions[0]) end end @value = Package.cents_from([:value]) @currency = [:currency] || ([:value].currency if [:value].respond_to?(:currency)) @cylinder = [:cylinder] || [:tube] ? true : false @gift = [:gift] ? true : false @oversized = [:oversized] ? true : false @unpackaged = [:unpackaged] ? true : false @packaging = Packaging.new(packaging_type) end |
Instance Attribute Details
#currency ⇒ Object (readonly)
Returns the value of attribute currency.
28 29 30 |
# File 'lib/freight_kit/package.rb', line 28 def currency @currency end |
#declared_freight_class ⇒ Object
135 136 137 |
# File 'lib/freight_kit/package.rb', line 135 def declared_freight_class @declared_freight_class || [:declared_freight_class] end |
#description ⇒ Object
Returns the value of attribute description.
27 28 29 |
# File 'lib/freight_kit/package.rb', line 27 def description @description end |
#hazmat ⇒ Object
Returns the value of attribute hazmat.
27 28 29 |
# File 'lib/freight_kit/package.rb', line 27 def hazmat @hazmat end |
#nmfc ⇒ Object
Returns the value of attribute nmfc.
27 28 29 |
# File 'lib/freight_kit/package.rb', line 27 def nmfc @nmfc end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
28 29 30 |
# File 'lib/freight_kit/package.rb', line 28 def end |
#packaging ⇒ Object (readonly)
Returns the value of attribute packaging.
28 29 30 |
# File 'lib/freight_kit/package.rb', line 28 def packaging @packaging end |
#quantity ⇒ Object
Returns the value of attribute quantity.
27 28 29 |
# File 'lib/freight_kit/package.rb', line 27 def quantity @quantity end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
28 29 30 |
# File 'lib/freight_kit/package.rb', line 28 def value @value end |
Class Method Details
.cents_from(money) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/freight_kit/package.rb', line 6 def cents_from(money) return if money.nil? if money.respond_to?(:cents) money.cents else case money when Float (money * 100).round when String money =~ /\./ ? (money.to_f * 100).round : money.to_i else money.to_i end end end |
Instance Method Details
#calculated_freight_class ⇒ Object
131 132 133 |
# File 'lib/freight_kit/package.rb', line 131 def calculated_freight_class sanitized_freight_class(density_to_freight_class(density)) end |
#centimetres(measurement = nil) ⇒ Object Also known as: cm
205 206 207 208 |
# File 'lib/freight_kit/package.rb', line 205 def centimetres(measurement = nil) @centimetres ||= @dimensions.map { |m| m.convert_to(:cm).value.to_f } measurement.nil? ? @centimetres : measure(measurement, @centimetres) end |
#cubic_ft(each_or_total) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/freight_kit/package.rb', line 108 def cubic_ft(each_or_total) q = case each_or_total when :each then 1 when :total then @quantity else raise ArgumentError, 'each_or_total must be one of :each, :total' end return unless inches[..2].all?(&:present?) cubic_ft = (inches[0] * inches[1] * inches[2]).to_f / 1728 cubic_ft *= q format('%0.2f', cubic_ft).to_f end |
#cylinder? ⇒ Boolean Also known as: tube?
155 156 157 |
# File 'lib/freight_kit/package.rb', line 155 def cylinder? @cylinder end |
#density ⇒ Object
124 125 126 127 128 129 |
# File 'lib/freight_kit/package.rb', line 124 def density return unless inches[..2].all?(&:present?) && pounds(:each) density = pounds(:each).to_f / cubic_ft(:each) format('%0.2f', density).to_f end |
#dim_weight ⇒ Object
211 212 213 214 215 |
# File 'lib/freight_kit/package.rb', line 211 def dim_weight return if inches(:length).blank? || inches(:width).blank? || inches(:height).blank? || pounds(:each).blank? @dim_weight ||= (inches(:length).ceil * inches(:width).ceil * inches(:height).ceil).to_f / 139 end |
#each_weight(options = {}) ⇒ Object
217 218 219 |
# File 'lib/freight_kit/package.rb', line 217 def each_weight( = {}) weight(@each_weight, ) end |
#freight_class ⇒ Object
139 140 141 |
# File 'lib/freight_kit/package.rb', line 139 def freight_class (declared_freight_class.presence || calculated_freight_class) end |
#gift? ⇒ Boolean
169 170 171 |
# File 'lib/freight_kit/package.rb', line 169 def gift? @gift end |
#grams(options = {}) ⇒ Object Also known as: g
182 183 184 |
# File 'lib/freight_kit/package.rb', line 182 def grams( = {}) weight().convert_to(:g).value.to_f end |
#hazmat? ⇒ Boolean
173 174 175 |
# File 'lib/freight_kit/package.rb', line 173 def hazmat? @hazmat end |
#height(unit) ⇒ Object
151 152 153 |
# File 'lib/freight_kit/package.rb', line 151 def height(unit) @dimensions[0].convert_to(unit).value.to_f end |
#inches(measurement = nil) ⇒ Object Also known as: in
199 200 201 202 |
# File 'lib/freight_kit/package.rb', line 199 def inches(measurement = nil) @inches ||= @dimensions.map { |m| m.convert_to(:in).value.to_f } measurement.nil? ? @inches : measure(measurement, @inches) end |
#kilograms(options = {}) ⇒ Object Also known as: kg, kgs
193 194 195 |
# File 'lib/freight_kit/package.rb', line 193 def kilograms( = {}) weight().convert_to(:kg).value.to_f end |
#length(unit) ⇒ Object
143 144 145 |
# File 'lib/freight_kit/package.rb', line 143 def length(unit) @dimensions[2].convert_to(unit).value.to_f end |
#ounces(options = {}) ⇒ Object Also known as: oz
177 178 179 |
# File 'lib/freight_kit/package.rb', line 177 def ounces( = {}) weight().convert_to(:oz).value.to_f end |
#oversized? ⇒ Boolean
159 160 161 |
# File 'lib/freight_kit/package.rb', line 159 def oversized? @oversized end |
#pounds(args) ⇒ Object Also known as: lb, lbs
187 188 189 |
# File 'lib/freight_kit/package.rb', line 187 def pounds(args) weight(*args).convert_to(:lb).value.to_f end |
#total_weight(options = {}) ⇒ Object
221 222 223 |
# File 'lib/freight_kit/package.rb', line 221 def total_weight( = {}) weight(@total_weight, ) end |
#unpackaged? ⇒ Boolean
163 164 165 |
# File 'lib/freight_kit/package.rb', line 163 def unpackaged? @unpackaged end |
#width(unit) ⇒ Object
147 148 149 |
# File 'lib/freight_kit/package.rb', line 147 def width(unit) @dimensions[1].convert_to(unit).value.to_f end |