Module: Formatting

Defined in:
lib/formatting.rb

Overview

Formatting module left/right alignment and justified alignment configurable functions

Defined Under Namespace

Classes: CellFormatter, HeaderFormatter

Constant Summary collapse

REPL_STR =

constants

'~'
PAD_STR =

replacement string value

' '

Class Method Summary collapse

Class Method Details

.format_value_by_width_and_just(value, width, just) ⇒ String

Returns Formatted string value.

Parameters:

  • value (String)

    String value for formatting

  • width (Integer)

    Width in chars for justified alignment

  • just (String)

    Type of alignment: left, right, center. Default: left

Returns:

  • (String)

    Formatted string value



14
15
16
17
18
19
20
21
22
23
# File 'lib/formatting.rb', line 14

def self.format_value_by_width_and_just(value,width,just)
  value = value.strip.length > width ? (value.strip[0..width-Formatting::REPL_STR.length-1]+Formatting::REPL_STR) : value.strip
  case just
    when 'left' then value.ljust(width,Formatting::PAD_STR)
    when 'right' then value.rjust(width,Formatting::PAD_STR)
    when 'center' then value.center(width,Formatting::PAD_STR)
    else
      value.ljust(width,Formatting::PAD_STR)
  end
end