Class: UHaul::Dimensions

Inherits:
Object
  • Object
show all
Defined in:
lib/uhaul/dimensions.rb

Overview

The dimensions (width + depth + sqft) of a price.

Defined Under Namespace

Classes: ParseError

Constant Summary collapse

DIMENSIONS_REGEX =
/(?<width>[\d\.]+)'\s*x\s*(?<depth>[\d\.]+)'\s*x\s*(?<height>[\d\.]+)'/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(depth:, width:, height: DEFAULT_HEIGHT) ⇒ Dimensions

Returns a new instance of Dimensions.

Parameters:

  • depth (Float)
  • width (Float)
  • height (Float) (defaults to: DEFAULT_HEIGHT)


25
26
27
28
29
# File 'lib/uhaul/dimensions.rb', line 25

def initialize(depth:, width:, height: DEFAULT_HEIGHT)
  @depth = depth
  @width = width
  @height = height
end

Instance Attribute Details

#depthFloat

Returns:

  • (Float)


12
13
14
# File 'lib/uhaul/dimensions.rb', line 12

def depth
  @depth
end

#heightFloat

Returns:

  • (Float)


20
21
22
# File 'lib/uhaul/dimensions.rb', line 20

def height
  @height
end

#widthFloat

Returns:

  • (Float)


16
17
18
# File 'lib/uhaul/dimensions.rb', line 16

def width
  @width
end

Class Method Details

.parse(text:) ⇒ Dimensions

Parameters:

  • text (String)

Returns:

Raises:



59
60
61
62
63
64
65
66
67
# File 'lib/uhaul/dimensions.rb', line 59

def self.parse(text:)
  match = DIMENSIONS_REGEX.match(text)
  raise ParseError, "unknown length / width / height for #{text}" unless match

  width = Float(match[:width])
  depth = Float(match[:depth])
  height = Float(match[:height])
  new(depth:, width:, height:)
end

Instance Method Details

#cuftInteger

Returns:

  • (Integer)


47
48
49
# File 'lib/uhaul/dimensions.rb', line 47

def cuft
  Integer(@width * @depth * @height)
end

#inspectString

Returns:

  • (String)


32
33
34
35
36
37
38
39
# File 'lib/uhaul/dimensions.rb', line 32

def inspect
  props = [
    "depth=#{@depth.inspect}",
    "width=#{@width.inspect}",
    "height=#{@height.inspect}"
  ]
  "#<#{self.class.name} #{props.join(' ')}>"
end

#sqftInteger

Returns:

  • (Integer)


42
43
44
# File 'lib/uhaul/dimensions.rb', line 42

def sqft
  Integer(@width * @depth)
end

#textString

Returns e.g. “10’ × 10’ (100 sqft)”.

Returns:

  • (String)

    e.g. “10’ × 10’ (100 sqft)”



52
53
54
# File 'lib/uhaul/dimensions.rb', line 52

def text
  "#{format('%g', @width)}' × #{format('%g', @depth)}' (#{sqft} sqft)"
end