Class: ActiveMerchant::Shipping::Package

Inherits:
Object
  • Object
show all
Includes:
Quantified
Defined in:
lib/active_shipping/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(Mass.new(100, :grams), [10, 20, 30].map {|m| Length.new(m, :centimetres)}) Package.new(100.grams, [10, 20, 30].map(&:centimetres))



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/active_shipping/shipping/package.rb', line 87

def initialize(grams_or_ounces, dimensions, options = {})
  options = @@default_options.update(options) if @@default_options
  options.symbolize_keys!
  @options = options
  
  @dimensions = [dimensions].flatten.reject {|d| d.nil?}

  
  imperial = (options[:units] == :imperial) ||
      ([grams_or_ounces, *dimensions].all? {|m| m.respond_to?(:unit) && m.unit.to_sym == :imperial})
  
  weight_imperial = dimensions_imperial = imperial if options.include?(:units)

  if options.include?(:weight_units)
    weight_imperial = (options[:weight_units] == :imperial) ||
        (grams_or_ounces.respond_to?(:unit) && m.unit.to_sym == :imperial)
  end

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



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

def currency
  @currency
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#valueObject (readonly)

Returns the value of attribute value.



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

def value
  @value
end

Class Method Details

.cents_from(money) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/active_shipping/shipping/package.rb', line 193

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



172
173
174
175
# File 'lib/active_shipping/shipping/package.rb', line 172

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?

Returns:

  • (Boolean)


137
138
139
# File 'lib/active_shipping/shipping/package.rb', line 137

def cylinder?
  @cylinder
end

#gift?Boolean

Returns:

  • (Boolean)


142
# File 'lib/active_shipping/shipping/package.rb', line 142

def gift?; @gift end

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



149
150
151
# File 'lib/active_shipping/shipping/package.rb', line 149

def grams(options={})
  weight(options).in_grams.amount
end

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



166
167
168
169
# File 'lib/active_shipping/shipping/package.rb', line 166

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



160
161
162
# File 'lib/active_shipping/shipping/package.rb', line 160

def kilograms(options={})
  weight(options).in_kilograms.amount
end

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



144
145
146
# File 'lib/active_shipping/shipping/package.rb', line 144

def ounces(options={})
  weight(options).in_ounces.amount
end

#oversized?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/active_shipping/shipping/package.rb', line 133

def oversized?
  @oversized
end

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



154
155
156
# File 'lib/active_shipping/shipping/package.rb', line 154

def pounds(options={})
  weight(options).in_pounds.amount
end

#unpackaged?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/active_shipping/shipping/package.rb', line 129

def unpackaged?
  @unpackaged
end

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



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/active_shipping/shipping/package.rb', line 178

def weight(options = {})
  case options[: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