Class: KBResultSet

Inherits:
Array
  • Object
show all
Defined in:
lib/og/store/kirby/kirbybase.rb

Overview


KBResult


Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, filter, filter_types, *args) ⇒ KBResultSet


initialize




1445
1446
1447
1448
1449
1450
# File 'lib/og/store/kirby/kirbybase.rb', line 1445

def initialize(table, filter, filter_types, *args)
    @table = table
    @filter = filter
    @filter_types = filter_types
    super(*args)
end

Class Method Details

.reverse(sort_field) ⇒ Object


KBResultSet.reverse




1437
1438
1439
# File 'lib/og/store/kirby/kirbybase.rb', line 1437

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.



1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
# File 'lib/og/store/kirby/kirbybase.rb', line 1466

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




1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
# File 'lib/og/store/kirby/kirbybase.rb', line 1484

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_aryObject


to_ary




1456
1457
1458
# File 'lib/og/store/kirby/kirbybase.rb', line 1456

def to_ary
    to_a
end

#to_report(recs_per_page = 0, print_rec_sep = false) ⇒ Object


to_report




1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
# File 'lib/og/store/kirby/kirbybase.rb', line 1528

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,
     :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