sensible_routes 0.0.4 → 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: 5b2b6d14f70eb338bdd255790623beca7edf1c78
4
- data.tar.gz: 6dff7a06c8524339679489b0a13fec5f4100a26c
3
+ metadata.gz: dfd1aaef7634b1e7600e7182b5e7b7d4995e57fa
4
+ data.tar.gz: abfceb1ddc01aae9e6c412fdf1e4c45819631f36
5
5
  SHA512:
6
- metadata.gz: 58ede276c02d1e49b8113a875f0248681652327b76d145a4d2d8bee97a19ad4b6a0894dfe2929c75a7131d8b2aef4483fd514ce26a6b42666421a34883b0f543
7
- data.tar.gz: cdc09925a68c5eb1086f6faed37ea39cb11915bd36d790cd79b68168aeeb5a197d50c47d8c3715c196d742b5462353fcab6da390345dfaa4861161309015f155
6
+ metadata.gz: 6ca1e8254e6b61b32e404c3bbf49c745335a4311f266d668359645bad782acf7265025a341bfd5cd86f85e949974b87c523348dd16393a1f90429ab203af19a0
7
+ data.tar.gz: 31baf0a4a3aab003ac013ef94148f7743699794b82da87e7e602188daeb4e96ed2b1c15753877944aeaabf0ecf6dc97bff58c560b5e6a203f3b5ee2a6c9859e0
data/.gitignore CHANGED
File without changes
data/Gemfile CHANGED
File without changes
data/Gemfile.lock CHANGED
File without changes
data/LICENSE.txt CHANGED
File without changes
data/README.md CHANGED
@@ -1,39 +1,29 @@
1
- # Sensible::Routes
1
+ # `sensible_routes`
2
+ Simple and comprehensible route introspection library for Rails.
2
3
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sensible/routes`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
4
+ ## Huh?
5
+ In a stock Rails app, `rake routes` is essentially the only way to get any introspection of routes. There's no programmatic access to them in detail,
6
+ beyond basic route helpers. This gem aims to make detailed programmatic introspection possible.
6
7
 
7
8
  ## Installation
9
+ Add the following line to your Gemfile and run `bundle install`:
8
10
 
9
- Add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'sensible-routes'
13
- ```
14
-
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install sensible-routes
11
+ gem 'sensible_routes'
12
+
13
+ Then, create a new initializer (say `config/initializers/sensible_routes.rb`), with this as its only content:
22
14
 
15
+ SensibleRoute.hook_rails
16
+
23
17
  ## Usage
18
+ At a basic level, you can get a list of your application's routes in a useful form with this:
24
19
 
25
- TODO: Write usage instructions here
20
+ Rails.sensible_routes
21
+
22
+ That returns a SensibleRouteCollection, which is array-like in that it responds to many of the same methods. You can also filter the collection down
23
+ further - see the API documentation for full details.
26
24
 
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
- ## Contributing
34
-
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sensible-routes.
25
+ ## Contributions
26
+ Welcome. Ping me a PR. For large changes you should probably open an issue first to discuss.
36
27
 
37
28
  ## License
38
-
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
29
+ Available under the terms of the MIT license.
data/Rakefile CHANGED
File without changes
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class SensibleRoutes
4
- VERSION = '0.0.4'
4
+ VERSION = '0.1.0'
5
5
  end
@@ -1,8 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Representation of a single route. Readers:
4
+ # @path: the normalized, parameterized path for the route (i.e. /labels/12/edit)
5
+ # @url_details: controller and action, plus any non-default parameter formats as regexes
6
+ # @parameters: an array of parameter names
7
+ # @verb: HTTP verb (GET, POST, etc)
8
+ # @request_line: minus the protocol, the HTTP request line (i.e. GET /labels/12/edit).
3
9
  class SensibleRoute
4
- attr_reader :path, :url_details, :parameters, :verb
10
+ attr_reader :path, :url_details, :parameters, :verb, :request_line
5
11
 
12
+ # Initialize a new SensibleRoute.
13
+ # @param rt a Journey route, as used internally by Rails
6
14
  def initialize(rt)
7
15
  @parameters = []
8
16
 
@@ -30,13 +38,20 @@ class SensibleRoute
30
38
  @path = parts.join
31
39
  @url_details = rt.requirements
32
40
  @verb = rt.verb
41
+ @request_line = "#{@verb} #{@path}"
33
42
  @regex = Regexp.new "^#{matcher.join}$"
34
43
  end
35
44
 
45
+ # Given a path (such as /labels/12/edit), detect whether that path is a match for this route
46
+ # (i.e. the route is actually /labels/:id/edit, but /labels/12/edit is a match for that).
47
+ # @param path the path string to test
48
+ # @return boolean
36
49
  def match?(path)
37
50
  @regex.match?(path)
38
51
  end
39
52
 
53
+ # For use in gem initialization - call this once Rails is fully loaded so that Rails.sensible_routes
54
+ # can be set up.
40
55
  def self.hook_rails
41
56
  Rails.instance_eval <<EOF
42
57
  cache.delete :sensible_routes
@@ -52,7 +67,10 @@ EOF
52
67
  end
53
68
  end
54
69
 
70
+ # An array-like collection of SensibleRoutes.
55
71
  class SensibleRouteCollection
72
+ # Initialize a new route collection. For an empty collection, call with no arguments; for a
73
+ # collection built from an existing array of routes, call with the routes: option.
56
74
  def initialize(**opts)
57
75
  @routes = if opts[:routes]
58
76
  opts[:routes]
@@ -61,22 +79,35 @@ class SensibleRouteCollection
61
79
  end
62
80
  end
63
81
 
82
+ # Add a route to the collection.
83
+ # @param new the route to add
64
84
  def add(new)
65
85
  @routes << new
66
86
  end
67
87
 
88
+ # Get an array representation of the collection.
89
+ # @return Array
68
90
  def to_a
69
91
  @routes
70
92
  end
71
93
 
94
+ # Filter the collection to only those entries that match the given block.
95
+ # @param &block a block that returns true to retain an element, or false to reject it
96
+ # @return SensibleRouteCollection
72
97
  def select(&block)
73
98
  self.class.new(routes: @routes.select(&block))
74
99
  end
75
100
 
101
+ # Map over the collection, applying the given block as a transformation to each element.
102
+ # @param &block a block to apply to each element; the return value will be included in the returned array
103
+ # @return Array
76
104
  def map(&block)
77
105
  @routes.map(&block)
78
106
  end
79
107
 
108
+ # Iterate through the collection, performing the specified operation with each element.
109
+ # @param &block a block is required; it will be passed the current element as an argument
110
+ # @return self
80
111
  def each
81
112
  @routes.each do |rt|
82
113
  yield rt
@@ -84,18 +115,31 @@ class SensibleRouteCollection
84
115
  self
85
116
  end
86
117
 
118
+ # Find a match from the current route collection for a given path string. Useful for finding the current
119
+ # route being executed.
120
+ # @param path the path string to find a matching route for
121
+ # @return SensibleRoute
87
122
  def match_for(path)
88
123
  @routes.select { |rt| rt.match?(path) }.first
89
124
  end
90
125
 
126
+ # Find the route that responds to the provided url_details (action and controller names).
127
+ # @param url_details the name of a controller and action to find the route for
128
+ # @return SensibleRoute
91
129
  def route_for(**url_details)
92
130
  @routes.select { |rt| url_details <= rt.url_details }.first
93
131
  end
94
132
 
133
+ # Filter the collection down to only the routes served by the specified controller.
134
+ # @param controller the controller name whose routes should be returned
135
+ # @return SensibleRouteCollection
95
136
  def controller(controller)
96
137
  self.class.new(routes: @routes.select { |rt| rt.url_details[:controller] == controller.to_s })
97
138
  end
98
139
 
140
+ # Given a list of path strings, return a new collection with their corresponding routes removed.
141
+ # @param paths a list of path strings to find routes for and remove
142
+ # @return SensibleRouteCollection
99
143
  def without(*paths)
100
144
  routes = paths.map { |p| match_for p }
101
145
  self.class.new(routes: @routes - routes)
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sensible_routes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ArtOfCode-
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-28 00:00:00.000000000 Z
11
+ date: 2018-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -102,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
102
  version: '0'
103
103
  requirements: []
104
104
  rubyforge_project:
105
- rubygems_version: 2.6.11
105
+ rubygems_version: 2.6.12
106
106
  signing_key:
107
107
  specification_version: 4
108
108
  summary: Simple and comprehensible route introspection library for Rails.