Class: Boxbot::Compute::TabCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/boxbot/compute/tab_calculator.rb

Overview

Computes the actual tab with for a given dimension index or name.

Examples:

compute number of tabs for the vertical dimension

dim = Boxbot::Dimension.new(width: 10, ... )
Boxbot::Compute::TabCalculator.new(dim)['height']
# => 9

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dimensions) ⇒ TabCalculator



32
33
34
# File 'lib/boxbot/compute/tab_calculator.rb', line 32

def initialize(dimensions)
  self.dimensions = dimensions
end

Instance Attribute Details

#dimensionsBoxbot::Dimensions

instance



29
30
31
# File 'lib/boxbot/compute/tab_calculator.rb', line 29

def dimensions
  @dimensions
end

Instance Method Details

#[](dimension_identifier) ⇒ Integer

Computes the tab count for a given dimension, and returns an integer result.

Raises:

  • ArgumentError if argument is not a String, Symbol, or Integer.



63
64
65
66
67
68
69
# File 'lib/boxbot/compute/tab_calculator.rb', line 63

def [](dimension_identifier)
  tab_count_for_length(
    dimension_length(
      dimension_identifier
    )
  )
end

#calculate(dimension_identifier) ⇒ TabWidthResult

Computes the tab width for a given dimension, and returns a TabWithResult struct.

Raises:

  • ArgumentError if argument is not a String, Symbol, or Integer.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/boxbot/compute/tab_calculator.rb', line 45

def calculate(dimension_identifier)
  index     = resolve_dimension_argument(dimension_identifier)
  length    = dimensions.inner_box[index]
  tab_count = tab_count_for_length(length)
  tab_width = length / tab_count

  TabWidthResult.new(Types::Dimensions[index],
                     length,
                     tab_count,
                     tab_width)
end