Class: HFieldTable
- Inherits:
-
Array
show all
- Defined in:
- lib/hdatastructures/hfieldtable.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#[](fieldName) ⇒ Object
-
#addFieldName(fieldName, visible = true) ⇒ Object
-
#columnCount ⇒ Object
-
#columnCountByKey(key = :visible) ⇒ Object
-
#copyConstructor ⇒ Object
-
#data(row, fieldName) ⇒ Object
-
#dataByFieldName(row, fieldName) ⇒ Object
-
#deleteRow(row) ⇒ Object
-
#each ⇒ Object
-
#eachWithFieldName ⇒ Object
-
#fields ⇒ Object
-
#initialize(allRows = false, allCols = false) ⇒ HFieldTable
constructor
A new instance of HFieldTable.
-
#insertRow(row, data = nil) ⇒ Object
Inserts a new row filled with data.
-
#match(row) ⇒ Object
-
#method_missing(methodName, *args) ⇒ Object
-
#row(row) ⇒ Object
-
#rowCount ⇒ Object
-
#rowWithFieldName(row) ⇒ Object
-
#setCaption ⇒ Object
-
#setData(row, fieldName, data) ⇒ Object
-
#setDataByFieldName(row, fieldName, data) ⇒ Object
-
#setIntoRecordByFieldName(row, fieldName, data) ⇒ Object
-
#show(key = :key) ⇒ Object
-
#showData(data, key = :key) ⇒ Object
-
#showHeader(key = :key) ⇒ Object
-
#showTotalizers ⇒ Object
-
#sortBy(*fieldNames, asc: true) ⇒ Object
fieldTable.sortBy(“price”) fieldTable.sortBy(“price”, “amount”) fieldTable.sortBy(“price”, “amount”, asc: false).
-
#sumByFieldName(fieldName) ⇒ Object
-
#valueByFieldName(row, fieldName) ⇒ Object
-
#valueByType(fieldName, fieldValue) ⇒ Object
Methods inherited from Array
#<, #addIfNotPresent, #column, #filter, #herbertsort, #herbertsort!, #hjoin, #hpartition, #sortByList, #sortByPositionList, #swap, #to_js_format
Constructor Details
#initialize(allRows = false, allCols = false) ⇒ HFieldTable
Returns a new instance of HFieldTable.
9
10
11
12
13
14
|
# File 'lib/hdatastructures/hfieldtable.rb', line 9
def initialize(allRows = false, allCols = false)
@tableName = nil
@fields = {}
@allRows = allRows
@allCols = allCols
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(methodName, *args) ⇒ Object
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/hdatastructures/hfieldtable.rb', line 34
def method_missing(methodName, *args)
if methodName.to_s =~ /^setField(.+)$/ @fields[args[0]].set($1.downcase().to_sym, args[1])
else
caller_infos = caller.first.split(":")
puts "Error: #{caller_infos[0]}:#{caller_infos[1]} - #{methodName} not found".hight_red()
end
end
|
Instance Attribute Details
#allCols ⇒ Object
Returns the value of attribute allCols.
7
8
9
|
# File 'lib/hdatastructures/hfieldtable.rb', line 7
def allCols
@allCols
end
|
#allRows ⇒ Object
Returns the value of attribute allRows.
7
8
9
|
# File 'lib/hdatastructures/hfieldtable.rb', line 7
def allRows
@allRows
end
|
#tableName ⇒ Object
Returns the value of attribute tableName.
7
8
9
|
# File 'lib/hdatastructures/hfieldtable.rb', line 7
def tableName
@tableName
end
|
Class Method Details
.test ⇒ Object
234
235
236
237
238
239
240
241
|
# File 'lib/hdatastructures/hfieldtable.rb', line 234
def self.test()
table = HFieldTable.new()
table.setData(0, 0, HRecord.new("0x0"))
table.setData(10, 1, HRecord.new("10x1"))
table.setData(9, 9, HRecord.new("9x9"))
table.show()
end
|
.test2 ⇒ Object
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
# File 'lib/hdatastructures/hfieldtable.rb', line 243
def self.test2()
fieldTable = HFieldTable.new()
fieldTable.addFieldName("quantity")
fieldTable.addFieldName("description")
fieldTable.addFieldName("price", false)
fieldTable.addFieldName("amount")
fieldTable.setFieldCaption("quantity", "Quantity")
fieldTable.setFieldCaption("description", "Description")
fieldTable.setFieldCaption("price", "Price")
fieldTable.setFieldCaption("amount", "Amount")
fieldTable.setFieldFilter("price", HRecord.new(10))
fieldTable.setFieldFilter("amount", HRecord.new(10))
for i in 0..30
fieldTable.setDataByFieldName(i, "quantity", HRecord.new(i))
fieldTable.setDataByFieldName(i, "description", HRecord.new(i))
fieldTable.setDataByFieldName(i, "price", HRecord.new(rand(10)))
fieldTable.setDataByFieldName(i, "amount", HRecord.new(i))
end
fieldTable.show()
fieldTable.allRows,fieldTable.allCols = true, true
fieldTable.show()
fieldTable.sortBy("price", "amount", asc: false)
fieldTable.show()
return fieldTable
end
|
Instance Method Details
#[](fieldName) ⇒ Object
88
89
90
91
|
# File 'lib/hdatastructures/hfieldtable.rb', line 88
def [](fieldName)
self[fieldName] = {} unless super(fieldName)
return super(fieldName)
end
|
#addFieldName(fieldName, visible = true) ⇒ Object
28
29
30
31
32
|
# File 'lib/hdatastructures/hfieldtable.rb', line 28
def addFieldName(fieldName, visible = true)
record = HRecord.new(fieldName) unless @fields[fieldName]
record.set(:visible, visible)
@fields[fieldName] = record
end
|
#columnCount ⇒ Object
49
50
51
|
# File 'lib/hdatastructures/hfieldtable.rb', line 49
def columnCount()
return @fields.length()
end
|
#columnCountByKey(key = :visible) ⇒ Object
53
54
55
56
57
58
|
# File 'lib/hdatastructures/hfieldtable.rb', line 53
def columnCountByKey(key = :visible)
return self.columnCount if @allCols
i = 0
@fields.each { |fieldName, fieldValue| i += 1 if fieldValue.value(key) }
return i
end
|
#copyConstructor ⇒ Object
16
17
18
|
# File 'lib/hdatastructures/hfieldtable.rb', line 16
def copyConstructor
Marshal.load(Marshal.dump(self))
end
|
#data(row, fieldName) ⇒ Object
93
94
95
|
# File 'lib/hdatastructures/hfieldtable.rb', line 93
def data(row, fieldName)
return self[row][fieldName]
end
|
#dataByFieldName(row, fieldName) ⇒ Object
97
98
99
|
# File 'lib/hdatastructures/hfieldtable.rb', line 97
def dataByFieldName(row, fieldName)
return self.data(row, fieldName)
end
|
#deleteRow(row) ⇒ Object
70
71
72
|
# File 'lib/hdatastructures/hfieldtable.rb', line 70
def deleteRow(row)
self.delete_at(row)
end
|
#each ⇒ Object
130
131
132
133
134
135
|
# File 'lib/hdatastructures/hfieldtable.rb', line 130
def each()
for i in 0...self.rowCount() do
row = self.row(i)
yield(row, i) if @allRows or self.match(i)
end
end
|
#eachWithFieldName ⇒ Object
146
147
148
149
150
151
|
# File 'lib/hdatastructures/hfieldtable.rb', line 146
def eachWithFieldName()
for i in 0...self.rowCount() do
row = self.rowWithFieldName(i)
yield(row, i) if @allRows or self.match(i)
end
end
|
#fields ⇒ Object
20
21
22
23
24
25
26
|
# File 'lib/hdatastructures/hfieldtable.rb', line 20
def fields()
result = {}
@fields.each do |fieldName, fieldValue|
result[fieldName] = fieldValue if fieldValue.visible or @allCols
end
return result
end
|
#insertRow(row, data = nil) ⇒ Object
Inserts a new row filled with data
65
66
67
68
|
# File 'lib/hdatastructures/hfieldtable.rb', line 65
def insertRow(row, data = nil)
tmp = @fields.dup
self.insert(row, tmp.each { |k, v| tmp[k] = data } )
end
|
#match(row) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/hdatastructures/hfieldtable.rb', line 116
def match(row)
@fields.each do |fieldName, fieldValue|
filter = fieldValue.filter
next unless filter
data = self.dataByFieldName(row, fieldName)
return false unless data
data = HRecord.new(data) if data.class != HRecord
filter = HRecord.new(filter) if filter.class != HRecord
return false if data != filter
end
return true
end
|
#row(row) ⇒ Object
107
108
109
110
111
112
113
114
|
# File 'lib/hdatastructures/hfieldtable.rb', line 107
def row(row)
result = {}
@fields.each do |fieldName, fieldValue|
result[fieldName] = self.dataByFieldName(row, fieldName) if fieldValue.visible or @allCols
end
return result
end
|
#rowCount ⇒ Object
45
46
47
|
# File 'lib/hdatastructures/hfieldtable.rb', line 45
def rowCount()
return self.length()
end
|
#rowWithFieldName(row) ⇒ Object
137
138
139
140
141
142
143
144
|
# File 'lib/hdatastructures/hfieldtable.rb', line 137
def rowWithFieldName(row)
result = {}
self[row].each do |fieldName, fieldValue|
field = @fields[fieldName]
result[fieldName] = fieldValue if field.visible or @allCols
end
return result
end
|
#setCaption ⇒ Object
82
83
84
85
86
|
# File 'lib/hdatastructures/hfieldtable.rb', line 82
def setCaption
self.fields.each do |fieldName, fieldValue|
self.setFieldCaption(fieldName, fieldValue.value.hcapitalize())
end
end
|
#setData(row, fieldName, data) ⇒ Object
60
61
62
|
# File 'lib/hdatastructures/hfieldtable.rb', line 60
def setData(row, fieldName, data)
self[row][fieldName] = data
end
|
#setDataByFieldName(row, fieldName, data) ⇒ Object
74
75
76
|
# File 'lib/hdatastructures/hfieldtable.rb', line 74
def setDataByFieldName(row, fieldName, data)
self.setData(row, fieldName, data)
end
|
#setIntoRecordByFieldName(row, fieldName, data) ⇒ Object
78
79
80
|
# File 'lib/hdatastructures/hfieldtable.rb', line 78
def setIntoRecordByFieldName(row, fieldName, data)
self.setDataByFieldName(row, fieldName, HRecord.new(data))
end
|
#show(key = :key) ⇒ Object
221
222
223
224
225
226
227
228
229
230
231
|
# File 'lib/hdatastructures/hfieldtable.rb', line 221
def show(key = :key)
print "=============> HFieldTable <==============\n"
self.(:caption)
self.each do |row, i|
row.each do |fieldName, fieldValue|
self.showData(fieldValue)
end
puts
end
puts
end
|
#showData(data, key = :key) ⇒ Object
194
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/hdatastructures/hfieldtable.rb', line 194
def showData(data, key = :key)
if !data
print $hpformat % "." unless data
elsif data.class == Fixnum or data.class == String
print $hpformat % data
else
data.show(key)
end
end
|
187
188
189
190
191
192
|
# File 'lib/hdatastructures/hfieldtable.rb', line 187
def (key = :key)
self.fields.each do |fieldName, fieldValue|
print $hpformat % fieldValue.value(key)
end
puts
end
|
#showTotalizers ⇒ Object
206
207
208
209
210
211
212
213
214
215
216
217
218
|
# File 'lib/hdatastructures/hfieldtable.rb', line 206
def showTotalizers()
@fields.each do |fieldName, fieldValue|
totalizerName = fieldValue.totalizer
if totalizerName
print $hpformat % totalizerName
print $hpformat % self.sumByFieldName(fieldName)
puts
end
end
puts
end
|
#sortBy(*fieldNames, asc: true) ⇒ Object
fieldTable.sortBy(“price”) fieldTable.sortBy(“price”, “amount”) fieldTable.sortBy(“price”, “amount”, asc: false)
182
183
184
185
|
# File 'lib/hdatastructures/hfieldtable.rb', line 182
def sortBy(*fieldNames, asc: true)
self.herbertsort!(asc) { |row| fieldNames.map {|fieldName| self.valueByType(fieldName, row[fieldName])} }
end
|
#sumByFieldName(fieldName) ⇒ Object
154
155
156
157
158
159
160
161
162
|
# File 'lib/hdatastructures/hfieldtable.rb', line 154
def sumByFieldName(fieldName)
total = 0
for i in 0...self.rowCount() do
total += (self.dataByFieldName(i, fieldName)).to_f
end
return total
end
|
#valueByFieldName(row, fieldName) ⇒ Object
101
102
103
104
105
|
# File 'lib/hdatastructures/hfieldtable.rb', line 101
def valueByFieldName(row, fieldName)
value = self.data(row, fieldName)
value = value.value() if (value.class == HRecord)
return value
end
|
#valueByType(fieldName, fieldValue) ⇒ Object
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/hdatastructures/hfieldtable.rb', line 164
def valueByType(fieldName, fieldValue)
return nil unless fieldValue
type = @fields[fieldName].value(:type) if @fields[fieldName]
value = fieldValue.value
return value unless type
return value.to_s if type == "text"
return value.to_i if type == "integer"
return value.to_f if type == "float"
return value.to_date() if type == "date"
return value.to_datetime() if type == "datetime"
return value.to_s
end
|