def set(*updates, &update_cond)
raise 'Cannot specify both a hash and a proc for method #set!' \
unless updates.empty? or update_cond.nil?
raise 'Must specify update proc or hash for method #set!' if \
updates.empty? and update_cond.nil?
if updates.empty?
@table.set(self, update_cond)
else
@table.set(self, updates)
end
end
def sort(*sort_fields)
sort_fields_arrs = []
sort_fields.each do |f|
if f.to_s[0..0] == '-'
sort_fields_arrs << [f.to_s[1..-1].to_sym, :desc]
elsif f.to_s[0..0] == '+'
sort_fields_arrs << [f.to_s[1..-1].to_sym, :asc]
else
sort_fields_arrs << [f, :asc]
end
end
sort_fields_arrs.each do |f|
raise "Invalid sort field" unless @filter.include?(f[0])
end
super() { |a,b|
x = []
y = []
sort_fields_arrs.each do |s|
if [:Integer, :Float].include?(
@filter_types[@filter.index(s[0])])
a_value = a.send(s[0]) || 0
b_value = b.send(s[0]) || 0
else
a_value = a.send(s[0])
b_value = b.send(s[0])
end
if s[1] == :desc
x << b_value
y << a_value
else
x << a_value
y << b_value
end
end
x <=> y
}
end
def to_report(recs_per_page=0, print_rec_sep=false)
result = collect { |r| @filter.collect {|f| r.send(f)} }
delim = ' | '
columns = [@filter].concat(result).transpose
max_widths = columns.collect { |c|
c.max { |a,b| a.to_s.length <=> b.to_s.length }.to_s.length
}
row_dashes = '-' * (max_widths.inject {|sum, n| sum + n} +
delim.length * (max_widths.size - 1))
justify_hash = { :String => :ljust, :Integer => :rjust,
:Float => :rjust, :Boolean => :ljust, :Date => :ljust,
:Time => :ljust, :DateTime => :ljust }
header_line = @filter.zip(max_widths, @filter.collect { |f|
@filter_types[@filter.index(f)] }).collect { |x,y,z|
x.to_s.send(justify_hash[z], y) }.join(delim)
output = ''
recs_on_page_cnt = 0
result.each do |row|
if recs_on_page_cnt == 0
output << header_line + "\n" << row_dashes + "\n"
end
output << row.zip(max_widths, @filter.collect { |f|
@filter_types[@filter.index(f)] }).collect { |x,y,z|
x.to_s.send(justify_hash[z], y) }.join(delim) + "\n"
output << row_dashes + '\n' if print_rec_sep
recs_on_page_cnt += 1
if recs_per_page > 0 and (recs_on_page_cnt ==
num_recs_per_page)
output << '\f'
recs_on_page_count = 0
end
end
return output
end
end