Class: RestPack::Serializer::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/restpack_serializer/options.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(serializer, params = {}, scope = nil, context = {}) ⇒ Options

Returns a new instance of Options.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/restpack_serializer/options.rb', line 6

def initialize(serializer, params = {}, scope = nil, context = {})
  params.symbolize_keys! if params.respond_to?(:symbolize_keys!)

  @page = 1
  @page_size = RestPack::Serializer.config.page_size
  @include = []
  @filters = filters_from_params(params, serializer)
  @serializer = serializer
  @model_class = serializer.model_class
  @scope = scope || model_class.send(:all)
  @context = context
  @include_links = true

  @page = params[:page].to_i if params[:page]
  @page_size = params[:page_size].to_i if params[:page_size]
  @include = params[:include].split(',').map(&:to_sym) if params[:include]
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



3
4
5
# File 'lib/restpack_serializer/options.rb', line 3

def context
  @context
end

#filtersObject

Returns the value of attribute filters.



3
4
5
# File 'lib/restpack_serializer/options.rb', line 3

def filters
  @filters
end

#includeObject

Returns the value of attribute include.



3
4
5
# File 'lib/restpack_serializer/options.rb', line 3

def include
  @include
end

Returns the value of attribute include_links.



3
4
5
# File 'lib/restpack_serializer/options.rb', line 3

def include_links
  @include_links
end

#model_classObject

Returns the value of attribute model_class.



3
4
5
# File 'lib/restpack_serializer/options.rb', line 3

def model_class
  @model_class
end

#pageObject

Returns the value of attribute page.



3
4
5
# File 'lib/restpack_serializer/options.rb', line 3

def page
  @page
end

#page_sizeObject

Returns the value of attribute page_size.



3
4
5
# File 'lib/restpack_serializer/options.rb', line 3

def page_size
  @page_size
end

#scopeObject

Returns the value of attribute scope.



3
4
5
# File 'lib/restpack_serializer/options.rb', line 3

def scope
  @scope
end

#serializerObject

Returns the value of attribute serializer.



3
4
5
# File 'lib/restpack_serializer/options.rb', line 3

def serializer
  @serializer
end

Instance Method Details

#default_page_size?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/restpack_serializer/options.rb', line 37

def default_page_size?
  @page_size == RestPack::Serializer.config.page_size
end

#filters_as_url_paramsObject



41
42
43
# File 'lib/restpack_serializer/options.rb', line 41

def filters_as_url_params
  @filters.sort.map {|k,v| "#{k}=#{v.join(',')}" }.join('&')
end

#scope_with_filtersObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/restpack_serializer/options.rb', line 24

def scope_with_filters
  scope_filter = {}
  @filters.keys.each do |filter|
    value = @filters[filter]
    if value.is_a?(String)
      value = value.split(',')
    end
    scope_filter[filter] = value
  end

  @scope.where(scope_filter)
end