Class: Coopy::SimpleTable

Inherits:
Object
  • Object
show all
Includes:
Table
Defined in:
lib/coopy/simple_table.rb

Instance Attribute Summary collapse

Attributes included from Table

#height, #width

Instance Method Summary collapse

Constructor Details

#initialize(w, h) ⇒ SimpleTable

Returns a new instance of SimpleTable.



6
7
8
9
10
# File 'lib/coopy/simple_table.rb', line 6

def initialize(w, h)
  @data = {} # Map<Int,Dynamic>
  @width = w
  @height = h
end

Instance Attribute Details

#sizeObject

Returns the value of attribute size.



16
17
18
# File 'lib/coopy/simple_table.rb', line 16

def size
  @size
end

Instance Method Details

#clearObject



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

def clear
  @data = {}
end

#get_cell(x, y) ⇒ Object



22
23
24
# File 'lib/coopy/simple_table.rb', line 22

def get_cell(x, y)
  @data[x+y*@width]
end

#get_cell_viewObject



46
47
48
# File 'lib/coopy/simple_table.rb', line 46

def get_cell_view
  Coopy::SimpleView.new
end

#get_sizeObject



18
19
20
# File 'lib/coopy/simple_table.rb', line 18

def get_size
  @height
end

#get_tableObject



12
13
14
# File 'lib/coopy/simple_table.rb', line 12

def get_table
  self
end

#insert_or_delete_columns(fate, wfate) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/coopy/simple_table.rb', line 81

def insert_or_delete_columns(fate, wfate)
  data2 = {}
  (0..fate.length-1).each do |i|
    j = fate[i]
    if (j!=-1)
      (0..@height-1).each do |r|
        idx = r*@width+i
        if (data.has_key?(idx))
          data2[r*wfate+j] = data.get(idx)
        end
      end
    end
  end
  @width = wfate
  @data = data2
  return true
end

#insert_or_delete_rows(fate, hfate) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/coopy/simple_table.rb', line 64

def insert_or_delete_rows(fate, hfate)
  data2 = {}
  (0..fate.length-1).each do |i|
    j = fate[i]
    if (j!=-1)
      (0..@width-1).each do |c|
        idx = i*@width+c;
        idxf (@data.has_key?(idx))
        data2[j*@width+c] = @data.get(idx)
      end
    end
  end
  @h = hfate
  @data = data2
  return true
end

#is_resizable?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/coopy/simple_table.rb', line 50

def is_resizable?
  true
end

#resize(w, h) ⇒ Object



54
55
56
57
58
# File 'lib/coopy/simple_table.rb', line 54

def resize(w, h)
  @width = w
  @height = h
  true
end

#set_cell(x, y, c) ⇒ Object



26
27
28
# File 'lib/coopy/simple_table.rb', line 26

def set_cell(x, y, c)
  @data[x+y*@width] = c
end

#table_to_string(tab) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/coopy/simple_table.rb', line 34

def table_to_string(tab)
  x = ""
  (0..tab.height-1).each do |i|
    (0..tab.width-1).each do |j|
      x += " " if (j>0)
      x += tab.get_cell(j,i).to_s
    end
    x += "\n"
  end
  return x
end

#to_sObject



30
31
32
# File 'lib/coopy/simple_table.rb', line 30

def to_s
  table_to_string(self)
end

#trim_blankObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/coopy/simple_table.rb', line 99

def trim_blank
  return true if (h==0)
  h_test = @height
  h_test = 3 if (h_test>=3)
  view = get_cell_view
  space = view.to_datum("")
  more = true
  while (more)
    (0..width-1).each do |i|
      c = get_cell(i,@height-1)
      if (!(view.equals(c,space)||c==nil))
        more = false
        break
      end
    end
    h-=1 if (more) 
  end
  more = true
  nw = @width
  while (more)
    break if (@width==0)
    (0..h_test-1).each do |i|
      c = get_cell(nw-1,i)
      if (!(view.equals(c,space)||c==nil))
        more = false
        break
      end
    end
    nw -=1 if (more)
  end
  return true if (nw==w) 
  data2 = {}
  (0..nw-1).each do |i|
    (0..h-1).each do |r|
      idx = r*@width+i;
      if (@data.exists(idx))
        data2[r*nw+i] = @data.get(idx)
      end
    end
  end
  @width = nw
  @data = data2
  return true
end