Class: Physical::Cuboid

Inherits:
Object
  • Object
show all
Defined in:
lib/physical/cuboid.rb

Direct Known Subclasses

Box, Item

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, dimensions: [], weight: Measured::Weight(0, :g), properties: {}) ⇒ Cuboid

Returns a new instance of Cuboid.



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

def initialize(id: nil, dimensions: [], weight: Measured::Weight(0, :g), properties: {})
  @id = id || SecureRandom.uuid
  @weight = Types::Weight[weight]
  @dimensions = []
  @dimensions = fill_dimensions(Types::Dimensions[dimensions])
  @length, @width, @height = *@dimensions
  @properties = properties
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object (private)



36
37
38
39
40
41
42
43
44
# File 'lib/physical/cuboid.rb', line 36

def method_missing(method)
  symbolized_properties = properties.symbolize_keys
  method_name = normalize_method_name(method)
  if symbolized_properties.key?(method_name)
    symbolized_properties[method_name]
  else
    super
  end
end

Instance Attribute Details

#dimensionsObject (readonly)

Returns the value of attribute dimensions.



7
8
9
# File 'lib/physical/cuboid.rb', line 7

def dimensions
  @dimensions
end

#heightObject (readonly)

Returns the value of attribute height.



7
8
9
# File 'lib/physical/cuboid.rb', line 7

def height
  @height
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/physical/cuboid.rb', line 7

def id
  @id
end

#lengthObject (readonly)

Returns the value of attribute length.



7
8
9
# File 'lib/physical/cuboid.rb', line 7

def length
  @length
end

#propertiesObject (readonly)

Returns the value of attribute properties.



7
8
9
# File 'lib/physical/cuboid.rb', line 7

def properties
  @properties
end

#weightObject (readonly)

Returns the value of attribute weight.



7
8
9
# File 'lib/physical/cuboid.rb', line 7

def weight
  @weight
end

#widthObject (readonly)

Returns the value of attribute width.



7
8
9
# File 'lib/physical/cuboid.rb', line 7

def width
  @width
end

Instance Method Details

#==(other) ⇒ Object



28
29
30
# File 'lib/physical/cuboid.rb', line 28

def ==(other)
  id == other.id
end

#densityObject



22
23
24
25
26
# File 'lib/physical/cuboid.rb', line 22

def density
  return Measured::Density(Float::INFINITY, :g_ml) if volume.value.zero?
  return Measured::Density(0.0, :g_ml) if volume.value.infinite?
  Measured::Density(weight.convert_to(:g).value / volume.convert_to(:ml).value, :g_ml)
end

#volumeObject



18
19
20
# File 'lib/physical/cuboid.rb', line 18

def volume
  Measured::Volume(dimensions.map { |d| d.convert_to(:cm).value }.reduce(1, &:*), :ml)
end