Class: Tabula::Cell

Inherits:
Rectangle show all
Defined in:
lib/tabula/table/cell.rb

Overview

Represents a cell in a table. Contains text content and positional information.

Constant Summary

Constants inherited from Rectangle

Rectangle::VERTICAL_COMPARISON_THRESHOLD

Instance Attribute Summary collapse

Attributes inherited from Rectangle

#height, #left, #top, #width

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rectangle

#<=>, #area, #bottom, #bottom=, bounding_box_of, #bounds, #center, #contains?, #contains_point?, #dup, from_bounds, from_points, #horizontal_overlap, #horizontally_overlaps?, #intersection, #intersects?, #merge, #merge!, #overlap_ratio, #points, #right, #right=, #vertical_overlap, #vertically_overlaps?, #x, #x=, #y, #y=

Constructor Details

#initialize(top, left, width, height, placeholder: false) ⇒ Cell

Returns a new instance of Cell.

Parameters:

  • top (Float) —

    top coordinate

  • left (Float) —

    left coordinate

  • width (Float) —

    cell width

  • height (Float) —

    cell height

  • placeholder (Boolean) (defaults to: false) —

    whether this is a placeholder cell



15
16
17
18
19
# File 'lib/tabula/table/cell.rb', line 15

def initialize(top, left, width, height, placeholder: false)
  super(top, left, width, height)
  @text_elements = []
  @placeholder = placeholder
end

Instance Attribute Details

#placeholder ⇒ Object

Returns the value of attribute placeholder.



8
9
10
# File 'lib/tabula/table/cell.rb', line 8

def placeholder
  @placeholder
end

#text_elements ⇒ Object (readonly)

Returns the value of attribute text_elements.



7
8
9
# File 'lib/tabula/table/cell.rb', line 7

def text_elements
  @text_elements
end

Class Method Details

.empty ⇒ Cell

Create an empty placeholder cell

Returns:



30
31
32
# File 'lib/tabula/table/cell.rb', line 30

def self.empty
  new(0, 0, 0, 0, placeholder: true)
end

.from_rectangle(rect) ⇒ Cell

Create a cell from a rectangle

Parameters:

Returns:



24
25
26
# File 'lib/tabula/table/cell.rb', line 24

def self.from_rectangle(rect)
  new(rect.top, rect.left, rect.width, rect.height)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



99
100
101
102
103
# File 'lib/tabula/table/cell.rb', line 99

def ==(other)
  return false unless other.is_a?(Cell)

  super && text == other.text
end

#add(element) ⇒ Object

Add a text element to this cell

Parameters:



36
37
38
39
# File 'lib/tabula/table/cell.rb', line 36

def add(element)
  @text_elements << element
  self
end

#add_all(elements) ⇒ Object

Add multiple text elements

Parameters:



43
44
45
46
# File 'lib/tabula/table/cell.rb', line 43

def add_all(elements)
  elements.each { |e| add(e) }
  self
end

#blank? ⇒ Boolean

Check if cell is blank (empty or contains only whitespace)

Returns:

  • (Boolean)


72
73
74
75
76
77
# File 'lib/tabula/table/cell.rb', line 72

def blank?
  return true if @text_elements.empty?

  # Check if all text content is just whitespace
  text.strip.empty?
end

#empty? ⇒ Boolean

Check if cell is empty (no text elements)

Returns:

  • (Boolean)


66
67
68
# File 'lib/tabula/table/cell.rb', line 66

def empty?
  @text_elements.empty?
end

#has_text? ⇒ Boolean

Check if cell has any text

Returns:

  • (Boolean)


60
61
62
# File 'lib/tabula/table/cell.rb', line 60

def has_text?
  @text_elements.any?
end

#hash ⇒ Object



106
107
108
# File 'lib/tabula/table/cell.rb', line 106

def hash
  [super, text].hash
end

#inspect ⇒ Object



95
96
97
# File 'lib/tabula/table/cell.rb', line 95

def inspect
  to_s
end

#placeholder? ⇒ Boolean

Check if this is a placeholder cell

Returns:

  • (Boolean)


81
82
83
# File 'lib/tabula/table/cell.rb', line 81

def placeholder?
  @placeholder
end

#spanning? ⇒ Boolean

Check if this cell spans multiple rows/columns (stub for future use)

Returns:

  • (Boolean)


87
88
89
# File 'lib/tabula/table/cell.rb', line 87

def spanning?
  false
end

#text(separator: ' ') ⇒ String

Get cell text content

Parameters:

  • separator (String) (defaults to: ' ') —

    separator between text elements

Returns:

  • (String)


51
52
53
54
55
56
# File 'lib/tabula/table/cell.rb', line 51

def text(separator: ' ')
  sorted = @text_elements.sort_by { |e| [e.top, e.left] }
  sorted.map do |e|
    e.respond_to?(:text) ? e.text : e.to_s
  end.join(separator).strip
end

#to_s ⇒ Object



91
92
93
# File 'lib/tabula/table/cell.rb', line 91

def to_s
  "Cell[#{text.inspect}](#{left}, #{top}, #{width}, #{height})"
end