Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Rothstein committed Sep 1, 2009
1 parent f9dd457 commit 6eb092b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
Empty file removed README
Empty file.
20 changes: 20 additions & 0 deletions 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.
2 changes: 2 additions & 0 deletions init.rb
@@ -0,0 +1,2 @@
require 'argonaut_template_handler'
ActionView::Template.register_template_handler :argonaut, ActionView::TemplateHandlers::ArgonautHandler
27 changes: 27 additions & 0 deletions 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
29 changes: 29 additions & 0 deletions 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.