Class: Physical::Structure

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, container: nil, packages: [], dimensions: nil, weight: nil, properties: {}) ⇒ Structure

Returns a new instance of Structure.



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

def initialize(id: nil, container: nil, packages: [], dimensions: nil, weight: nil, properties: {})
  @id = id || SecureRandom.uuid
  @container = container || Physical::Pallet.new(dimensions: dimensions || [], weight: weight || Measured::Weight(0, :g), properties: properties)

  @packages = Set[*packages]
  @packages_weight = @packages.map(&:weight).reduce(Measured::Weight(0, :g), &:+)
  @used_volume = @packages.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/structure.rb', line 6

def container
  @container
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#packagesObject (readonly)

Returns the value of attribute packages.



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

def packages
  @packages
end

#packages_weightObject (readonly)

Returns the value of attribute packages_weight.



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

def packages_weight
  @packages_weight
end

#used_volumeObject (readonly)

Returns the value of attribute used_volume.



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

def used_volume
  @used_volume
end

Instance Method Details

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



19
20
21
22
23
# File 'lib/physical/structure.rb', line 19

def <<(other)
  @packages.add(other)
  @packages_weight += other.weight
  @used_volume += other.volume
end

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



26
27
28
29
30
# File 'lib/physical/structure.rb', line 26

def >>(other)
  @packages.delete(other)
  @packages_weight -= other.weight
  @used_volume -= other.volume
end

#densityObject



49
50
51
52
53
54
# File 'lib/physical/structure.rb', line 49

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

#packages_valueObject

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

Returns:

  • Money



40
41
42
43
# File 'lib/physical/structure.rb', line 40

def packages_value
  packages_cost = packages.map(&:items_value)
  packages_cost.reduce(&:+) if packages_cost.compact.size == packages_cost.size
end

#remaining_volumeObject



45
46
47
# File 'lib/physical/structure.rb', line 45

def remaining_volume
  container.inner_volume - used_volume
end

#weightObject



33
34
35
# File 'lib/physical/structure.rb', line 33

def weight
  container.weight + packages_weight
end