From 6eb092bc133440b53096bdacf06eeac7f94af3cc Mon Sep 17 00:00:00 2001 From: Jacob Rothstein Date: Mon, 31 Aug 2009 21:30:34 -0700 Subject: [PATCH] initial commit --- README | 0 README.textile | 20 ++++++++++++++++++++ init.rb | 2 ++ lib/argonaut.rb | 27 +++++++++++++++++++++++++++ lib/argonaut_template_handler.rb | 29 +++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+) delete mode 100644 README create mode 100644 README.textile create mode 100644 init.rb create mode 100644 lib/argonaut.rb create mode 100644 lib/argonaut_template_handler.rb diff --git a/README b/README deleted file mode 100644 index e69de29..0000000 diff --git a/README.textile b/README.textile new file mode 100644 index 0000000..76bcd35 --- /dev/null +++ b/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: + +

+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
+
+ +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. diff --git a/init.rb b/init.rb new file mode 100644 index 0000000..15f2734 --- /dev/null +++ b/init.rb @@ -0,0 +1,2 @@ +require 'argonaut_template_handler' +ActionView::Template.register_template_handler :argonaut, ActionView::TemplateHandlers::ArgonautHandler diff --git a/lib/argonaut.rb b/lib/argonaut.rb new file mode 100644 index 0000000..a7e19d8 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/lib/argonaut_template_handler.rb b/lib/argonaut_template_handler.rb new file mode 100644 index 0000000..09f4a5d --- /dev/null +++ b/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 +