cache_flow 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.rdoc +26 -55
  2. data/VERSION +1 -1
  3. data/cache_flow.gemspec +1 -1
  4. metadata +1 -1
@@ -1,77 +1,48 @@
1
- = vestal_versions
1
+ = cache_flow
2
2
 
3
- Finally, DRY ActiveRecord versioning!
3
+ Drop-dead simple XML fragment caching in Builder templates!
4
4
 
5
- <tt>acts_as_versioned</tt>[http://github.com/technoweenie/acts_as_versioned] by technoweenie[http://github.com/technoweenie] was a great start, but it failed to keep up with ActiveRecord's introduction of dirty objects in version 2.1. Additionally, each versioned model needs its own versions table that duplicates most of the original table's columns. The versions table is then populated with records that often duplicate most of the original record's attributes. All in all, not very DRY.
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
- <tt>simply_versioned</tt>[http://github.com/mmower/simply_versioned] by mmower[http://github.com/mmower] started to move in the right direction by removing a great deal of the duplication of acts_as_versioned. It requires only one versions table and no changes whatsoever to existing models. Its versions table stores all of the model attributes as a YAML hash in a single text column. But we could be DRYer!
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
- <tt>vestal_versions</tt>[http://github.com/laserlemon/vestal_versions] keeps in the spirit of consolidating to one versions table, polymorphically associated with its parent models. But it goes one step further by storing a serialized hash of _only_ the models' changes. Think modern version control systems. By traversing the record of changes, the models can be reverted to any point in time.
10
-
11
- And that's just what <tt>vestal_versions</tt> does. Not only can a model be reverted to a previous version number but also to a date or time!
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 'vestal_versions'
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 version an ActiveRecord model, simply add <tt>versioned</tt> to your class like so:
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
- class User < ActiveRecord::Base
35
- versioned
36
-
37
- validates_presence_of :first_name, :last_name
38
-
39
- def name
40
- "#{first_name} #{last_name}"
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! Now watch it in action...
45
-
46
- >> u = User.create(:first_name => 'Steve', :last_name => 'Richert')
47
- => #<User first_name: "Steve", last_name: "Richert">
48
- >> u.version
49
- => 1
50
- >> u.update_attribute(:first_name, 'Stephen')
51
- => true
52
- >> u.name
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.0
1
+ 0.1.1
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cache_flow}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["laserlemon"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cache_flow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - laserlemon