Class: Physical::Structure
- Inherits:
-
Object
- Object
- Physical::Structure
- Extended by:
- Forwardable
- Defined in:
- lib/physical/structure.rb
Instance Attribute Summary collapse
-
#container ⇒ Object
readonly
Returns the value of attribute container.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#packages ⇒ Object
readonly
Returns the value of attribute packages.
-
#packages_weight ⇒ Object
readonly
Returns the value of attribute packages_weight.
-
#used_volume ⇒ Object
readonly
Returns the value of attribute used_volume.
Instance Method Summary collapse
- #<<(other) ⇒ Object (also: #add)
- #>>(other) ⇒ Object (also: #delete)
- #density ⇒ Object
-
#initialize(id: nil, container: nil, packages: [], dimensions: nil, weight: nil, properties: {}) ⇒ Structure
constructor
A new instance of Structure.
-
#packages_value ⇒ Object
Cost is optional.
- #remaining_volume ⇒ Object
- #weight ⇒ Object
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
#container ⇒ Object (readonly)
Returns the value of attribute container.
6 7 8 |
# File 'lib/physical/structure.rb', line 6 def container @container end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
6 7 8 |
# File 'lib/physical/structure.rb', line 6 def id @id end |
#packages ⇒ Object (readonly)
Returns the value of attribute packages.
6 7 8 |
# File 'lib/physical/structure.rb', line 6 def packages @packages end |
#packages_weight ⇒ Object (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_volume ⇒ Object (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 |
#density ⇒ Object
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_value ⇒ Object
Cost is optional. We will only return an aggregate if all packages have items value defined. Otherwise we will return nil.
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_volume ⇒ Object
45 46 47 |
# File 'lib/physical/structure.rb', line 45 def remaining_volume container.inner_volume - used_volume end |
#weight ⇒ Object
33 34 35 |
# File 'lib/physical/structure.rb', line 33 def weight container.weight + packages_weight end |