sort_param 1.0.0 → 1.1.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 +4 -4
- data/CHANGELOG.md +3 -0
- data/README.md +13 -2
- data/lib/sort_param/definition.rb +29 -6
- data/lib/sort_param/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb0d8250b0a318bf85f2031f497b703e044387bb9b0ce53588a7bd8aefe42cc1
|
4
|
+
data.tar.gz: e48ff69e8d9f9d1eeb188222abceb7368b1fc928bf36e52b6f9822321b854782
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e99bca609530200771778839f406bf270d2fa4a11041f9f44f095f2d6e81882fbf7f814e1a14699aa7b194ed6fa907eb69cb804824c85f5bcf4c3f85ca34ed65
|
7
|
+
data.tar.gz: 884b4a6af691fb6782c9b9f4db453d5e7f904e9b4ddf309c3e07999a51e136eae363acb5dbd2c60368ed315f17e707eacd8d2aa2c21f1bc8ad85dc91e4fa5765
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -124,7 +124,18 @@ sort_param.load!("+first_name:nulls_last,-last_name:nulls_first", mode: :pg)
|
|
124
124
|
|
125
125
|
=> "first_name asc nulls last, last_name desc nulls first"
|
126
126
|
```
|
127
|
-
|
127
|
+
|
128
|
+
### Ignoring non-whitelisted sort fields instead of raising error
|
129
|
+
Use `#load` method instead:
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
sort_param = SortParam.define do
|
133
|
+
field :first_name
|
134
|
+
end
|
135
|
+
|
136
|
+
sort_param.load("+first_name,+last_name", mode: :pg)
|
137
|
+
=> "first_name asc"
|
138
|
+
```
|
128
139
|
|
129
140
|
### Render a different field name in the output
|
130
141
|
Set the `:rename` field option to output a different field name:
|
@@ -207,7 +218,7 @@ end
|
|
207
218
|
|
208
219
|
| Class | Description |
|
209
220
|
| ----------- | ----------- |
|
210
|
-
| `SortParam::UnsupportedSortField` | Raised when a sort field from the parameter isn't included in the whitelisted sort fields. |
|
221
|
+
| `SortParam::UnsupportedSortField` | Raised when loading sort string via `#load!` and a sort field from the parameter isn't included in the whitelisted sort fields. |
|
211
222
|
|
212
223
|
## Development
|
213
224
|
|
@@ -49,6 +49,20 @@ module SortParam
|
|
49
49
|
@fields_hash[name].dup
|
50
50
|
end
|
51
51
|
|
52
|
+
# Parse then translate a sort string expression and raise an error if the sort string includes
|
53
|
+
# a non-whitelisted sort field.
|
54
|
+
#
|
55
|
+
# @see #load
|
56
|
+
#
|
57
|
+
# @raise [SortParam::UnsupportedSortField] if :sort_string includes a non-whitelisted sort field.
|
58
|
+
#
|
59
|
+
def load!(sort_string, mode: :hash)
|
60
|
+
fields = Fields.new(sort_string)
|
61
|
+
validate_fields!(fields)
|
62
|
+
|
63
|
+
format_fields(mode, fields)
|
64
|
+
end
|
65
|
+
|
52
66
|
# Parse then translate a sort string expression
|
53
67
|
#
|
54
68
|
# @param sort_string [String] Sort expression. Comma-separated sort fields.
|
@@ -71,18 +85,27 @@ module SortParam
|
|
71
85
|
# @return [Hash, String, NilClass] Translated to SQL or Hash.
|
72
86
|
# Returns nil if there is no column to sort.
|
73
87
|
#
|
74
|
-
def load
|
88
|
+
def load(sort_string, mode: :hash)
|
75
89
|
fields = Fields.new(sort_string)
|
76
|
-
|
90
|
+
fields = fields.reject { |field| non_whitelisted_fields(fields).include?(field.name) }
|
77
91
|
|
78
|
-
|
79
|
-
formatter.new(self).format(*fields)
|
92
|
+
format_fields(mode, fields)
|
80
93
|
end
|
81
94
|
|
82
95
|
private
|
83
96
|
|
84
|
-
def
|
85
|
-
|
97
|
+
def format_fields(mode, fields)
|
98
|
+
Formatters::Formatter.for(mode)
|
99
|
+
.new(self)
|
100
|
+
.format(*fields)
|
101
|
+
end
|
102
|
+
|
103
|
+
def non_whitelisted_fields(loaded_fields)
|
104
|
+
loaded_fields.names - fields_hash.keys
|
105
|
+
end
|
106
|
+
|
107
|
+
def validate_fields!(loaded_fields)
|
108
|
+
unknown_field = non_whitelisted_fields(loaded_fields).first
|
86
109
|
return true if unknown_field.nil?
|
87
110
|
|
88
111
|
raise SortParam::UnsupportedSortField.new("Unsupported sort field: #{unknown_field}")
|
data/lib/sort_param/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sort_param
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Uy Jayson B
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-06-
|
11
|
+
date: 2023-06-20 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Sort records using a sort query parameter à la JSON-API style
|
14
14
|
email:
|