Class: CachedRoutes::Marshaller
- Inherits:
-
Object
- Object
- CachedRoutes::Marshaller
- Defined in:
- lib/cached_routes/marshaller.rb
Instance Attribute Summary collapse
-
#cached_file ⇒ Object
readonly
Returns the value of attribute cached_file.
-
#routes_file ⇒ Object
readonly
Returns the value of attribute routes_file.
Instance Method Summary collapse
- #can_unmarshal_routes? ⇒ Boolean
-
#initialize ⇒ Marshaller
constructor
A new instance of Marshaller.
- #marshal_routes(new_routes) ⇒ Object
- #unmarshal_routes(routes) ⇒ Object
Constructor Details
#initialize ⇒ Marshaller
Returns a new instance of Marshaller.
6 7 8 9 10 11 12 13 |
# File 'lib/cached_routes/marshaller.rb', line 6 def initialize if caller_line = caller.detect { |li| !(li =~ /cached_routes/i) } @routes_file = caller_line.sub /:.*/, '' @cached_file = caller_line.sub /\.rb:.*/, '.cached' else warn "Can not determine caller routes file. Skipping route caching." end end |
Instance Attribute Details
#cached_file ⇒ Object (readonly)
Returns the value of attribute cached_file.
4 5 6 |
# File 'lib/cached_routes/marshaller.rb', line 4 def cached_file @cached_file end |
#routes_file ⇒ Object (readonly)
Returns the value of attribute routes_file.
4 5 6 |
# File 'lib/cached_routes/marshaller.rb', line 4 def routes_file @routes_file end |
Instance Method Details
#can_unmarshal_routes? ⇒ Boolean
15 16 17 18 19 20 21 |
# File 'lib/cached_routes/marshaller.rb', line 15 def can_unmarshal_routes? routes_file && cached_file && File.exist?(routes_file) && File.exist?(cached_file) && File.mtime(cached_file) >= File.mtime(routes_file) end |
#marshal_routes(new_routes) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/cached_routes/marshaller.rb', line 23 def marshal_routes(new_routes) new_routes.map! do |route| if ActionDispatch::Routing::Redirect === route.app route = route.clone route.instance_variable_set :@app, CachedRedirect.new(route.app) end route end File.open(cached_file, 'wb') do |io| Marshal.dump(new_routes, io) end nil end |
#unmarshal_routes(routes) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cached_routes/marshaller.rb', line 37 def unmarshal_routes(routes) File.open(cached_file, 'rb') do |io| Marshal.load(io).each do |route| if CachedRedirect === route.app route.instance_variable_set :@app, route.app.to_action_dispatch_redirect end routes << route end end end |