Browse files

initial commit

  • Loading branch information...
1 parent f9dd457 commit 6eb092bc133440b53096bdacf06eeac7f94af3cc @jbr committed Aug 31, 2009
Showing with 78 additions and 0 deletions.
  1. 0 README
  2. +20 −0 README.textile
  3. +2 −0 init.rb
  4. +27 −0 lib/argonaut.rb
  5. +29 −0 lib/argonaut_template_handler.rb
View
0 README
No changes.
View
20 README.textile
@@ -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.
View
2 init.rb
@@ -0,0 +1,2 @@
+require 'argonaut_template_handler'
+ActionView::Template.register_template_handler :argonaut, ActionView::TemplateHandlers::ArgonautHandler
View
27 lib/argonaut.rb
@@ -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
View
29 lib/argonaut_template_handler.rb
@@ -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
+

0 comments on commit 6eb092b

Please sign in to comment.