class Cell

Objects of this class represent cells in a spreadsheet table

Attributes

ideal_height[R]
ideal_width[R]
value[R]

Public Class Methods

new(row = nil, col = nil, value = nil) click to toggle source
# File lib/cell.rb, line 38
def initialize(row = nil, col = nil, value = nil)
  @log = @@log
  @row = row if row
  @col = col if col
  @type_class = @@DEF_TYPE
  @value = value
  split_value
  set_limits
  @row.resize
  @log.debug("cell.initialize, lines is #{@lines}, value is #{value}, ideal_width is #{@ideal_width}")
end

Public Instance Methods

col() click to toggle source
# File lib/cell.rb, line 76
def col
  @col.number
end
line(num = nil) click to toggle source
# File lib/cell.rb, line 54
def line(num = nil)
  num && num < @lines.length ? @lines[num].to_s : ' ' if @lines && !@lines.empty?
end
resize() click to toggle source
# File lib/cell.rb, line 62
def resize
  @@split_pattern = nil
  split_value
  set_limits
  @row.resize
end
row() click to toggle source
# File lib/cell.rb, line 80
def row
  @row.number
end
to_cell() click to toggle source
# File lib/cell.rb, line 50
def to_cell()
  self
end
to_s() click to toggle source
# File lib/cell.rb, line 58
def to_s
  object_id.to_s << "{" << @row.number.to_s << ":" << @col.number.to_s << ", " << @lines.to_s << ", " << @ideal_height.to_s << ", " << @ideal_width.to_s << " }"
end
value=(value) click to toggle source
# File lib/cell.rb, line 69
def value=(value)
  @@split_pattern = nil
  @value = value
  split_value
  @log.debug('after split_value, lines is ' <<  @lines.to_s)
end

Private Instance Methods

set_limits() click to toggle source
# File lib/cell.rb, line 112
def set_limits
  if @lines && !@lines.empty?
    @ideal_height = @lines.length
    @ideal_width = @lines.max {|l1, l2 | l1.length <=> l2.length}.length if @lines && !@lines.empty?
  else
    @ideal_width ||= @col.width 
    @ideal_height ||= @@DEF_HEIGHT
    @height ||= @ideal_height
  end
end
split_value() click to toggle source
# File lib/cell.rb, line 88
def split_value()
  # The regex may be used in many cells. Define on class-level.
  # And...
  # This looks complicated because it is.
  #
  # until 18 february 2017
  # @@split_pattern = Regexp.new('[\p{P}\p{M}]?\b.{1,%i}\b[\p{P}\p{M}]?' %(@col.width)) if !@@split_pattern
  # since 18 february 2017
  @@split_pattern = Regexp.new('\b(?:.{1,%i}(?:[\b\s\p{P}]?))' %(@col.width - 1)) if !@@split_pattern

  @lines = @value.to_s.scan(@@split_pattern) # .collect {|match| match.strip}
  @lines.reject! {|l| l.empty?}
  set_limits
  if @value.to_s.strip.length > @lines.join.strip.length
    # @lines.insert(0, '...' )
    if(! @lines.empty?)
      @lines[@lines.length - 1] = '...'  
    else
      @lines.insert(0, '...' )
    end
  end
  @row.resize
end