Class: Encore::Serializer::OptionsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/encore/serializer/options_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ OptionsParser

Returns a new instance of OptionsParser.



4
5
6
# File 'lib/encore/serializer/options_parser.rb', line 4

def initialize(opts)
  @opts = opts
end

Instance Method Details

#include(serializer) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/encore/serializer/options_parser.rb', line 8

def include(serializer)
  opt = @opts[:include]

  output = []

  # Split includes list
  output += opt.split(',').map(&:to_sym) if opt.present?

  # Remove resource type not included in 'can_include'
  output = serializer.can_include & output if serializer.respond_to?(:can_include)

  # Add 'always_include' resource type
  output += serializer.always_include if serializer.respond_to?(:always_include)

  output
end

#pageObject



25
26
27
28
29
# File 'lib/encore/serializer/options_parser.rb', line 25

def page
  opt = @opts[:page]

  opt.present? ? opt.to_i : 1
end

#per_pageObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/encore/serializer/options_parser.rb', line 31

def per_page
  opt = @opts[:per_page]

  return Kaminari.config.default_per_page if opt.nil?

  max_per_page = Kaminari.config.max_per_page.to_i
  opt = opt.to_i

  if max_per_page.zero?
    opt
  else
    opt > max_per_page ? max_per_page : opt
  end
end

#skip_pagingObject



46
47
48
# File 'lib/encore/serializer/options_parser.rb', line 46

def skip_paging
  @opts[:skip_paging].present?
end