Class: Physical::Package

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/physical/package.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, container: nil, items: [], void_fill_density: Measured::Density(0, :g_ml), dimensions: nil, weight: nil, description: nil, properties: {}) ⇒ Package

Returns a new instance of Package.



8
9
10
11
12
13
14
15
16
17
# File 'lib/physical/package.rb', line 8

def initialize(id: nil, container: nil, items: [], void_fill_density: Measured::Density(0, :g_ml), dimensions: nil, weight: nil, description: nil, properties: {})
  @id = id || SecureRandom.uuid
  @void_fill_density = Types::Density[void_fill_density]
  @container = container || Physical::Box.new(dimensions: dimensions || [], weight: weight || Measured::Weight(0, :g), properties: properties)
  @description = description

  @items = Set[*items]
  @items_weight = @items.map(&:weight).reduce(Measured::Weight(0, :g), &:+)
  @used_volume = @items.map(&:volume).reduce(Measured::Volume(0, :ml), &:+)
end

Instance Attribute Details

#containerObject (readonly)

Returns the value of attribute container.



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

def container
  @container
end

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#itemsObject (readonly)

Returns the value of attribute items.



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

def items
  @items
end

#items_weightObject (readonly)

Returns the value of attribute items_weight.



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

def items_weight
  @items_weight
end

#used_volumeObject (readonly)

Returns the value of attribute used_volume.



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

def used_volume
  @used_volume
end

#void_fill_densityObject (readonly)

Returns the value of attribute void_fill_density.



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

def void_fill_density
  @void_fill_density
end

Instance Method Details

#<<(other) ⇒ Object Also known as: add



21
22
23
24
25
# File 'lib/physical/package.rb', line 21

def <<(other)
  @items.add(other)
  @items_weight += other.weight
  @used_volume += other.volume
end

#>>(other) ⇒ Object Also known as: delete



28
29
30
31
32
# File 'lib/physical/package.rb', line 28

def >>(other)
  @items.delete(other)
  @items_weight -= other.weight
  @used_volume -= other.volume
end

#densityObject



57
58
59
60
61
62
# File 'lib/physical/package.rb', line 57

def density
  return Measured::Density(Float::INFINITY, :g_ml) if container.volume.value.zero?
  return Measured::Density(0.0, :g_ml) if container.volume.value.infinite?

  Measured::Density(weight.convert_to(:g).value / container.volume.convert_to(:ml).value, :g_ml)
end

#items_valueObject

Cost is optional. We will only return an aggregate if all items have cost defined. Otherwise we will return nil.

Returns:

  • Money



42
43
44
45
# File 'lib/physical/package.rb', line 42

def items_value
  items_cost = items.map(&:cost)
  items_cost.reduce(&:+) if items_cost.compact.size == items_cost.size
end

#remaining_volumeObject



53
54
55
# File 'lib/physical/package.rb', line 53

def remaining_volume
  container.inner_volume - used_volume
end

#void_fill_weightObject



47
48
49
50
51
# File 'lib/physical/package.rb', line 47

def void_fill_weight
  return Measured::Weight(0, :g) if container.volume.value.infinite?

  Measured::Weight(void_fill_density.convert_to(:g_ml).value * remaining_volume.convert_to(:ml).value, :g)
end

#weightObject



35
36
37
# File 'lib/physical/package.rb', line 35

def weight
  container.weight + items_weight + void_fill_weight
end