Class: UrlParamsManager::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/url_params_manager/service.rb

Constant Summary collapse

EMPTY_STRING =
''.freeze
PREFIX_SEPARATOR =
'-'.freeze
DEFAULT_PLACEHOLDER =
'_'.freeze
DEFAULT_MULTIPLE_SEPARATOR_FOR_POSITION =
'--'.freeze
UNITY_FUNCTION =
->(filter_params) { filter_params }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url_to_filter_params: nil, indexed_url_params_order: nil, app_url_helpers: nil, default_params: {}, always_lists_fields: {}, position_defined_url_params: nil, filter_params_pretreatment: UNITY_FUNCTION, filter_params_treatment: UNITY_FUNCTION) ⇒ Service

Returns a new instance of Service.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/url_params_manager/service.rb', line 19

def initialize(url_to_filter_params: nil,
               indexed_url_params_order: nil,
               app_url_helpers: nil,
               default_params: {},
               always_lists_fields: {},
               position_defined_url_params: nil,
               filter_params_pretreatment: UNITY_FUNCTION,
               filter_params_treatment: UNITY_FUNCTION
)
  @url_to_filter_params        = url_to_filter_params
  @filter_to_url_params        = url_to_filter_params.invert
  @indexed_url_params_order    = indexed_url_params_order.map &:to_s
  @app_url_helpers             = app_url_helpers
  @default_params              = default_params
  @position_defined_url_params = position_defined_url_params.presence || {}
  @filter_params_treatment     = filter_params_treatment
  @filter_params_pretreatment  = filter_params_pretreatment
  @always_lists_fields         = prepare_always_lists_fields(always_lists_fields)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, args) ⇒ Object

for url/path methods from filter args



40
41
42
# File 'lib/url_params_manager/service.rb', line 40

def method_missing(method, args)
  path_method method, args
end

Instance Attribute Details

#always_lists_fieldsObject (readonly)

Returns the value of attribute always_lists_fields.



9
10
11
# File 'lib/url_params_manager/service.rb', line 9

def always_lists_fields
  @always_lists_fields
end

#app_url_helpersObject (readonly)

Returns the value of attribute app_url_helpers.



9
10
11
# File 'lib/url_params_manager/service.rb', line 9

def app_url_helpers
  @app_url_helpers
end

#default_paramsObject (readonly)

Returns the value of attribute default_params.



9
10
11
# File 'lib/url_params_manager/service.rb', line 9

def default_params
  @default_params
end

#filter_params_pretreatmentObject (readonly)

Returns the value of attribute filter_params_pretreatment.



9
10
11
# File 'lib/url_params_manager/service.rb', line 9

def filter_params_pretreatment
  @filter_params_pretreatment
end

#filter_params_treatmentObject (readonly)

Returns the value of attribute filter_params_treatment.



9
10
11
# File 'lib/url_params_manager/service.rb', line 9

def filter_params_treatment
  @filter_params_treatment
end

#filter_to_url_paramsObject (readonly)

Returns the value of attribute filter_to_url_params.



9
10
11
# File 'lib/url_params_manager/service.rb', line 9

def filter_to_url_params
  @filter_to_url_params
end

#indexed_url_params_orderObject (readonly)

Returns the value of attribute indexed_url_params_order.



9
10
11
# File 'lib/url_params_manager/service.rb', line 9

def indexed_url_params_order
  @indexed_url_params_order
end

#position_defined_url_paramsObject (readonly)

Returns the value of attribute position_defined_url_params.



9
10
11
# File 'lib/url_params_manager/service.rb', line 9

def position_defined_url_params
  @position_defined_url_params
end

#url_to_filter_paramsObject (readonly)

Returns the value of attribute url_to_filter_params.



9
10
11
# File 'lib/url_params_manager/service.rb', line 9

def url_to_filter_params
  @url_to_filter_params
end

Instance Method Details

#filters_from_url_params(given_params) ⇒ Object

for filters from url/path params



45
46
47
48
49
50
51
52
# File 'lib/url_params_manager/service.rb', line 45

def filters_from_url_params(given_params)
  params  = filter_params_pretreatment.call given_params.to_h.symbolize_keys
  filters = unfold_filters(params)

  pars = filters.merge querystring_to_filters(params)

  filter_params(pars)
end

#querystring_to_filters(querystring_params) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/url_params_manager/service.rb', line 54

def querystring_to_filters(querystring_params)
  querystring_params.inject({}) do |h, (k, v)|
    new_key    = url_to_filter_params[k] || k
    h[new_key] = v
    h
  end
end