Class: Realize::Format::Pad

Inherits:
Object
  • Object
show all
Includes:
Side
Defined in:
lib/realize/format/pad.rb

Overview

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.

Defined Under Namespace

Modules: Side

Constant Summary collapse

DEFAULT_SIDE =
LEFT
DEFAULT_WITH =
' '

Constants included from Side

Side::LEFT, Side::RIGHT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(length: nil, side: LEFT, with: DEFAULT_WITH) ⇒ Pad

Returns a new instance of Pad.



35
36
37
38
39
40
41
# 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

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



33
34
35
# File 'lib/realize/format/pad.rb', line 33

def length
  @length
end

#sideObject (readonly)

Returns the value of attribute side.



33
34
35
# File 'lib/realize/format/pad.rb', line 33

def side
  @side
end

#withObject (readonly)

Returns the value of attribute with.



33
34
35
# File 'lib/realize/format/pad.rb', line 33

def with
  @with
end

Instance Method Details

#transform(_resolver, value, _time, _record) ⇒ Object



43
44
45
46
47
# 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