Class: Column

Inherits:
Object
  • Object
show all
Includes:
BasicLogging
Defined in:
lib/column.rb

Overview

Objects of this class represent columns in a spreadsheet table

Constant Summary collapse

@@DEF_WIDTH =
15
@@col_width =
@@DEF_WIDTH
@@columns =
Array.new

Constants included from BasicLogging

BasicLogging::DEBUG, BasicLogging::ERROR, BasicLogging::FATAL, BasicLogging::INFO, BasicLogging::Levels, BasicLogging::UNKNOWN, BasicLogging::WARN

Instance Attribute Summary collapse

Attributes included from BasicLogging

#log_level, #target

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BasicLogging

is_muted?, #log, mute, #set_level, set_level, set_target, #set_target

Constructor Details

#initialize(number = nil) ⇒ Column

Returns a new instance of Column.



28
29
30
31
32
33
# File 'lib/column.rb', line 28

def initialize(number = nil)
  @number = number
  @cells = Array.new
  @width = @@col_width
  @@columns << self
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



84
85
86
# File 'lib/column.rb', line 84

def number
  @number
end

#widthObject (readonly)

Returns the value of attribute width.



84
85
86
# File 'lib/column.rb', line 84

def width
  @width
end

Class Method Details

.col_widthObject

return the width of the column



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

def self::col_width
  @@col_width
end

.col_width=(w) ⇒ Object

set the column width to w



65
66
67
68
69
70
# File 'lib/column.rb', line 65

def self::col_width=(w)
  @@col_width = w
  debug(@number.to_s << ' ' <<"self::col_width=(#{w}), calling resize")

  @@columns.each {|col| col.resize(w)}
end

Instance Method Details

#add(cell) ⇒ Object Also known as: <<

add a cell to the column



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

def add(cell)
  n_cell = nil
  if cell.respond_to?(:to_cell)
    n_cell = cell
  elsif cell.respond_to?(:to_i)
    n_cell = Cell.new( cell, @number) 
  else
    msg = 'For a new cell, a row-number must be specified'
    error(yellow(msg))
    raise StandardError(msg)
  end
  @cells.insert(n_cell.row, n_cell) 
  @cells.compact!
  set_limits
end

#each(&b) ⇒ Object

execute the block for each cell in the column



53
54
55
56
57
# File 'lib/column.rb', line 53

def each(&b)
  @cells.each do |c|
    yield(c) 
  end
end

#resize(width) ⇒ Object

resize the column to width



73
74
75
76
77
# File 'lib/column.rb', line 73

def resize(width)
  @width = width
  debug(@number.to_s << ' ' <<'calling resize')
  @cells.each {|cell| cell.resize}
end

#to_sObject

returns a string representation of the column



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

def to_s
  '<' << self.class.name.dup << ':' << object_id.to_s << "{number=%s width=%s cells.length=}>" %[@number, @cells.length]
end