aboutsummaryrefslogtreecommitdiff
path: root/src/guff/results.cr
blob: 61d5ce52324fec30443997ac25540bed52ccb0e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
module Guff
  class Results(R)
    getter :page
    getter :limit
    getter :num_rows
    getter :rows

    def initialize(
      @page = 1       : Int32,
      @limit = 1      : Int32,
      @num_rows = 0   : Int32,
      @rows = [] of R : Array(R),
    )
    end

    def to_json
      { 
        meta: {
          page:       @page,
          limit:      @limit,
          num_pages:  (@num_rows / @limit).ceil,
          num_rows:   @num_rows,
        },

        rows: @rows,
      }.to_json
    end

    def size
      @num_rows
    end
  end
end