Class: Pombo::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/pombo/package.rb,
lib/pombo/package/format.rb

Overview

This is the composite package of items that will be sent

Examples:

package = Pombo::Package.new ({
  destination_zip_code: '29999000',
  origin_zip_code: '28888000',
  services: "40010",
  in_hand: false,
  delivery_notice: false
})
# => <Pombo::Package:0x007fcfd32080f0 @items=[], @length=0, @height=0, @width=0, @declared_value=0, @destination_zip_code="29999000", @origin_zip_code="28888000">

package.add_item weight: 5, length: 4, height: 3, width: 5, diameter: 0
package.add_item weight: 4, length: 10, height: 5, width: 5, diameter: 5, format: Pombo::Package::Format.find(:roll).code
package.in_hand? # => false
package.delivery_notice? # => false
package.weight # => 9
package.diameter # => 0
package.format # => 1
package.volume # => 158.17000000000002
package.single_item? # => false

Defined Under Namespace

Modules: Format Classes: Item

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Package

Returns a new instance of Package


27
28
29
30
31
# File 'lib/pombo/package.rb', line 27

def initialize(**args)
  { items: [], length: 0, height: 0, width: 0 }.each { |key, value| instance_variable_set("@#{ key }", value) }
  args = { declared_value: 0 }.merge(args)
  args.each { |key, value| __send__("#{ key }=", value) }
end

Instance Attribute Details

#declared_valueObject

Returns the value of attribute declared_value


24
25
26
# File 'lib/pombo/package.rb', line 24

def declared_value
  @declared_value
end

#destination_zip_codeObject

Returns the value of attribute destination_zip_code


24
25
26
# File 'lib/pombo/package.rb', line 24

def destination_zip_code
  @destination_zip_code
end

#heightObject (readonly)

returns the height of the package or the format if `min_package` is true


59
60
61
# File 'lib/pombo/package.rb', line 59

def height
  @height
end

#itemsObject (readonly)

Returns the value of attribute items


25
26
27
# File 'lib/pombo/package.rb', line 25

def items
  @items
end

#lengthObject (readonly)

returns the length of the package or the format if `min_package` is true


59
60
61
# File 'lib/pombo/package.rb', line 59

def length
  @length
end

#origin_zip_codeObject

Returns the value of attribute origin_zip_code


24
25
26
# File 'lib/pombo/package.rb', line 24

def origin_zip_code
  @origin_zip_code
end

#servicesObject

Returns the value of attribute services


25
26
27
# File 'lib/pombo/package.rb', line 25

def services
  @services
end

#widthObject (readonly)

returns the width of the package or the format if `min_package` is true


59
60
61
# File 'lib/pombo/package.rb', line 59

def width
  @width
end

Instance Method Details

#add_item(item = nil, **args) ⇒ Pombo::Package::Item

It allows you to add an item to the package

Examples:

item = Pombo::Package::Item.new weight: 5, length: 4, height: 3, width: 5, diameter: 0
package.add_item item

# => or

package.add_item weight: 5, length: 4, height: 3, width: 5, diameter: 0

Returns:


85
86
87
88
89
90
91
92
93
94
95
# File 'lib/pombo/package.rb', line 85

def add_item(item = nil, **args)
  item = if item.kind_of?(Pombo::Package::Item)
           item
         else
           Pombo::Package::Item.new(args)
         end

  @items << item
  update_measures
  item
end

#delivery_notice?Object

Informs you are contracted delivery notice service


37
38
39
# File 'lib/pombo/package.rb', line 37

in_hand delivery_notice].each do |method|
  define_method("#{ method }?"){ instance_variable_get "@#{ method }" }
end

#diameterFloat

Returns the total diameter of the items

Returns:

  • (Float)

    the total diameter of the items


103
104
105
106
# File 'lib/pombo/package.rb', line 103

def diameter
  return @items.first.diameter if single_item? && format == Pombo::Package::Format.find(:roll).code
  0
end

#formatFixnum

Returns the code of the packet format For packages with more than one item format will be 1 (:box)

Returns:

  • (Fixnum)

    the code of the packet format For packages with more than one item format will be 1 (:box)


110
111
112
113
# File 'lib/pombo/package.rb', line 110

def format
  return @items.first.format if single_item?
  Pombo::Package::Format.find(:box).code
end

#in_hand?Object

Informs you are contracted delivery in hand service


37
38
39
# File 'lib/pombo/package.rb', line 37

in_hand delivery_notice].each do |method|
  define_method("#{ method }?"){ instance_variable_get "@#{ method }" }
end

#single_item?Boolean

Returns tells if the package contains only one item

Returns:

  • (Boolean)

    tells if the package contains only one item


121
122
123
# File 'lib/pombo/package.rb', line 121

def single_item?
  @items.size == 1 && @items.first.quantity == 1
end

#volumeFloat

Returns the total volume of the items

Returns:

  • (Float)

    the total volume of the items


116
117
118
# File 'lib/pombo/package.rb', line 116

def volume
  @items.inject(0) { |sum, item| sum += item.volume }
end

#weightFloat

Returns the total weight of the items

Returns:

  • (Float)

    the total weight of the items


98
99
100
# File 'lib/pombo/package.rb', line 98

def weight
  @items.inject(0) { |sum, item| sum += item.weight }
end