Class: RestPack::Serializer::Options
- Inherits:
-
Object
- Object
- RestPack::Serializer::Options
- Defined in:
- lib/restpack_serializer/options.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
-
#filters ⇒ Object
Returns the value of attribute filters.
-
#include ⇒ Object
Returns the value of attribute include.
-
#include_links ⇒ Object
Returns the value of attribute include_links.
-
#model_class ⇒ Object
Returns the value of attribute model_class.
-
#page ⇒ Object
Returns the value of attribute page.
-
#page_size ⇒ Object
Returns the value of attribute page_size.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#serializer ⇒ Object
Returns the value of attribute serializer.
-
#sorting ⇒ Object
Returns the value of attribute sorting.
Instance Method Summary collapse
- #default_page_size? ⇒ Boolean
- #filters_as_url_params ⇒ Object
-
#initialize(serializer, params = {}, scope = nil, context = {}) ⇒ Options
constructor
A new instance of Options.
- #scope_with_filters ⇒ Object
- #sorting_as_url_params ⇒ Object
Constructor Details
#initialize(serializer, params = {}, scope = nil, context = {}) ⇒ Options
Returns a new instance of Options.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/restpack_serializer/options.rb', line 7 def initialize(serializer, params = {}, scope = nil, context = {}) params.symbolize_keys! if params.respond_to?(:symbolize_keys!) @page = params[:page] ? params[:page].to_i : 1 @page_size = params[:page_size] ? params[:page_size].to_i : RestPack::Serializer.config.page_size @include = params[:include] ? params[:include].split(',') : [] @filters = filters_from_params(params, serializer) @sorting = sorting_from_params(params, serializer) @serializer = serializer @model_class = serializer.model_class @scope = scope || model_class.send(:all) @context = context @include_links = true end |
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
3 4 5 |
# File 'lib/restpack_serializer/options.rb', line 3 def context @context end |
#filters ⇒ Object
Returns the value of attribute filters.
3 4 5 |
# File 'lib/restpack_serializer/options.rb', line 3 def filters @filters end |
#include ⇒ Object
Returns the value of attribute include.
3 4 5 |
# File 'lib/restpack_serializer/options.rb', line 3 def include @include end |
#include_links ⇒ Object
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_class ⇒ Object
Returns the value of attribute model_class.
3 4 5 |
# File 'lib/restpack_serializer/options.rb', line 3 def model_class @model_class end |
#page ⇒ Object
Returns the value of attribute page.
3 4 5 |
# File 'lib/restpack_serializer/options.rb', line 3 def page @page end |
#page_size ⇒ Object
Returns the value of attribute page_size.
3 4 5 |
# File 'lib/restpack_serializer/options.rb', line 3 def page_size @page_size end |
#scope ⇒ Object
Returns the value of attribute scope.
3 4 5 |
# File 'lib/restpack_serializer/options.rb', line 3 def scope @scope end |
#serializer ⇒ Object
Returns the value of attribute serializer.
3 4 5 |
# File 'lib/restpack_serializer/options.rb', line 3 def serializer @serializer end |
#sorting ⇒ Object
Returns the value of attribute sorting.
3 4 5 |
# File 'lib/restpack_serializer/options.rb', line 3 def sorting @sorting end |
Instance Method Details
#default_page_size? ⇒ Boolean
33 34 35 |
# File 'lib/restpack_serializer/options.rb', line 33 def default_page_size? @page_size == RestPack::Serializer.config.page_size end |
#filters_as_url_params ⇒ Object
37 38 39 |
# File 'lib/restpack_serializer/options.rb', line 37 def filters_as_url_params @filters.sort.map { |k,v| map_filter_ids(k,v) }.join('&') end |
#scope_with_filters ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/restpack_serializer/options.rb', line 22 def scope_with_filters scope_filter = {} @filters.keys.each do |filter| value = query_to_array(@filters[filter]) scope_filter[filter] = value end @scope.where(scope_filter) end |
#sorting_as_url_params ⇒ Object
41 42 43 44 |
# File 'lib/restpack_serializer/options.rb', line 41 def sorting_as_url_params sorting_values = sorting.map { |k, v| v == :asc ? k : "-#{k}" }.join(',') "sort=#{sorting_values}" end |