Class: ActiveShipping::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/active_shipping/package.rb

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(Measured::Weight.new(100, :g), [10, 20, 30].map {|m| Length.new(m, :centimetres)}) Package.new(100.grams, [10, 20, 30].map(&:centimetres))



9
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
40
41
42
43
44
45
46
# File 'lib/active_shipping/package.rb', line 9

def initialize(grams_or_ounces, dimensions, options = {})
  options = @@default_options.update(options) if @@default_options
  options.symbolize_keys!
  @options = options

  @dimensions = [dimensions].flatten.reject(&:nil?)

  imperial = (options[:units] == :imperial)

  weight_imperial = dimensions_imperial = imperial if options.include?(:units)

  if options.include?(:weight_units)
    weight_imperial = (options[:weight_units] == :imperial)
  end

  if options.include?(:dim_units)
    dimensions_imperial = (options[:dim_units] == :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, 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
    process_dimensions
  end

  @value = Package.cents_from(options[:value])
  @currency = options[:currency] || (options[:value].currency if options[:value].respond_to?(:currency))
  @cylinder = (options[:cylinder] || options[:tube]) ? true : false
  @gift = options[:gift] ? true : false
  @oversized = options[:oversized] ? true : false
  @unpackaged = options[:unpackaged] ? true : false
end

Instance Attribute Details

#currencyObject (readonly)

Returns the value of attribute currency.



4
5
6
# File 'lib/active_shipping/package.rb', line 4

def currency
  @currency
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/active_shipping/package.rb', line 4

def options
  @options
end

#valueObject (readonly)

Returns the value of attribute value.



4
5
6
# File 'lib/active_shipping/package.rb', line 4

def value
  @value
end

Class Method Details

.cents_from(money) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/active_shipping/package.rb', line 114

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



93
94
95
96
# File 'lib/active_shipping/package.rb', line 93

def centimetres(measurement = nil)
  @centimetres ||= @dimensions.map { |m| m.convert_to(:cm).value.to_f }
  measurement.nil? ? @centimetres : measure(measurement, @centimetres)
end

#cylinder?Boolean Also known as: tube?

Returns:

  • (Boolean)


56
57
58
# File 'lib/active_shipping/package.rb', line 56

def cylinder?
  @cylinder
end

#gift?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/active_shipping/package.rb', line 61

def gift?
  @gift
end

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



70
71
72
# File 'lib/active_shipping/package.rb', line 70

def grams(options = {})
  weight(options).convert_to(:g).value.to_f
end

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



87
88
89
90
# File 'lib/active_shipping/package.rb', line 87

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



81
82
83
# File 'lib/active_shipping/package.rb', line 81

def kilograms(options = {})
  weight(options).convert_to(:kg).value.to_f
end

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



65
66
67
# File 'lib/active_shipping/package.rb', line 65

def ounces(options = {})
  weight(options).convert_to(:oz).value.to_f
end

#oversized?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/active_shipping/package.rb', line 52

def oversized?
  @oversized
end

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



75
76
77
# File 'lib/active_shipping/package.rb', line 75

def pounds(options = {})
  weight(options).convert_to(:lb).value.to_f
end

#unpackaged?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/active_shipping/package.rb', line 48

def unpackaged?
  @unpackaged
end

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



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/active_shipping/package.rb', line 99

def weight(options = {})
  case options[:type]
  when nil, :actual
    @weight
  when :volumetric, :dimensional
    @volumetric_weight ||= begin
      m = Measured::Weight.new((centimetres(:box_volume) / 6.0), :grams)
      @weight_unit_system == :imperial ? m.convert_to(:oz) : m
    end
  when :billable
    [weight, weight(:type => :volumetric)].max
  end
end