sorted 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 24619b1bb18184b31fed12d6dc314cea988201f7
4
- data.tar.gz: 5b31c5ef8375ca83aa0853e2fe219477fa40afe5
3
+ metadata.gz: 2f0ad49531eb40c3d51bc2a4950f86b72ae83ea7
4
+ data.tar.gz: 2e51b786f0cd6517b6ad3ae2f0fe51a7eb77d257
5
5
  SHA512:
6
- metadata.gz: 2a285ff4a99085d9ca9740a6e4c5187a594b14a6698750b03ec2e8846473d6f93145aedff4f1c98bfdd4aa4b32c28d395d3908ed428bbf4b62818f52f9461ff2
7
- data.tar.gz: f77377cd895a964111ff98edf7fe62e16193a8ee5f397b9613fa1db66f7a6631f861f1a0cab2fbcb622fec21cec8ec833b25b2a3b1dad528b5d3de50418b9c2e
6
+ metadata.gz: 0c1ac0dfd1f9364671961822558ab89f5363e8d5c39e6b3bdaf638eb2ad0d14dcd98205ebc7935650633e66955f582945fe08708d4085619bb9d7d775395b2bc
7
+ data.tar.gz: f74792f358763f1b61df176cf900b4581659f53fd838adf98c60c2b0606f426581e2db94311665d33ef2d9484d153295e97a225ced3d1f21bf507581e5a9a1d4
data/README.md CHANGED
@@ -35,6 +35,7 @@ Currently implemented:
35
35
  * `Sorted::SQLQuery`
36
36
  * `Sorted::URIQuery`
37
37
  * `Sorted::JSONQuery`
38
+ * `Sorted::ParamsQuery`
38
39
 
39
40
  ## Projects
40
41
 
data/lib/sorted.rb CHANGED
@@ -142,18 +142,22 @@ module Sorted
142
142
  return Set.new if raw.nil?
143
143
  raw.to_s.split(delim).inject(Set.new, &block)
144
144
  end
145
+
146
+ def parse_match(m)
147
+ [(m[2].nil? ? m[1] : m[2]), (m[3].nil? ? 'asc' : m[3].downcase)]
148
+ end
145
149
  end
146
150
 
147
151
  class URIQuery
148
152
  extend Parse
149
153
 
150
- REGEXP = /([a-zA-Z0-9._]+)_(asc|desc)$/
154
+ REGEXP = /(([a-z0-9._]+)_([asc|desc]+)|[a-z0-9._]+)/i
151
155
 
152
156
  def self.parse(raw)
153
157
  split(raw, /!/) do |set, part|
154
158
  m = part.match(REGEXP)
155
- return set unless m
156
- set << [m[1], m[2].downcase]
159
+ next unless m
160
+ set << parse_match(m)
157
161
  end
158
162
  end
159
163
 
@@ -170,8 +174,8 @@ module Sorted
170
174
  def self.parse(raw)
171
175
  split(raw, /,/) do |set, part|
172
176
  m = part.match(REGEXP)
173
- return set unless m
174
- set << [(m[2].nil? ? m[1] : m[2]), (m[3].nil? ? 'asc' : m[3].downcase)]
177
+ next unless m
178
+ set << parse_match(m)
175
179
  end
176
180
  end
177
181
 
@@ -199,4 +203,38 @@ module Sorted
199
203
  set.inject({}) { |a, e| a.merge(Hash[e[0], SORTED_TO_JSON[e[1]]]) }
200
204
  end
201
205
  end
206
+
207
+ ##
208
+ # Parses an array of decoded query params
209
+ #
210
+ # This parser/encoder uses an already decoded array of sort strings parsed by
211
+ # a URI library.
212
+ #
213
+ # Parsing:
214
+ #
215
+ # params = ['phone_desc', 'name_asc']
216
+ # set = Sorted::ParamsQuery.parse(params)
217
+ # set.to_a #=> [['phone', 'desc'], ['name', asc']]
218
+ #
219
+ # Encoding:
220
+ #
221
+ # Sorted::ParamsQuery.encode(set) #=> ['phone_desc', 'name_asc']
222
+
223
+ class ParamsQuery
224
+ extend Parse
225
+
226
+ REGEXP = /(([a-z0-9._]+)_([asc|desc]+)|[a-z0-9._]+)/i
227
+
228
+ def self.parse(params)
229
+ params.inject(Set.new) do |set, part|
230
+ m = part.match(REGEXP)
231
+ next unless m
232
+ set << parse_match(m)
233
+ end
234
+ end
235
+
236
+ def self.encode(set)
237
+ set.map { |a| a.join('_') }
238
+ end
239
+ end
202
240
  end
@@ -1,3 +1,3 @@
1
1
  module Sorted
2
- VERSION = '2.0.0'
2
+ VERSION = '2.0.1'
3
3
  end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sorted::ParamsQuery, 'decode' do
4
+ it 'should return a set from an array of params' do
5
+ params = %w(email_asc phone_asc name_desc)
6
+ set = Sorted::ParamsQuery.parse(params)
7
+ result = Sorted::Set.new([['email', 'asc'], ['phone', 'asc'], ['name', 'desc']])
8
+
9
+ expect(set).to eq(result)
10
+ end
11
+
12
+ it 'should defalut to asc if missing direction' do
13
+ params = %w(email)
14
+ set = Sorted::ParamsQuery.parse(params)
15
+ result = Sorted::Set.new([['email', 'asc']])
16
+
17
+ expect(set).to eq(result)
18
+ end
19
+ end
20
+
21
+ describe Sorted::ParamsQuery, 'encode' do
22
+ it 'should return an array of params to be used with a URL library' do
23
+ set = Sorted::Set.new([['email', 'desc'], ['name', 'desc'], ['phone', 'asc']])
24
+ result = %w(email_desc name_desc phone_asc)
25
+
26
+ expect(Sorted::ParamsQuery.encode(set)).to eq(result)
27
+ end
28
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sorted
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rufus Post
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-31 00:00:00.000000000 Z
11
+ date: 2015-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -86,6 +86,7 @@ files:
86
86
  - lib/sorted/version.rb
87
87
  - sorted.gemspec
88
88
  - spec/json_query_spec.rb
89
+ - spec/params_query_spec.rb
89
90
  - spec/set_spec.rb
90
91
  - spec/spec_helper.rb
91
92
  - spec/sql_query_spec.rb