zooplankton 0.0.1 → 0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c7d6e26e86c416d4baed08ac30d7f6c1d8517eee
4
- data.tar.gz: a23f33cd1c35d09be545df0bb7384c11a01f16e9
3
+ metadata.gz: 471752a449c9e81524d8c0ce22d8ed5f0e68cd66
4
+ data.tar.gz: 264eb30f3309d7855330c627a20fcb1ed1877abe
5
5
  SHA512:
6
- metadata.gz: 0590bdb30fa2aaa10c01c9b11cae283f00369463896353d5b52183045acfaf4d046c6d5371d516cfef6677f8f2984462f82208899aa6de187b78fc675eef635a
7
- data.tar.gz: e991454e719c1d54978a1bbbe0bd5197448144d32bddfcb32d0f7ec03eeb160318e01ee52a13dfd0cf123e386673103c08221cc74f6400a17b274dafe87135f8
6
+ metadata.gz: 7dbde1412250a6fa7d120e6243d1101277584de10f796b93f079cd5b6eaa2114136c522fd53c43dd3f8d782677c4120c047d6533210653a8a80f41c0d0398bcf
7
+ data.tar.gz: 2f9f7dc28c002a40351d2b7911b1578c70c0f06586c8b31854f5fcce87088c7c6b5a3f524f5cc70fc7d5ada6d74f66f297e5ce6d8aa5caa79c9331c6c8006542
@@ -0,0 +1,9 @@
1
+ # Changes
2
+
3
+ ## 0.1.0
4
+
5
+ * Basic functionality. - Ben Hamill
6
+
7
+ ## 0.0.1
8
+
9
+ * Let's get this party started. - Ben Hamill
@@ -1,4 +1,49 @@
1
+ require "rails"
2
+
1
3
  require "zooplankton/version"
2
4
 
3
5
  module Zooplankton
6
+ class << self
7
+ def path_template_for(helper_name, params={})
8
+ return unless named_routes.names.include?(helper_name)
9
+
10
+ unescape_template(expand_helper(helper_name, :path, params))
11
+ end
12
+
13
+ def url_template_for(helper_name, params={})
14
+ return unless named_routes.names.include?(helper_name)
15
+
16
+ unescape_template(expand_helper(helper_name, :url, params))
17
+ end
18
+
19
+ private
20
+
21
+ def expand_helper(helper_name, path_or_url, params)
22
+ helper_method = "#{helper_name}_#{path_or_url}"
23
+
24
+ url_helpers.send(helper_method, *templated_required_params_for(helper_name, params))
25
+ end
26
+
27
+ def named_routes
28
+ Rails.application.routes.named_routes
29
+ end
30
+
31
+ def route_object_for(helper_name)
32
+ named_routes.routes[helper_name]
33
+ end
34
+
35
+ def templated_required_params_for(helper_name, params)
36
+ route_object_for(helper_name).required_parts.map do |required_part|
37
+ params.fetch(required_part) { "{#{required_part}}" }
38
+ end
39
+ end
40
+
41
+ def unescape_template(template)
42
+ CGI.unescape(template)
43
+ end
44
+
45
+ def url_helpers
46
+ Rails.application.routes.url_helpers
47
+ end
48
+ end
4
49
  end
@@ -1,3 +1,3 @@
1
1
  module Zooplankton
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -23,3 +23,5 @@ RSpec.configure do |config|
23
23
  mocks.syntax = :expect
24
24
  end
25
25
  end
26
+
27
+ require 'pry'
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+ require 'zooplankton'
3
+
4
+ module Plankton
5
+ class Application < Rails::Application
6
+ end
7
+ end
8
+
9
+ Plankton::Application.routes.tap do |routes|
10
+ routes.default_url_options[:host] = 'http://example.com'
11
+ routes.draw do
12
+ root 'root#index'
13
+ get '/post/:slug', to: 'posts#show', as: :post
14
+ get '/post/:slug/comment/:comment_id', to: 'commendts#show', as: :comment
15
+ end
16
+ end
17
+
18
+ describe Zooplankton do
19
+ describe ".path_template_for" do
20
+ context "for a route with no required params" do
21
+ subject { Zooplankton.path_template_for(:root) }
22
+
23
+ it "returns just the path" do
24
+ expect(subject).to eq('/')
25
+ end
26
+ end
27
+
28
+ context "for a route with one required param" do
29
+ subject { Zooplankton.path_template_for(:post) }
30
+
31
+ it "uses simple string expansion to describe the parameter" do
32
+ expect(subject).to eq('/post/{slug}')
33
+ end
34
+ end
35
+
36
+ context "for a route with more than one required param" do
37
+ context "with no other arguments" do
38
+ subject { Zooplankton.path_template_for(:comment) }
39
+
40
+ it "uses simple string expansion to describe the parameters" do
41
+ expect(subject).to eq('/post/{slug}/comment/{comment_id}')
42
+ end
43
+ end
44
+
45
+ context "with one supplied parameter value" do
46
+ subject { Zooplankton.path_template_for(:comment, slug: 'post-slug') }
47
+
48
+ it "uses simple string expansion to describe the parameters" do
49
+ expect(subject).to eq('/post/post-slug/comment/{comment_id}')
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ describe ".url_template_for" do
56
+ context "for a route with no required params" do
57
+ subject { Zooplankton.url_template_for(:root) }
58
+
59
+ it "returns just the url" do
60
+ expect(subject).to eq('http://example.com/')
61
+ end
62
+ end
63
+
64
+ context "for a route with one required param" do
65
+ subject { Zooplankton.url_template_for(:post) }
66
+
67
+ it "uses simple string expansion to describe the parameter" do
68
+ expect(subject).to eq('http://example.com/post/{slug}')
69
+ end
70
+ end
71
+
72
+ context "for a route with more than one required param" do
73
+ context "with no other arguments" do
74
+ subject { Zooplankton.url_template_for(:comment) }
75
+
76
+ it "uses simple string expansion to describe the parameters" do
77
+ expect(subject).to eq('http://example.com/post/{slug}/comment/{comment_id}')
78
+ end
79
+ end
80
+
81
+ context "with one supplied parameter value" do
82
+ subject { Zooplankton.url_template_for(:comment, slug: 'post-slug') }
83
+
84
+ it "uses simple string expansion to describe the parameters" do
85
+ expect(subject).to eq('http://example.com/post/post-slug/comment/{comment_id}')
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -21,5 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "pry"
24
- spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "rspec", "~> 2.14"
25
+
26
+ spec.add_dependency "rails", "~> 4.0"
25
27
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zooplankton
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Hamill
@@ -56,16 +56,30 @@ dependencies:
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: '2.14'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ~>
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: '2.14'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '4.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '4.0'
69
83
  description: A library for turning Rails routes into URI Templates, like maybe for
70
84
  HAL.
71
85
  email:
@@ -76,6 +90,7 @@ extra_rdoc_files: []
76
90
  files:
77
91
  - .gitignore
78
92
  - .rspec
93
+ - CHANGES.md
79
94
  - Gemfile
80
95
  - LICENSE.txt
81
96
  - README.md
@@ -83,6 +98,7 @@ files:
83
98
  - lib/zooplankton.rb
84
99
  - lib/zooplankton/version.rb
85
100
  - spec/spec_helper.rb
101
+ - spec/zooplankton_spec.rb
86
102
  - zooplankton.gemspec
87
103
  homepage: https://github.com/benhamill/zooplankton#zooplankton
88
104
  licenses:
@@ -110,3 +126,4 @@ specification_version: 4
110
126
  summary: A library for turning Rails routes into URI Templates.
111
127
  test_files:
112
128
  - spec/spec_helper.rb
129
+ - spec/zooplankton_spec.rb