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 107 108 |
# File 'lib/freight_kit/package.rb', line 34 def initialize(total_grams_or_ounces, dimensions, packaging_type, = {}) = @@default_options.update() if @@default_options .symbolize_keys! @options = raise ArgumentError, 'Package#new: packaging_type is required' unless packaging_type raise ArgumentError, 'Package#new: quantity is required' unless [:quantity] @dimensions = if dimensions.is_a?(Hash) [ dimensions[:height].presence, dimensions[:width].presence, dimensions[:length].presence, ] else dimensions 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
137 138 139 |
# File 'lib/freight_kit/package.rb', line 137 def declared_freight_class @declared_freight_class || @options[: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 @options 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
133 134 135 |
# File 'lib/freight_kit/package.rb', line 133 def calculated_freight_class sanitized_freight_class(density_to_freight_class(density)) end |
#centimetres(measurement = nil) ⇒ Object Also known as: cm
207 208 209 210 |
# File 'lib/freight_kit/package.rb', line 207 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
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/freight_kit/package.rb', line 110 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?
157 158 159 |
# File 'lib/freight_kit/package.rb', line 157 def cylinder? @cylinder end |
#density ⇒ Object
126 127 128 129 130 131 |
# File 'lib/freight_kit/package.rb', line 126 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
213 214 215 216 217 |
# File 'lib/freight_kit/package.rb', line 213 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
219 220 221 |
# File 'lib/freight_kit/package.rb', line 219 def each_weight( = {}) weight(@each_weight, ) end |
#freight_class ⇒ Object
141 142 143 |
# File 'lib/freight_kit/package.rb', line 141 def freight_class declared_freight_class.presence || calculated_freight_class end |
#gift? ⇒ Boolean
171 172 173 |
# File 'lib/freight_kit/package.rb', line 171 def gift? @gift end |
#grams(options = {}) ⇒ Object Also known as: g
184 185 186 |
# File 'lib/freight_kit/package.rb', line 184 def grams( = {}) weight().convert_to(:g).value.to_f end |
#hazmat? ⇒ Boolean
175 176 177 |
# File 'lib/freight_kit/package.rb', line 175 def hazmat? @hazmat end |
#height(unit) ⇒ Object
153 154 155 |
# File 'lib/freight_kit/package.rb', line 153 def height(unit) @dimensions[0].convert_to(unit).value.to_f end |
#inches(measurement = nil) ⇒ Object Also known as: in
201 202 203 204 |
# File 'lib/freight_kit/package.rb', line 201 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
195 196 197 |
# File 'lib/freight_kit/package.rb', line 195 def kilograms( = {}) weight().convert_to(:kg).value.to_f end |
#length(unit) ⇒ Object
145 146 147 |
# File 'lib/freight_kit/package.rb', line 145 def length(unit) @dimensions[2].convert_to(unit).value.to_f end |
#ounces(options = {}) ⇒ Object Also known as: oz
179 180 181 |
# File 'lib/freight_kit/package.rb', line 179 def ounces( = {}) weight().convert_to(:oz).value.to_f end |
#oversized? ⇒ Boolean
161 162 163 |
# File 'lib/freight_kit/package.rb', line 161 def oversized? @oversized end |
#pounds(args) ⇒ Object Also known as: lb, lbs
189 190 191 |
# File 'lib/freight_kit/package.rb', line 189 def pounds(args) weight(*args).convert_to(:lb).value.to_f end |
#total_weight(options = {}) ⇒ Object
223 224 225 |
# File 'lib/freight_kit/package.rb', line 223 def total_weight( = {}) weight(@total_weight, ) end |
#unpackaged? ⇒ Boolean
165 166 167 |
# File 'lib/freight_kit/package.rb', line 165 def unpackaged? @unpackaged end |
#width(unit) ⇒ Object
149 150 151 |
# File 'lib/freight_kit/package.rb', line 149 def width(unit) @dimensions[1].convert_to(unit).value.to_f end |