Class: Numerals::Format::Symbols::Padding

Inherits:
Numerals::FormattingAspect show all
Includes:
ModalSupport::StateEquivalent
Defined in:
lib/numerals/format/symbols/padding.rb

Overview

Padding a number to a given width

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Numerals::FormattingAspect

#[], [], aspect, #set, set

Constructor Details

#initialize(*args) ⇒ Padding

Parameters:

  • :width field width (0 for no padding)

  • :fill filling symbol; nil for no padding; 0 to use the digit zero; otherwise should be a String

  • :adjust adjust mode: :left, :right, :integer, :center



13
14
15
16
17
18
# File 'lib/numerals/format/symbols/padding.rb', line 13

def initialize(*args)
  @width = 0
  @fill = nil
  @adjust = :right
  set! *args
end

Instance Attribute Details

#adjustObject

Returns the value of attribute adjust.



29
30
31
# File 'lib/numerals/format/symbols/padding.rb', line 29

def adjust
  @adjust
end

#fillObject

Returns the value of attribute fill.



29
30
31
# File 'lib/numerals/format/symbols/padding.rb', line 29

def fill
  @fill
end

#widthObject

Returns the value of attribute width.



29
30
31
# File 'lib/numerals/format/symbols/padding.rb', line 29

def width
  @width
end

Instance Method Details

#inspectObject



63
64
65
# File 'lib/numerals/format/symbols/padding.rb', line 63

def inspect
  "Format::Symbols::#{to_s}"
end

#leading_zeros=(width) ⇒ Object



31
32
33
34
35
# File 'lib/numerals/format/symbols/padding.rb', line 31

def leading_zeros=(width)
  @width = width
  @fill = 0
  @adjust = :internal
end

#padded?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/numerals/format/symbols/padding.rb', line 37

def padded?
  @width > 0 && @fill && @fill != ''
end

#padding_sizes(number_size) ⇒ Object

Returns size (characters of left, internal and right padding) for a number of given width (without padding)



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/numerals/format/symbols/padding.rb', line 69

def padding_sizes(number_size)
  left_padding_size = internal_padding_size = right_padding_size = 0
  padding_size = width - number_size
  if padding_size > 0 && padded?
    case adjust
    when :left
      left_padding_size = padding_size
    when :right
      right_padding_size = padding_size
    when :internal
      internal_padding_size = padding_size
    when :center
      left_padding_size = (padding_size + 1) / 2
      right_padding_size = padding_size - left_padding_size
    end
  end
  [left_padding_size, internal_padding_size, right_padding_size]
end

#parametersObject



41
42
43
# File 'lib/numerals/format/symbols/padding.rb', line 41

def parameters
  { width: width, fill: fill, adjust: adjust }
end

#to_sObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/numerals/format/symbols/padding.rb', line 45

def to_s
  params = []
  if fill == 0 && adjust == :internal
    params << "leading_zeros: #{width}"
  else
    if width != 0
      params << "width: #{width}"
    end
    if fill
      params << "fill: #{fill.inspect}"
    end
    if adjust != :right || !params.empty?
      params << "adjust: #{adjust.inspect}"
    end
  end
  "Padding[#{params.join(', ')}]"
end