Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jacob Rothstein
committed
Sep 1, 2009
1 parent
f9dd457
commit 6eb092b
Showing
5 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
h1. Argonaut is a lightweight JSON builder template handler for Rails | ||
|
||
h2. Usage | ||
|
||
It's a whole lot like .xml.builder, only it's .json.argonaut. Make an appropriately named view, like @show.json.argonaut@ and put something in it like: | ||
|
||
<pre><code> | ||
doc.list do |list| | ||
list.created_at @list.created_at | ||
list.items do |items| | ||
for item in @list.items | ||
items.item do |item_node| | ||
item_node.title item.title | ||
end | ||
end | ||
end | ||
end | ||
</code></pre> | ||
|
||
And you'll get back some nice JSON. All of the actual heavy lifting is handled by the json gem. We're really just building up a big hash with the builder pattern. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require 'argonaut_template_handler' | ||
ActionView::Template.register_template_handler :argonaut, ActionView::TemplateHandlers::ArgonautHandler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require 'json' | ||
class Argonaut | ||
instance_methods.each {|m| undef_method(m) unless %w(__id__ __send__ to_json instance_eval).include?(m.to_s)} | ||
|
||
def initialize(&blk) | ||
@hash = {} | ||
blk.call(self) | ||
end | ||
|
||
def method_missing(key, *values) | ||
if block_given? | ||
@hash[key] = Argonaut.new {|a| yield a} | ||
else | ||
@hash[key] = values.size == 1 ? values.first : values | ||
end | ||
end | ||
|
||
def to_json() @hash.to_json end | ||
|
||
def to_s | ||
@hash.to_s | ||
end | ||
|
||
def inspect | ||
@hash.inspect | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
require 'argonaut' | ||
|
||
module ActionView | ||
module TemplateHandlers | ||
class ArgonautHandler < TemplateHandler | ||
include Compilable | ||
|
||
def self.line_offset() 2 end | ||
|
||
def compile(template) | ||
STDOUT.puts 'here' | ||
<<-END_RUBY | ||
STDOUT.puts 'htere' | ||
doc = ::Argonaut.new do |doc| | ||
#{template.source} | ||
end.to_json | ||
END_RUBY | ||
end | ||
|
||
def cache_fragment(block, name = {}, options = nil) | ||
@view.fragment_for(block, name, options) do | ||
eval('doc', block.binding) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|