cache_flow 0.1.0 → 0.1.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.
- data/README.rdoc +26 -55
- data/VERSION +1 -1
- data/cache_flow.gemspec +1 -1
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -1,77 +1,48 @@
|
|
1
|
-
=
|
1
|
+
= cache_flow
|
2
2
|
|
3
|
-
|
3
|
+
Drop-dead simple XML fragment caching in Builder templates!
|
4
4
|
|
5
|
-
|
5
|
+
How is it that RESTful web services riding Rails are such a big thing and there doesn't seem to be one definitive, up-to-date reference as to how to cache the XML output? Well <tt>cache_flow</tt> was built to solve that very problem... and _surprisingly_ easily!
|
6
6
|
|
7
|
-
|
7
|
+
You may be saying to yourself, "Yes, Rails web services are cool, but only if you're using ActiveRecord::Base#to_xml." Agree to disagree. The <tt>to_xml</tt> approach is just fine, but it seems like scaffolding. I believe there are some serious advantages to building your XML in the view:
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
1. It's the view! You wouldn't build your HTML in the model, so why your XML? (See http://bit.ly/rails-models-are-views)
|
10
|
+
2. Flexibility. Name your XML nodes anything you want. You're not tied down to your column (or even method) names.
|
11
|
+
3. And now... caching! Cache your XML output like you would your ERB views.
|
12
12
|
|
13
13
|
== Installation
|
14
14
|
|
15
15
|
In <tt>environment.rb</tt>:
|
16
16
|
|
17
17
|
Rails::Initializer.run do |config|
|
18
|
-
config.gem '
|
18
|
+
config.gem 'cache_flow'
|
19
19
|
end
|
20
20
|
|
21
21
|
At your application root, run:
|
22
22
|
|
23
23
|
$ sudo rake gems:install
|
24
24
|
|
25
|
-
Next, generate and run the first and last versioning migration you'll ever need:
|
26
|
-
|
27
|
-
$ script/generate vestal_versions_migration
|
28
|
-
$ rake db:migrate
|
29
|
-
|
30
25
|
== Example
|
31
26
|
|
32
|
-
To
|
27
|
+
To cache a chunk of XML, use the one method that <tt>cache_flow</tt> adds to your Builder instance, aptly named <tt>cache!</tt>:
|
33
28
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
29
|
+
xml.users, :type => 'array' do
|
30
|
+
@users.each do |user|
|
31
|
+
xml.cache!(user) do
|
32
|
+
xml.user do
|
33
|
+
xml.first_name user.first_name
|
34
|
+
xml.last_name user.last_name
|
35
|
+
end
|
36
|
+
end
|
41
37
|
end
|
42
38
|
end
|
43
39
|
|
44
|
-
It's that easy!
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
=> "Stephen Richert"
|
54
|
-
>> u.version
|
55
|
-
=> 2
|
56
|
-
>> u.revert_to(:first)
|
57
|
-
=> 1
|
58
|
-
>> u.name
|
59
|
-
=> "Steve Richert"
|
60
|
-
>> u.version
|
61
|
-
=> 1
|
62
|
-
>> u.save
|
63
|
-
=> true
|
64
|
-
>> u.version
|
65
|
-
=> 3
|
66
|
-
>> u.update_attribute(:last_name, 'Jobs')
|
67
|
-
=> true
|
68
|
-
>> u.name
|
69
|
-
=> "Steve Jobs"
|
70
|
-
>> u.version
|
71
|
-
=> 4
|
72
|
-
>> u.revert_to!(2)
|
73
|
-
=> true
|
74
|
-
>> u.name
|
75
|
-
=> "Stephen Richert"
|
76
|
-
>> u.version
|
77
|
-
=> 5
|
40
|
+
It's that easy! The <tt>cache!</tt> method takes the same arguments as the <tt>cache</tt> method we all know and love from our ERB views. So in the example above, <tt>user.cache_key</tt> will be automatically used as the cache key. You get all the same goodies as Rails' built-in fragment caching.
|
41
|
+
|
42
|
+
== How It Works
|
43
|
+
|
44
|
+
Behind the scenes, <tt>cache_flow</tt> is using the same mechanism that's used for caching ERB fragments, only using <tt>XmlMarkup#target!</tt> as its buffer rather than <tt>ActionView::Base#output_buffer</tt>. I'd like to take more credit, but it really _is_ that simple.
|
45
|
+
|
46
|
+
== To-Do
|
47
|
+
|
48
|
+
* Write tests. Any takers? I'm not very familiar with testing the view layer.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/cache_flow.gemspec
CHANGED