Class: KBResultSet
Overview
KBResultSet
Class Method Summary collapse
-
.reverse(sort_field) ⇒ Object
———————————————————————– KBResultSet.reverse ———————————————————————–.
Instance Method Summary collapse
-
#initialize(table, filter, filter_types, *args) ⇒ KBResultSet
constructor
———————————————————————– initialize ———————————————————————–.
-
#set(*updates, &update_cond) ⇒ Object
———————————————————————– set ———————————————————————– ++ Update record(s) in table, return number of records updated.
-
#sort(*sort_fields) ⇒ Object
———————————————————————– sort ———————————————————————–.
-
#to_ary ⇒ Object
———————————————————————– to_ary ———————————————————————–.
-
#to_report(recs_per_page = 0, print_rec_sep = false) ⇒ Object
———————————————————————– to_report ———————————————————————–.
Constructor Details
#initialize(table, filter, filter_types, *args) ⇒ KBResultSet
initialize
3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 |
# File 'lib/kirbybase.rb', line 3645 def initialize(table, filter, filter_types, *args) @table = table @filter = filter @filter_types = filter_types super(*args) @filter.each do |f| get_meth_str = " def \#{f}()\n if defined?(@\#{f}) then\n return @\#{f}\n else\n @\#{f} = self.collect { |x| x.\#{f} }\n return @\#{f}\n end\n end\n END_OF_STRING\n self.class.class_eval(get_meth_str)\n end\nend\n" |
Class Method Details
.reverse(sort_field) ⇒ Object
KBResultSet.reverse
3638 3639 3640 |
# File 'lib/kirbybase.rb', line 3638 def KBResultSet.reverse(sort_field) return [sort_field, :desc] end |
Instance Method Details
#set(*updates, &update_cond) ⇒ Object
set
++ Update record(s) in table, return number of records updated.
3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 |
# File 'lib/kirbybase.rb', line 3679 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 |
#sort(*sort_fields) ⇒ Object
sort
3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 |
# File 'lib/kirbybase.rb', line 3696 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 |
#to_report(recs_per_page = 0, print_rec_sep = false) ⇒ Object
to_report
3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 |
# File 'lib/kirbybase.rb', line 3740 def to_report(recs_per_page=0, print_rec_sep=false) result = collect { |r| @filter.collect {|f| r.send(f)} } # How many records before a formfeed. delim = ' | ' # columns of physical rows 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 |