zooplankton 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +5 -0
- data/README.md +7 -0
- data/lib/zooplankton/version.rb +1 -1
- data/lib/zooplankton.rb +31 -14
- data/spec/zooplankton_spec.rb +8 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 07178fdc94e41e3d73816c73617dca152a1c2220
|
4
|
+
data.tar.gz: 202ce012feb0b347b5863219052b008096d36bf2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f81add78dbebd1b84126ace5d89e9eec047196a48d3ea33086ed8374b3a255e87b22900c92a8e76ff57938d37c209f6158404603f3aa1f88d7cb9086e201cb5
|
7
|
+
data.tar.gz: d02fa7b216e7b9c71e18bf7c9175437996e35e5422698519941fa899505afed5ea57a144b78907fefe742e5193a5c3e3b5c3ddde88307870d2c44fcf89c559f3
|
data/CHANGES.md
CHANGED
data/README.md
CHANGED
@@ -62,6 +62,13 @@ you need:
|
|
62
62
|
# => '/post/{slug}/comment/{comment_id}{?foo,bar}'
|
63
63
|
```
|
64
64
|
|
65
|
+
If you supply a query parameter for replacement, it'll denote a continuation:
|
66
|
+
|
67
|
+
``` ruby
|
68
|
+
> Zooplankton.path_template_for(:comment, %i(foo bar), slug: 'the-best-post-ever', bar: 'baz')
|
69
|
+
# => '/post/the-best-post-ever/comment/{comment_id}?bar=baz{&foo}'
|
70
|
+
```
|
71
|
+
|
65
72
|
It'll generate URLs, too, not just paths.
|
66
73
|
|
67
74
|
``` ruby
|
data/lib/zooplankton/version.rb
CHANGED
data/lib/zooplankton.rb
CHANGED
@@ -4,12 +4,12 @@ require "zooplankton/version"
|
|
4
4
|
|
5
5
|
module Zooplankton
|
6
6
|
class << self
|
7
|
-
def path_template_for(helper_name, query_params={},
|
8
|
-
build_template(:path, helper_name, *parse_params(query_params,
|
7
|
+
def path_template_for(helper_name, query_params={}, supplied_params=nil)
|
8
|
+
build_template(:path, helper_name, *parse_params(query_params, supplied_params))
|
9
9
|
end
|
10
10
|
|
11
|
-
def url_template_for(helper_name, query_params={},
|
12
|
-
build_template(:url, helper_name, *parse_params(query_params,
|
11
|
+
def url_template_for(helper_name, query_params={}, supplied_params=nil)
|
12
|
+
build_template(:url, helper_name, *parse_params(query_params, supplied_params))
|
13
13
|
end
|
14
14
|
|
15
15
|
private
|
@@ -17,34 +17,51 @@ module Zooplankton
|
|
17
17
|
def parse_params(*args)
|
18
18
|
if args.first.respond_to?(:to_h)
|
19
19
|
query_params = []
|
20
|
-
|
20
|
+
supplied_params = args.first.to_h
|
21
21
|
else
|
22
22
|
query_params = Array(args.shift)
|
23
|
-
|
23
|
+
supplied_params = args.first || {}
|
24
24
|
end
|
25
25
|
|
26
|
-
[query_params,
|
26
|
+
[query_params, supplied_params]
|
27
27
|
end
|
28
28
|
|
29
|
-
def build_template(type, helper_name, query_params,
|
29
|
+
def build_template(type, helper_name, query_params, supplied_params)
|
30
30
|
return unless named_routes.names.include?(helper_name)
|
31
31
|
|
32
|
-
escaped_template_without_query_params = expand_helper(helper_name, type,
|
33
|
-
escaped_template = append_query_params(escaped_template_without_query_params, query_params)
|
32
|
+
escaped_template_without_query_params = expand_helper(helper_name, type, supplied_params)
|
33
|
+
escaped_template = append_query_params(escaped_template_without_query_params, query_params, supplied_params)
|
34
34
|
|
35
35
|
unescape_template(escaped_template)
|
36
36
|
end
|
37
37
|
|
38
|
-
def append_query_params(template, query_params)
|
38
|
+
def append_query_params(template, query_params, supplied_params)
|
39
39
|
return template unless query_params.any?
|
40
40
|
|
41
|
-
|
41
|
+
supplied_query_params = query_params & supplied_params.keys
|
42
|
+
supplied_query_string = ''
|
43
|
+
|
44
|
+
if supplied_query_params.any?
|
45
|
+
continuation_or_expansion = '&'
|
46
|
+
|
47
|
+
supplied_query_string << '?'
|
48
|
+
|
49
|
+
supplied_query_string << supplied_query_params.map do |key|
|
50
|
+
"#{key.to_s}=#{URI.encode(supplied_params[key].to_s)}"
|
51
|
+
end.join('&')
|
52
|
+
|
53
|
+
query_params = query_params - supplied_query_params
|
54
|
+
else
|
55
|
+
continuation_or_expansion = '?'
|
56
|
+
end
|
57
|
+
|
58
|
+
"#{template}#{supplied_query_string}{#{continuation_or_expansion}#{query_params.join(',')}}"
|
42
59
|
end
|
43
60
|
|
44
61
|
def expand_helper(helper_name, path_or_url, params)
|
45
62
|
helper_method = "#{helper_name}_#{path_or_url}"
|
46
63
|
|
47
|
-
url_helpers.send(helper_method, *
|
64
|
+
url_helpers.send(helper_method, *templated_supplied_params_for(helper_name, params))
|
48
65
|
end
|
49
66
|
|
50
67
|
def named_routes
|
@@ -55,7 +72,7 @@ module Zooplankton
|
|
55
72
|
named_routes.routes[helper_name]
|
56
73
|
end
|
57
74
|
|
58
|
-
def
|
75
|
+
def templated_supplied_params_for(helper_name, params)
|
59
76
|
route_object_for(helper_name).required_parts.map do |required_part|
|
60
77
|
params.fetch(required_part) { "{#{required_part}}" }
|
61
78
|
end
|
data/spec/zooplankton_spec.rb
CHANGED
@@ -112,6 +112,14 @@ describe Zooplankton do
|
|
112
112
|
expect(subject).to eq('/post/post-slug/comment/{comment_id}{?q,f}')
|
113
113
|
end
|
114
114
|
end
|
115
|
+
|
116
|
+
context "with some query parameters that are supplied" do
|
117
|
+
subject { Zooplankton.path_template_for(:comment, %i(q f), slug: 'post-slug', q: 'find me') }
|
118
|
+
|
119
|
+
it "templateizes the query params" do
|
120
|
+
expect(subject).to eq('/post/post-slug/comment/{comment_id}?q=find me{&f}')
|
121
|
+
end
|
122
|
+
end
|
115
123
|
end
|
116
124
|
end
|
117
125
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zooplankton
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Hamill
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|