Module: Senro::QueryParamsFormatter
- Included in:
- Controller
- Defined in:
- lib/senro/query_params_formatter.rb
Class Method Summary collapse
-
.query(param) ⇒ String
Format RESTful API’s query params to SQL where clause.
-
.sort(param) ⇒ String
Format RESTful API’s query params to SQL order clause.
Class Method Details
.query(param) ⇒ String
Format RESTful API’s query params to SQL where clause.
And convert camel case attributes to snake case one.
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/senro/query_params_formatter.rb', line 31 def self.query(param) data = { query: ''.dup, status: {} } elements = param.split(' ') elements.each_with_object(data) do |ele, h| if ele.include? ':' ary = ele.split(':') h[:status][ary[0].underscore.to_sym] = Array(h[:status][ary[0].underscore.to_sym]) << ary[1] else h[:query] << (h[:query] == '' ? ele : " #{ele}") end end end |
.sort(param) ⇒ String
Format RESTful API’s query params to SQL order clause. +: asc -: desc none(default): asc
And convert camel case attributes to snake case one.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/senro/query_params_formatter.rb', line 14 def self.sort(param) attributes = param.split(',') attributes.each_with_object({}) do |attr, hash| if /^\-/.match(attr).nil? hash[attr.strip.gsub(/^\+/, '').underscore.to_sym] = 'asc' else hash[attr.strip.gsub(/^\-/, '').underscore.to_sym] = 'desc' end end end |