Class: Shipping::Package
- Inherits:
-
Object
- Object
- Shipping::Package
- Defined in:
- app/services/shipping/package.rb
Instance Attribute Summary collapse
-
#container_code ⇒ Object
readonly
Returns the value of attribute container_code.
-
#currency ⇒ Object
readonly
Returns the value of attribute currency.
-
#flat_rate_package_type ⇒ Object
readonly
Returns the value of attribute flat_rate_package_type.
-
#nmfc_code ⇒ Object
readonly
Returns the value of attribute nmfc_code.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#shipment_id ⇒ Object
readonly
Returns the value of attribute shipment_id.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #centimetres(measurement = nil) ⇒ Object (also: #cm)
- #crate? ⇒ Boolean
- #cylinder? ⇒ Boolean (also: #tube?)
- #grams(options = {}) ⇒ Object (also: #g)
- #inches(measurement = nil) ⇒ Object (also: #in)
-
#initialize(grams_or_ounces, dimensions, options = {}) ⇒ Package
constructor
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)).
- #kilograms(options = {}) ⇒ Object (also: #kg, #kgs)
- #ounces(options = {}) ⇒ Object (also: #oz)
- #pallet? ⇒ Boolean
- #pounds(options = {}) ⇒ Object (also: #lb, #lbs)
- #weight(options = {}) ⇒ Object (also: #mass)
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))
10 11 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 |
# File 'app/services/shipping/package.rb', line 10 def initialize(grams_or_ounces, dimensions, = {}) = self.class..update() if self.class. .symbolize_keys! @options = @dimensions = [dimensions].flatten.reject {|d| d.nil?} imperial = ([: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([:value]) @currency = [:currency] || ([:value].currency if [:value].respond_to?(:currency)) @cylinder = ([:cylinder] || [:tube]) ? true : false @pallet = ([:pallet]) ? true : false @crate = ([:crate]) ? true : false @container_code = [:container_code] @shipment_id = [:shipment_id] @nmfc_code = [:nmfc_code] @flat_rate_package_type = [:flat_rate_package_type] end |
Instance Attribute Details
#container_code ⇒ Object (readonly)
Returns the value of attribute container_code.
5 6 7 |
# File 'app/services/shipping/package.rb', line 5 def container_code @container_code end |
#currency ⇒ Object (readonly)
Returns the value of attribute currency.
5 6 7 |
# File 'app/services/shipping/package.rb', line 5 def currency @currency end |
#flat_rate_package_type ⇒ Object (readonly)
Returns the value of attribute flat_rate_package_type.
5 6 7 |
# File 'app/services/shipping/package.rb', line 5 def flat_rate_package_type @flat_rate_package_type end |
#nmfc_code ⇒ Object (readonly)
Returns the value of attribute nmfc_code.
5 6 7 |
# File 'app/services/shipping/package.rb', line 5 def nmfc_code @nmfc_code end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'app/services/shipping/package.rb', line 5 def @options end |
#shipment_id ⇒ Object (readonly)
Returns the value of attribute shipment_id.
5 6 7 |
# File 'app/services/shipping/package.rb', line 5 def shipment_id @shipment_id end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
5 6 7 |
# File 'app/services/shipping/package.rb', line 5 def value @value end |
Class Method Details
.dollars_from(money) ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'app/services/shipping/package.rb', line 103 def self.dollars_from(money) return nil if money.nil? if money.respond_to?(:dollars) return money.dollars else money.to_f.round(2) end end |
Instance Method Details
#centimetres(measurement = nil) ⇒ Object Also known as: cm
82 83 84 85 |
# File 'app/services/shipping/package.rb', line 82 def centimetres(measurement=nil) @centimetres ||= @dimensions.map {|m| m.in_centimetres.amount } measurement.nil? ? @centimetres : measure(measurement, @centimetres) end |
#crate? ⇒ Boolean
45 46 47 |
# File 'app/services/shipping/package.rb', line 45 def crate? @crate end |
#cylinder? ⇒ Boolean Also known as: tube?
49 50 51 |
# File 'app/services/shipping/package.rb', line 49 def cylinder? @cylinder end |
#grams(options = {}) ⇒ Object Also known as: g
59 60 61 |
# File 'app/services/shipping/package.rb', line 59 def grams(={}) weight().in_grams.amount end |
#inches(measurement = nil) ⇒ Object Also known as: in
76 77 78 79 |
# File 'app/services/shipping/package.rb', line 76 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
70 71 72 |
# File 'app/services/shipping/package.rb', line 70 def kilograms(={}) weight().in_kilograms.amount end |
#ounces(options = {}) ⇒ Object Also known as: oz
54 55 56 |
# File 'app/services/shipping/package.rb', line 54 def ounces(={}) weight().in_ounces.amount end |
#pallet? ⇒ Boolean
41 42 43 |
# File 'app/services/shipping/package.rb', line 41 def pallet? @pallet end |
#pounds(options = {}) ⇒ Object Also known as: lb, lbs
64 65 66 |
# File 'app/services/shipping/package.rb', line 64 def pounds(={}) weight().in_pounds.amount end |
#weight(options = {}) ⇒ Object Also known as: mass
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'app/services/shipping/package.rb', line 88 def weight( = {}) case [: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 |