Class: ActiveShipping::Package
- Inherits:
-
Object
- Object
- ActiveShipping::Package
- Includes:
- Quantified
- Defined in:
- lib/active_shipping/package.rb
Instance Attribute Summary collapse
-
#currency ⇒ Object
readonly
Returns the value of attribute currency.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #centimetres(measurement = nil) ⇒ Object (also: #cm)
- #cylinder? ⇒ Boolean (also: #tube?)
- #gift? ⇒ Boolean
- #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)
- #oversized? ⇒ Boolean
- #pounds(options = {}) ⇒ Object (also: #lb, #lbs)
- #unpackaged? ⇒ Boolean
- #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))
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 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/active_shipping/package.rb', line 11 def initialize(grams_or_ounces, dimensions, = {}) = @@default_options.update() if @@default_options .symbolize_keys! @options = @dimensions = [dimensions].flatten.reject(&:nil?) imperial = ([:units] == :imperial) || ([grams_or_ounces, *dimensions].all? { |m| m.respond_to?(:unit) && m.unit.to_sym == :imperial }) weight_imperial = dimensions_imperial = imperial if .include?(:units) if .include?(:weight_units) weight_imperial = ([:weight_units] == :imperial) || (grams_or_ounces.respond_to?(:unit) && m.unit.to_sym == :imperial) end if .include?(:dim_units) dimensions_imperial = ([:dim_units] == :imperial) || (dimensions && dimensions.all? { |m| m.respond_to?(:unit) && m.unit.to_sym == :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) if @dimensions.blank? @dimensions = [Length.new(0, (dimensions_imperial ? :inches : :centimetres))] * 3 else process_dimensions 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 end |
Instance Attribute Details
#currency ⇒ Object (readonly)
Returns the value of attribute currency.
6 7 8 |
# File 'lib/active_shipping/package.rb', line 6 def currency @currency end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/active_shipping/package.rb', line 6 def @options end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
6 7 8 |
# File 'lib/active_shipping/package.rb', line 6 def value @value end |
Class Method Details
.cents_from(money) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/active_shipping/package.rb', line 118 def self.cents_from(money) return nil if money.nil? if money.respond_to?(:cents) return 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
#centimetres(measurement = nil) ⇒ Object Also known as: cm
97 98 99 100 |
# File 'lib/active_shipping/package.rb', line 97 def centimetres(measurement = nil) @centimetres ||= @dimensions.map { |m| m.in_centimetres.amount } measurement.nil? ? @centimetres : measure(measurement, @centimetres) end |
#cylinder? ⇒ Boolean Also known as: tube?
60 61 62 |
# File 'lib/active_shipping/package.rb', line 60 def cylinder? @cylinder end |
#gift? ⇒ Boolean
65 66 67 |
# File 'lib/active_shipping/package.rb', line 65 def gift? @gift end |
#grams(options = {}) ⇒ Object Also known as: g
74 75 76 |
# File 'lib/active_shipping/package.rb', line 74 def grams( = {}) weight().in_grams.amount end |
#inches(measurement = nil) ⇒ Object Also known as: in
91 92 93 94 |
# File 'lib/active_shipping/package.rb', line 91 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
85 86 87 |
# File 'lib/active_shipping/package.rb', line 85 def kilograms( = {}) weight().in_kilograms.amount end |
#ounces(options = {}) ⇒ Object Also known as: oz
69 70 71 |
# File 'lib/active_shipping/package.rb', line 69 def ounces( = {}) weight().in_ounces.amount end |
#oversized? ⇒ Boolean
56 57 58 |
# File 'lib/active_shipping/package.rb', line 56 def oversized? @oversized end |
#pounds(options = {}) ⇒ Object Also known as: lb, lbs
79 80 81 |
# File 'lib/active_shipping/package.rb', line 79 def pounds( = {}) weight().in_pounds.amount end |
#unpackaged? ⇒ Boolean
52 53 54 |
# File 'lib/active_shipping/package.rb', line 52 def unpackaged? @unpackaged end |
#weight(options = {}) ⇒ Object Also known as: mass
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/active_shipping/package.rb', line 103 def weight( = {}) case [: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 end when :billable [weight, weight(:type => :volumetric)].max end end |