Class: NSAStorage::Dimensions

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

Overview

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

Constant Summary collapse

DEFAULT_WIDTH =

feet

5.0
DEFAULT_DEPTH =

feet

5.0
DEFAULT_HEIGHT =

feet

8.0
DIMENSIONS_REGEX =
/(?<width>[\d\.]+) x (?<depth>[\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)


27
28
29
30
31
# File 'lib/nsastorage/dimensions.rb', line 27

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

Instance Attribute Details

#depthFloat

Returns:

  • (Float)


14
15
16
# File 'lib/nsastorage/dimensions.rb', line 14

def depth
  @depth
end

#heightFloat

Returns:

  • (Float)


22
23
24
# File 'lib/nsastorage/dimensions.rb', line 22

def height
  @height
end

#widthFloat

Returns:

  • (Float)


18
19
20
# File 'lib/nsastorage/dimensions.rb', line 18

def width
  @width
end

Class Method Details

.parse(element:) ⇒ Dimensions

Parameters:

  • element (Nokogiri::XML::Element)

Returns:



66
67
68
69
70
71
72
73
# File 'lib/nsastorage/dimensions.rb', line 66

def self.parse(element:)
  text = element.at_css('.unit-select-item-detail').text
  match = DIMENSIONS_REGEX.match(text)

  width = match ? Float(match[:width]) : DEFAULT_WIDTH
  depth = match ? Float(match[:depth]) : DEFAULT_DEPTH
  new(depth:, width:, height: DEFAULT_HEIGHT)
end

Instance Method Details

#cuftInteger

Returns:

  • (Integer)


54
55
56
# File 'lib/nsastorage/dimensions.rb', line 54

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

#idString

Returns e.g. “5×5”.

Returns:

  • (String)

    e.g. “5×5”



44
45
46
# File 'lib/nsastorage/dimensions.rb', line 44

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

#inspectString

Returns:

  • (String)


34
35
36
37
38
39
40
41
# File 'lib/nsastorage/dimensions.rb', line 34

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

#sqftInteger

Returns:

  • (Integer)


49
50
51
# File 'lib/nsastorage/dimensions.rb', line 49

def sqft
  Integer(@width * @depth)
end

#textString

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

Returns:

  • (String)

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



59
60
61
# File 'lib/nsastorage/dimensions.rb', line 59

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