class Realize::Format::Pad

Pad a string value with a specified 'with' value (defaults to blank space) up until the passed in length is reached. The 'side' option can be used to specify whether the padding should occur to the left or right side of the value. Examples:

'ABC' + [length: 10, side: 'left', with: '123'] => '1231231ABC'
'ABC' + [length: 10, side: 'right, with: '123'] => 'ABC1231231'

If length is not specified or is less than the actual value's length then no padding will occur.

Constants

DEFAULT_SIDE
DEFAULT_WITH

Attributes

length[R]
side[R]
with[R]

Public Class Methods

new(length: nil, side: LEFT, with: DEFAULT_WITH) click to toggle source
# File lib/realize/format/pad.rb, line 35
def initialize(length: nil, side: LEFT, with: DEFAULT_WITH)
  @length = length ? length.to_i : nil
  @side   = Side.const_get(side.to_s.upcase.to_sym)
  @with   = with.to_s

  freeze
end

Public Instance Methods

transform(_resolver, value, _time, _record) click to toggle source
# File lib/realize/format/pad.rb, line 43
def transform(_resolver, value, _time, _record)
  return value unless length

  value.to_s.send(just_method, length, with)
end

Private Instance Methods

just_method() click to toggle source
# File lib/realize/format/pad.rb, line 51
def just_method
  case side
  when LEFT
    :rjust
  when RIGHT
    :ljust
  end
end