rails-cached-routes 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3ce322076dded160513fbd3ad08bf642d0fdbf0b
4
+ data.tar.gz: a6d95f51b4f2a747f7ac6b19bd57355443d5a0ad
5
+ SHA512:
6
+ metadata.gz: 449a2c21e367ee27a8b2b34e86dc9282c3a3cd0f324c0c5205666e9383dbdf7fa279283ead4cd458026a8e682901a15d43af507f35750fc2d653837be354ad9e
7
+ data.tar.gz: 7e5e729f756cb5d01c407707fbbcc6a175b867ed1c9ad9f251c786de14daad981bed98aaf21e729267846ea293b105f33294b525c4121a33d01414360dec9403
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .ruby-version
19
+ .ruby-gemset
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails-cached-routes.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 David McCullars
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Rails::Cached::Routes
2
+
3
+ For large applications with extensive routes files, the expense of loading
4
+ the routes file for every command that requires the rails stack can become
5
+ painfully slow. In this case, time can be saved by marshalling the
6
+ routes to a file so that subsequent loads of the rails stack can just
7
+ unmarshal that file and be on its way.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'rails-cached-routes'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rails-cached-routes
22
+
23
+ ## Usage
24
+
25
+ No special steps are needed to utilize rails-cached-routes as the railtie
26
+ automatically adds the functionality into place. For each route file
27
+ processed by rails, a .cached file will be created on-the-fly whenever
28
+ the rails stack is loaded the first time. From then on the cached
29
+ file will be used until its timestamp becomes older than the original
30
+ routes file.
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ require 'action_dispatch/routing/redirection'
2
+
3
+ module CachedRoutes
4
+ class CachedRedirect
5
+
6
+ include ActionDispatch::Routing::Redirection
7
+
8
+ def initialize(ad_redirect)
9
+ @path = case ad_redirect
10
+ when ActionDispatch::Routing::Redirect
11
+ ad_redirect.path({}, nil)
12
+ else
13
+ ad_redirect
14
+ end
15
+ end
16
+
17
+ def to_action_dispatch_redirect
18
+ redirect(@path)
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,49 @@
1
+ module CachedRoutes
2
+ class Marshaller
3
+
4
+ attr_reader :routes_file, :cached_file
5
+
6
+ def initialize
7
+ if caller_line = caller.detect { |li| !(li =~ /cached_routes/i) }
8
+ @routes_file = caller_line.sub /:.*/, ''
9
+ @cached_file = caller_line.sub /\.rb:.*/, '.cached'
10
+ else
11
+ warn "Can not determine caller routes file. Skipping route caching."
12
+ end
13
+ end
14
+
15
+ def can_unmarshal_routes?
16
+ routes_file &&
17
+ cached_file &&
18
+ File.exist?(routes_file) &&
19
+ File.exist?(cached_file) &&
20
+ File.mtime(cached_file) >= File.mtime(routes_file)
21
+ end
22
+
23
+ def marshal_routes(new_routes)
24
+ new_routes.map! do |route|
25
+ if ActionDispatch::Routing::Redirect === route.app
26
+ route = route.clone
27
+ route.instance_variable_set :@app, CachedRedirect.new(route.app)
28
+ end
29
+ route
30
+ end
31
+ File.open(cached_file, 'wb') do |io|
32
+ Marshal.dump(new_routes, io)
33
+ end
34
+ nil
35
+ end
36
+
37
+ def unmarshal_routes(routes)
38
+ File.open(cached_file, 'rb') do |io|
39
+ Marshal.load(io).each do |route|
40
+ if CachedRedirect === route.app
41
+ route.instance_variable_set :@app, route.app.to_action_dispatch_redirect
42
+ end
43
+ routes << route
44
+ end
45
+ end
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,9 @@
1
+ module CachedRoutes
2
+ class Railtie < Rails::Railtie
3
+
4
+ initializer "add_caching_of_routes" do |app|
5
+ app.routes.singleton_class.send :include, RouteSet
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ module CachedRoutes
2
+ module RouteSet
3
+
4
+ def self.included(base)
5
+ base.alias_method_chain :draw, :caching
6
+ end
7
+
8
+ def draw_with_caching(*args, &block)
9
+ marshaller = Marshaller.new
10
+ if marshaller.can_unmarshal_routes?
11
+ marshaller.unmarshal_routes(self.set.routes)
12
+ else
13
+ routes_before = self.set.routes.clone
14
+ draw_without_caching(*args, &block)
15
+ marshaller.marshal_routes(self.set.routes - routes_before)
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module CachedRoutes
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,3 @@
1
+ Dir[File.expand_path('../cached_routes/**/*.rb', __FILE__)].each do |file|
2
+ load file
3
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cached_routes/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails-cached-routes"
8
+ spec.version = CachedRoutes::VERSION
9
+ spec.authors = ["David McCullars"]
10
+ spec.email = ["david.mccullars@gmail.com"]
11
+ spec.description = %q{Cache expensive rails routes files to speed up rails stack}
12
+ spec.summary = <<-END
13
+ For large applications with extensive routes files, the expense of loading
14
+ the routes file for every command that requires the rails stack can become
15
+ painfully slow. In this case, time can be saved by marshalling the
16
+ routes to a file so that subsequent loads of the rails stack can just
17
+ unmarshal that file and be on its way.
18
+ END
19
+ spec.homepage = ""
20
+ spec.license = "MIT"
21
+
22
+ spec.files = `git ls-files`.split($/)
23
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
24
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_dependency "rails", "> 3.2"
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.3"
30
+ spec.add_development_dependency "rake"
31
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-cached-routes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - David McCullars
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>'
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>'
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Cache expensive rails routes files to speed up rails stack
56
+ email:
57
+ - david.mccullars@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/cached_routes/cached_redirect.rb
68
+ - lib/cached_routes/marshaller.rb
69
+ - lib/cached_routes/railtie.rb
70
+ - lib/cached_routes/route_set.rb
71
+ - lib/cached_routes/version.rb
72
+ - lib/rails-cached-routes.rb
73
+ - rails-cached-routes.gemspec
74
+ homepage: ''
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.1.11
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: For large applications with extensive routes files, the expense of loading
98
+ the routes file for every command that requires the rails stack can become painfully
99
+ slow. In this case, time can be saved by marshalling the routes to a file so that
100
+ subsequent loads of the rails stack can just unmarshal that file and be on its way.
101
+ test_files: []