Class: Interactors::Api::Paginate

Inherits:
Object
  • Object
show all
Includes:
Interpipe::Interactor
Defined in:
app/interactors/api/paginate.rb

Constant Summary collapse

DEFAULT_LIMIT =
20
DEFAULT_OFFSET =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Interpipe::Interactor

included, #initialize, #setup

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit


12
13
14
# File 'app/interactors/api/paginate.rb', line 12

def limit
  @limit
end

#offsetObject (readonly)

Returns the value of attribute offset


12
13
14
# File 'app/interactors/api/paginate.rb', line 12

def offset
  @offset
end

Instance Method Details

#countObject


24
25
26
# File 'app/interactors/api/paginate.rb', line 24

def count
  @count ||= @orig_dataset.count
end

#datasetObject


28
29
30
31
32
# File 'app/interactors/api/paginate.rb', line 28

def dataset
  @orig_dataset
    .limit(@limit)
    .offset(@offset)
end

#metaObject


34
35
36
37
38
39
40
# File 'app/interactors/api/paginate.rb', line 34

def meta
  {
    count: count,
    limit: limit,
    offset: offset
  }
end

#perform(dataset:, params: {}, **options) ⇒ Object


17
18
19
20
21
22
# File 'app/interactors/api/paginate.rb', line 17

def perform(dataset:, params: {}, **options)
  @orig_dataset = dataset
  @params = params
  @limit = params[:limit] || DEFAULT_LIMIT
  @offset = params[:offset] || DEFAULT_OFFSET
end

#to_hObject


42
43
44
45
46
47
48
49
# File 'app/interactors/api/paginate.rb', line 42

def to_h
  {
    dataset: dataset,
    count: count,
    limit: limit,
    offset: offset
  }
end