aboutsummaryrefslogtreecommitdiff
path: root/src/guff/results.cr
blob: 3133d21c3c467d88ad47e61efe64da133cc59117 (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     : Int32     = 1,
      @limit    : Int32     = 1,
      @num_rows : Int32     = 0,
      @rows     : Array(R)  = [] of 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