senro 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ad674976759b95336fbb1a2f69447f929d3fc9c92946f59d26667ff8686d31e4
4
- data.tar.gz: 2495c31d877ceb87a80669706f98ff7addde3c9704f6d52f49e415427686f706
3
+ metadata.gz: 683bde670ad39736e25d12de38a473ee568ab748d36c1c7b483fcd329578aab9
4
+ data.tar.gz: c1a3fb5c6c26e275076db973a3ff2819af9d4bc6ea5d8121c894d52ce2bae66a
5
5
  SHA512:
6
- metadata.gz: 54c8f89254370e335df9d565231d041527b30a0bcf45988eb969baa2eb388d82961f41e58e3d466c9f33d7ec7f10ab22aea1c43129e768765d2f8c46b5ddcadc
7
- data.tar.gz: 64ad5d29a171e9296b426fdffd42483d0e108743489ba5c65198114543c0e174210920e20dd3e8636112e448dddf17b29fbccb48a55e4aa974854be814eb566c
6
+ metadata.gz: 2b62578b935abfc41bf6d864c03372b53d5aca0d816e5430507d13799c647313d38a8626a535423b8c32a05a42baa7bb4e4c696a486769617d2f801431ba7e59
7
+ data.tar.gz: d3502c40b8c9b9d6577944338272fb51af19698efc9fa008ec7fe0b5b7220d6d64558f156f904e18b8ae941e7beb1eb0487b195ba9a79d8b6938a9f15c27e508
data/README.md CHANGED
@@ -20,33 +20,31 @@ Or install it yourself as:
20
20
  $ gem install senro
21
21
 
22
22
  ## Usage
23
- ### in plain Ruby
24
- #### QueryParamsFormatter#sort
25
-
26
- ```ruby
27
- puts params[:sort]
28
- # => '+id,-name'
29
-
30
- order_clause = Senro::QueryParamsFormatter.sort(params[:sort])
31
-
32
- puts order_clause
33
- # => 'id ASC, name DESC'
34
-
35
- @items = Item.all.order(order_clause)
36
- ```
37
-
38
23
  ### in Rails
39
24
 
40
25
  ```ruby
41
26
  class ApplicationController < ActionController::Base # or ::API class
42
27
  include Senro::Controller
43
28
  before_action :query_params_formatter_sort
29
+ before_action :query_params_formatter_query
44
30
  end
45
31
 
46
32
  pp params['sort']
47
33
  # => 'id ASC, name DESC'
48
34
  pp params['original_sort']
49
35
  # => 'id,-name'
36
+
37
+ # e.g.
38
+ @items = Item.all.order(params['sort'])
39
+
40
+
41
+ pp params['q']
42
+ # => { 'query' => 'senro gem', 'status' => { 'is' => ['open', 'close'] } }
43
+ pp params['original_q']
44
+ # => 'is:open is:close senro gem'
45
+
46
+ # e.g.
47
+ @items = Item.where(title: params['q']['query'], status: params['q']['status']['is'])
50
48
  ```
51
49
 
52
50
  ## Development
@@ -10,5 +10,12 @@ module Senro #:nodoc:
10
10
  params[:original_sort] = params[:sort].clone
11
11
  params[:sort] = Senro::QueryParamsFormatter.sort(params[:sort])
12
12
  end
13
+
14
+ def query_params_formatter_query
15
+ return if params[:q].nil? || params[:q] == ''
16
+
17
+ params[:original_q] = params[:q].clone
18
+ params[:q] = Senro::QueryParamsFormatter.query(params[:q])
19
+ end
13
20
  end
14
21
  end
@@ -13,13 +13,34 @@ module Senro
13
13
  # @return [String] formated stirng
14
14
  def self.sort(param)
15
15
  attributes = param.split(',')
16
- attributes.map do |attr|
16
+ attributes.map! do |attr|
17
17
  if /^\-/.match(attr).nil?
18
18
  "#{attr.strip.gsub(/^\+/, '').underscore} ASC"
19
19
  else
20
20
  "#{attr.strip.gsub(/^\-/, '').underscore} DESC"
21
21
  end
22
- end.join(', ')
22
+ end
23
+ attributes.join(', ')
24
+ end
25
+
26
+ # Format RESTful API's query params to SQL where clause.
27
+ #
28
+ # And convert camel case attributes to snake case one.
29
+ #
30
+ # @param param [String] format string. e.g. `is:open is:close senro gem`
31
+ # @return [String] formated stirng
32
+ def self.query(param)
33
+ data = { query: ''.dup, status: {} }
34
+ elements = param.split(' ')
35
+ elements.each_with_object(data) do |ele, h|
36
+ if ele.include? ':'
37
+ ary = ele.split(':')
38
+ h[:status][ary[0].underscore.to_sym] =
39
+ Array(h[:status][ary[0].underscore.to_sym]) << ary[1]
40
+ else
41
+ h[:query] << (h[:query] == '' ? ele : " #{ele}")
42
+ end
43
+ end
23
44
  end
24
45
  end
25
46
  end
data/lib/senro/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Senro
4
- VERSION = '0.3.0'
4
+ VERSION = '0.4.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: senro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - WalkerSumida
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-25 00:00:00.000000000 Z
11
+ date: 2020-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler