class HFieldTable
Attributes
allCols[RW]
allRows[RW]
tableName[RW]
Public Class Methods
new(allRows = false, allCols = false)
click to toggle source
# File lib/hdatastructures/hfieldtable.rb, line 9 def initialize(allRows = false, allCols = false) @tableName = nil @fields = {} @allRows = allRows @allCols = allCols end
test()
click to toggle source
# 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()
click to toggle source
# 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.insertRow(1, HRecord.new("ciao")) #fieldTable.deleteRow(1) fieldTable.show() fieldTable.allRows,fieldTable.allCols = true, true fieldTable.show() fieldTable.sortBy("price", "amount", asc: false) fieldTable.show() return fieldTable end
Public Instance Methods
[](fieldName)
click to toggle source
Calls superclass method
# File lib/hdatastructures/hfieldtable.rb, line 88 def [](fieldName) self[fieldName] = {} unless super(fieldName) return super(fieldName) end
addFieldName(fieldName, visible = true)
click to toggle source
# 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()
click to toggle source
# File lib/hdatastructures/hfieldtable.rb, line 49 def columnCount() return @fields.length() end
columnCountByKey(key = :visible)
click to toggle source
# 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()
click to toggle source
# File lib/hdatastructures/hfieldtable.rb, line 16 def copyConstructor Marshal.load(Marshal.dump(self)) end
data(row, fieldName)
click to toggle source
# File lib/hdatastructures/hfieldtable.rb, line 93 def data(row, fieldName) return self[row][fieldName] end
dataByFieldName(row, fieldName)
click to toggle source
# File lib/hdatastructures/hfieldtable.rb, line 97 def dataByFieldName(row, fieldName) return self.data(row, fieldName) end
deleteRow(row)
click to toggle source
# File lib/hdatastructures/hfieldtable.rb, line 70 def deleteRow(row) self.delete_at(row) end
each() { |row, i| ... }
click to toggle source
# 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() { |row, i| ... }
click to toggle source
# 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()
click to toggle source
# 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)
click to toggle source
Inserts a new row filled with data
# 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)
click to toggle source
# 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
method_missing(methodName, *args)
click to toggle source
# File lib/hdatastructures/hfieldtable.rb, line 34 def method_missing(methodName, *args) if methodName.to_s =~ /^setField(.+)$/ # example: setFieldCaption(fieldName, fieldCaption) @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
row(row)
click to toggle source
# File lib/hdatastructures/hfieldtable.rb, line 107 def row(row) #return self[row] if @allCols result = {} @fields.each do |fieldName, fieldValue| result[fieldName] = self.dataByFieldName(row, fieldName) if fieldValue.visible or @allCols end return result end
rowCount()
click to toggle source
# File lib/hdatastructures/hfieldtable.rb, line 45 def rowCount() return self.length() end
rowWithFieldName(row)
click to toggle source
# 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()
click to toggle source
# 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)
click to toggle source
# File lib/hdatastructures/hfieldtable.rb, line 60 def setData(row, fieldName, data) self[row][fieldName] = data end
setDataByFieldName(row, fieldName, data)
click to toggle source
# File lib/hdatastructures/hfieldtable.rb, line 74 def setDataByFieldName(row, fieldName, data) self.setData(row, fieldName, data) end
setIntoRecordByFieldName(row, fieldName, data)
click to toggle source
# File lib/hdatastructures/hfieldtable.rb, line 78 def setIntoRecordByFieldName(row, fieldName, data) self.setDataByFieldName(row, fieldName, HRecord.new(data)) end
show(key = :key)
click to toggle source
# File lib/hdatastructures/hfieldtable.rb, line 221 def show(key = :key) print "=============> HFieldTable <==============\n" self.showHeader(:caption) self.each do |row, i| row.each do |fieldName, fieldValue| self.showData(fieldValue) end puts end puts end
showData(data, key = :key)
click to toggle source
# 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
showHeader(key = :key)
click to toggle source
# File lib/hdatastructures/hfieldtable.rb, line 187 def showHeader(key = :key) self.fields.each do |fieldName, fieldValue| print $hpformat % fieldValue.value(key) end puts end
showTotalizers()
click to toggle source
# 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)
click to toggle source
fieldTable.sortBy(“price”) fieldTable.sortBy(“price”, “amount”) fieldTable.sortBy(“price”, “amount”, asc: false)
# File lib/hdatastructures/hfieldtable.rb, line 182 def sortBy(*fieldNames, asc: true) #self.herbertsort!(asc) { |row| fieldNames.map {|fieldName| row[fieldName]} } self.herbertsort!(asc) { |row| fieldNames.map {|fieldName| self.valueByType(fieldName, row[fieldName])} } end
sumByFieldName(fieldName)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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