Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Use a Tilt::Cache to cache rabl files sources #3

Merged
merged 1 commit into from almost 2 years ago

2 participants

Brendon Murphy Alexey Gaziev
Brendon Murphy

It's preferred to not hit the filesystem everytime you need a
template. I used tilt cache since sinatra depends on tilt. I
didn't mess with the gemspec in this case.

Brendon Murphy Use a Tilt::Cache to cache rabl files sources
It's preferred to not hit the filesystem everytime you need a
template.  I used tilt cache since sinatra depends on tilt.  I
didn't mess with the gemspec in this case.
d6309c7
Alexey Gaziev
Owner
gazay commented May 02, 2012

Cool! Thanks for pull!

Alexey Gaziev gazay closed this May 02, 2012
Alexey Gaziev gazay reopened this May 02, 2012
Alexey Gaziev gazay merged commit 21e045b into from May 02, 2012
Alexey Gaziev gazay closed this May 02, 2012
Alexey Gaziev
Owner
gazay commented May 02, 2012

omg, I pushed wrong button o_O

Brendon Murphy

No worries. I'm looking forward to trying this in a project I started last weekend! I had my own helper around rabl that worked but this feels better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

May 01, 2012
Brendon Murphy Use a Tilt::Cache to cache rabl files sources
It's preferred to not hit the filesystem everytime you need a
template.  I used tilt cache since sinatra depends on tilt.  I
didn't mess with the gemspec in this case.
d6309c7
This page is out of date. Refresh to see the latest.
10  lib/gon/sinatra/rabl.rb
... ...
@@ -1,11 +1,19 @@
1 1
 require 'rabl'
  2
+require 'tilt'
2 3
 
3 4
 module Gon
4 5
   module Sinatra
5 6
     module Rabl
6 7
       class << self
  8
+        def cache
  9
+          @cache ||= Tilt::Cache.new
  10
+        end
  11
+
7 12
         def parse_rabl(rabl_path, controller)
8  
-          source = File.read(rabl_path)
  13
+          source = cache.fetch(rabl_path) do
  14
+            File.read(rabl_path)
  15
+          end
  16
+
9 17
           rabl_engine = ::Rabl::Engine.new(source, :format => 'json')
10 18
           output = rabl_engine.render(controller, {})
11 19
           ::Rabl.configuration.json_engine.decode(output)
12  spec/gon/gon_spec.rb
@@ -69,6 +69,18 @@ def app
69 69
     @gon.objects.length.should == 2
70 70
   end
71 71
 
  72
+  it 'caches the rabl template' do
  73
+    @gon.clear
  74
+    @objects = [1,2]
  75
+    @gon.rabl 'spec/test_data/sample.rabl', :instance => self
  76
+
  77
+    File.should_not_receive(:read)
  78
+    @gon.clear
  79
+    @objects = [1,2,3]
  80
+    @gon.rabl 'spec/test_data/sample.rabl', :instance => self
  81
+    @gon.objects.length.should == 3
  82
+  end
  83
+
72 84
   def request
73 85
     @request ||= double 'request', :env => {}
74 86
   end
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.