Class: Strings::Padder Private

Inherits:
Object
  • Object
show all
Defined in:
lib/strings/padder.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A class responsible for parsing padding value

Used internally by Pad

Constant Summary collapse

ParseError =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Class.new(ArgumentError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(padding) ⇒ Padder

Initialize a Padder



60
61
62
# File 'lib/strings/padder.rb', line 60

def initialize(padding)
  @padding = padding
end

Instance Attribute Details

#paddingArray[Integer] (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Padding

Returns:

  • (Array[Integer])


55
56
57
# File 'lib/strings/padder.rb', line 55

def padding
  @padding
end

Class Method Details

.convert_to_ary(value) ⇒ Array[Integer]

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convert value to 4 element array

Returns:

  • (Array[Integer])

    the 4 element padding array



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/strings/padder.rb', line 38

def self.convert_to_ary(value)
  if value.class <= Numeric
    [value, value, value, value]
  elsif value.nil?
    []
  elsif value.size == 2
    [value[0], value[1], value[0], value[1]]
  elsif value.size == 4
    value
  else
    raise ParseError, "Wrong :padding parameter, must be an array"
  end
end

.parse(value = nil) ⇒ TTY::Padder

Parse padding options

Turn possible values into 4 element array

Examples:

padder = TTY::Table::Padder.parse(5)
padder.padding # => [5, 5, 5, 5]

Parameters:

  • value (Object) (defaults to: nil)

Returns:

  • (TTY::Padder)

    the new padder with padding values



26
27
28
29
30
# File 'lib/strings/padder.rb', line 26

def self.parse(value = nil)
  return value if value.is_a?(self)

  new(convert_to_ary(value))
end

Instance Method Details

#bottomInteger

Bottom padding

Returns:

  • (Integer)


107
108
109
# File 'lib/strings/padder.rb', line 107

def bottom
  @padding[2].to_i
end

#bottom=(value) ⇒ nil

Set bottom padding

Parameters:

  • value (Integer)

Returns:

  • (nil)


118
119
120
# File 'lib/strings/padder.rb', line 118

def bottom=(value)
  @padding[2] = value
end

#empty?Boolean

Check if padding is set

Returns:

  • (Boolean)


147
148
149
# File 'lib/strings/padder.rb', line 147

def empty?
  padding.empty?
end

#leftInteger

Left padding

Returns:

  • (Integer)


127
128
129
# File 'lib/strings/padder.rb', line 127

def left
  @padding[3].to_i
end

#left=(value) ⇒ nil

Set left padding

Parameters:

  • value (Integer)

Returns:

  • (nil)


138
139
140
# File 'lib/strings/padder.rb', line 138

def left=(value)
  @padding[3] = value
end

#rightInteger

Right padding

Returns:

  • (Integer)


89
90
91
# File 'lib/strings/padder.rb', line 89

def right
  @padding[1].to_i
end

#right=(value) ⇒ Object

Set right padding

Parameters:

  • value (Integer)


98
99
100
# File 'lib/strings/padder.rb', line 98

def right=(value)
  @padding[1] = value
end

#to_sString

String represenation of this padder with padding values

Returns:

  • (String)


156
157
158
# File 'lib/strings/padder.rb', line 156

def to_s
  inspect
end

#topInteger

Top padding

Returns:

  • (Integer)


69
70
71
# File 'lib/strings/padder.rb', line 69

def top
  @padding[0].to_i
end

#top=(value) ⇒ nil

Set top padding

Parameters:

  • value (Integer)

Returns:

  • (nil)


80
81
82
# File 'lib/strings/padder.rb', line 80

def top=(value)
  @padding[0] = value
end