Class: NattyUI::Table::Cell::Attributes

Inherits:
Object
  • Object
show all
Defined in:
lib/natty-ui/helper/table.rb

Overview

Formatting attributes for a NattyUI::Table::Cell, Row, or NattyUI::Table::Column.

An Attributes instance is exposed on each NattyUI::Table::Cell (via #attributes), each Row (via Row#attributes), and each NattyUI::Table::Column (via NattyUI::Table::Column#attributes). Attributes set on a row or column serve as defaults that are merged with individual cell attributes during rendering.

Examples:

Set alignment on a cell

cell.attributes.align = :center

Set padding via the bulk helper

cell.attributes.assign(padding: [1, 2], vertical: :middle)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attributes) ⇒ Attributes

Returns a new instance of Attributes.

Parameters:

  • attributes (Hash)

    a customizable set of attributes

Options Hash (**attributes):

  • :eol (Boolean) — default: true

    see #eol

  • :spaces (Boolean) — default: true

    see #spaces

  • :align (:left, :center, :right, nil) — default: nil

    see #align

  • :vertical (:top, :middle, :bottom, nil) — default: nil
  • :width (Integer, Float, Range, nil) — default: nil

    see #width

  • :min_width (Integer, Float, nil) — default: nil
  • :max_width (Integer, Float, nil) — default: nil
  • :height (Integer, Range, nil) — default: nil

    see #height

  • :min_height (Integer, nil) — default: nil
  • :max_height (Integer, nil) — default: nil
  • :padding (Integer, #map, nil) — default: 0
  • :top_padding (Integer, nil) — default: 0
  • :right_padding (Integer, nil) — default: 0
  • :bottom_padding (Integer, nil) — default: 0
  • :left_padding (Integer, nil) — default: 0


461
462
463
464
465
# File 'lib/natty-ui/helper/table.rb', line 461

def initialize(**attributes)
  @eol = @spaces = true
  @padding = [0, 0, 0, 0]
  assign(attributes) unless attributes.empty?
end

Instance Attribute Details

#align:left, ...

Horizontal text alignment within the cell.

Returns:

  • (:left, :center, :right, nil)


70
71
72
# File 'lib/natty-ui/helper/table.rb', line 70

def align
  @align
end

#bottom_paddingInteger

Bottom padding in lines.

Returns:

  • (Integer)


368
# File 'lib/natty-ui/helper/table.rb', line 368

def bottom_padding = @padding[2]

#eolBoolean

Whether line breaks inside the text are collapsed to spaces.

Returns:

  • (Boolean)


48
49
50
# File 'lib/natty-ui/helper/table.rb', line 48

def eol
  @eol
end

#heightInteger, ...

Height constraint for this cell.

Returns an exact Integer when both bounds are equal, a Range when they differ, or nil when neither is set.

Examples:

cell.attributes.height   # => nil, Integer, or Range

Set exact height

cell.attributes.height = 3

Set height range

cell.attributes.height = (2..5)

Returns:

  • (Integer, Range, nil)


252
253
254
255
# File 'lib/natty-ui/helper/table.rb', line 252

def height
  return @min_height if @min_height == @max_height
  (@min_height..@max_height) if @min_height || @max_height
end

#max_heightInteger?

Maximum row height in lines for this cell.

Returns:

  • (Integer, nil)


218
219
220
# File 'lib/natty-ui/helper/table.rb', line 218

def max_height
  @max_height
end

#max_widthInteger, ...

Maximum column width in characters for this cell.

Returns:

  • (Integer, Float, nil)


119
120
121
# File 'lib/natty-ui/helper/table.rb', line 119

def max_width
  @max_width
end

#min_heightInteger?

Minimum row height in lines for this cell.

Returns:

  • (Integer, nil)


197
198
199
# File 'lib/natty-ui/helper/table.rb', line 197

def min_height
  @min_height
end

#min_widthInteger, ...

Minimum column width in characters for this cell.

Returns:

  • (Integer, Float, nil)


99
100
101
# File 'lib/natty-ui/helper/table.rb', line 99

def min_width
  @min_width
end

#paddingArray(Integer, Integer, Integer, Integer)

Cell padding as a four-element array [top, right, bottom, left].

Returns:

  • (Array(Integer, Integer, Integer, Integer))


297
298
299
# File 'lib/natty-ui/helper/table.rb', line 297

def padding
  @padding
end

#padding_leftInteger

Left padding in characters.

Returns:

  • (Integer)


380
# File 'lib/natty-ui/helper/table.rb', line 380

def left_padding = @padding[3]

#right_paddingInteger

Right padding in characters.

Returns:

  • (Integer)


356
# File 'lib/natty-ui/helper/table.rb', line 356

def right_padding = @padding[1]

#spacesBoolean

Whether whitespace are preserved.

Returns:

  • (Boolean)


59
60
61
# File 'lib/natty-ui/helper/table.rb', line 59

def spaces
  @spaces
end

#top_paddingInteger

Top padding in lines.

Returns:

  • (Integer)


344
# File 'lib/natty-ui/helper/table.rb', line 344

def top_padding = @padding[0]

#vertical:top, ...

Vertical text alignment within the cell.

Returns:

  • (:top, :middle, :bottom, nil)


83
84
85
# File 'lib/natty-ui/helper/table.rb', line 83

def vertical
  @vertical
end

#widthInteger, ...

Width constraint for this cell.

Returns an exact Integer or Float when min_width equals max_width, a Range when they differ, or nil when neither is set.

Examples:

cell.attributes.width   # => nil, Integer, Float, or Range

Set exact width

cell.attributes.width = 20

Set width range

cell.attributes.width = (10..30)

Returns:

  • (Integer, Float, Range, nil)


152
153
154
155
# File 'lib/natty-ui/helper/table.rb', line 152

def width
  return @min_width if @min_width == @max_width
  (@min_width..@max_width) if @min_width || @max_width
end

Instance Method Details

#assign(attributes) ⇒ Cell::Attributes

Applies attribute values from a hash.

Examples:

cell.attributes.assign(align: :center, padding: [0, 2])

Parameters:

  • attributes (#to_hash)

    attribute hash, see #initialize

Returns:



416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/natty-ui/helper/table.rb', line 416

def assign(attributes)
  attributes = attributes.to_hash
  unless attributes.empty?
    @eol = false if attributes[:eol] == false
    @spaces = false if attributes[:spaces] == false
    self.align = attributes[:align] if attributes.key?(:align)
    self.vertical = attributes[:vertical] if attributes.key?(:vertical)
    assign_padding(attributes)
    assign_width(attributes)
    assign_height(attributes)
  end
  self
end

#empty?Boolean

Returns true when all attributes are at their default values.

Returns:

  • (Boolean)


391
392
393
394
# File 'lib/natty-ui/helper/table.rb', line 391

def empty?
  @align.nil? && @vertical.nil? && @min_width.nil? && @max_width.nil? &&
    @min_height.nil? && @max_height.nil? && @padding.all?(&:zero?)
end

#left_paddingInteger

Left padding in characters.

Returns:

  • (Integer)


380
# File 'lib/natty-ui/helper/table.rb', line 380

def left_padding = @padding[3]