Class: Omniship::Package

Inherits:
Object show all
Includes:
Quantified
Defined in:
lib/omniship/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))



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
# File 'lib/omniship/package.rb', line 13

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})
  
  @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.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
end

Instance Attribute Details

#currencyObject (readonly)

Returns the value of attribute currency.



6
7
8
# File 'lib/omniship/package.rb', line 6

def currency
  @currency
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/omniship/package.rb', line 6

def options
  @options
end

#valueObject (readonly)

Returns the value of attribute value.



6
7
8
# File 'lib/omniship/package.rb', line 6

def value
  @value
end

Class Method Details

.cents_from(money) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/omniship/package.rb', line 95

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



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

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)


39
40
41
# File 'lib/omniship/package.rb', line 39

def cylinder?
  @cylinder
end

#gift?Boolean

Returns:

  • (Boolean)


44
# File 'lib/omniship/package.rb', line 44

def gift?; @gift end

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



51
52
53
# File 'lib/omniship/package.rb', line 51

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

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



68
69
70
71
# File 'lib/omniship/package.rb', line 68

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



62
63
64
# File 'lib/omniship/package.rb', line 62

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

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



46
47
48
# File 'lib/omniship/package.rb', line 46

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

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



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

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

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



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/omniship/package.rb', line 80

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)
      @unit_system == :imperial ? m.in_pounds : m
    end
  when :billable
    [ weight, weight(:type => :volumetric) ].max
  end
end