Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Basic rendering working

  • Loading branch information...
commit 7a802fc23c9c2463dd7dca92de92b9d8c86bcb01 1 parent 497c2ba
Paul Sadauskas authored
67 lib/rest-json-template.rb
... ... @@ -0,0 +1,67 @@
  1 +require 'active_support/ordered_hash'
  2 +require 'tilt'
  3 +require 'yajl'
  4 +
  5 +
  6 +module RestJson
  7 +
  8 + class Template < Tilt::Template
  9 +
  10 + def compile!
  11 + end
  12 +
  13 + def evaluate(scope, locals, &block)
  14 + scope.instance_eval(data)
  15 + end
  16 + end
  17 + Tilt.register('restjson', Template)
  18 +
  19 + module Helper
  20 + def restjson_for(resource, &blk)
  21 + builder = RestJson::Builder.new(resource)
  22 + yield builder
  23 + builder.render
  24 + end
  25 + end
  26 +
  27 + class Builder
  28 +
  29 + def initialize(resource)
  30 + @resource = resource
  31 + @attributes = ActiveSupport::OrderedHash.new
  32 + set_keys!
  33 + end
  34 +
  35 + def set_keys!
  36 + @attributes[:_type] = @resource.class.to_s.split('::').last
  37 + end
  38 +
  39 + def href(href)
  40 + @attributes[:href] = href
  41 + end
  42 +
  43 + def attributes(*keys)
  44 + keys = keys.flatten
  45 + if keys.first.is_a?(Hash)
  46 + @attributes.merge!(keys.first)
  47 + else
  48 + keys.each do |name|
  49 + @attributes[name] = @resource.send(name)
  50 + end
  51 + end
  52 + end
  53 + alias attribute attributes
  54 +
  55 + def link_to(name, url)
  56 + name = name.to_s
  57 + name << "_href" unless name =~ /_href\Z/
  58 + attribute(name => url)
  59 + end
  60 +
  61 + def render
  62 + Yajl::Encoder.encode(@attributes)
  63 + end
  64 +
  65 + end
  66 +
  67 +end
43 spec/rest-json-template_spec.rb
... ... @@ -1,7 +1,46 @@
1 1 require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2 2
3 3 describe "RestJsonTemplate" do
4   - it "fails" do
5   - fail "hey buddy, you should probably rename this file and start specing for real"
  4 +
  5 + class Article < Struct.new(:id, :title, :text); end
  6 +
  7 + class Scope
  8 + include RestJson::Helper
  9 + end
  10 +
  11 + def scope
  12 + scope = Scope.new
  13 + scope.instance_variable_set(:@article, article)
  14 + scope
  15 + end
  16 +
  17 + def article
  18 + @article = Article.new(42, "The Answer", "Life, the Universe, and Everything")
6 19 end
  20 +
  21 + def data
  22 + @data ||= File.read(__FILE__).split("\n__END__\n").last
  23 + end
  24 +
  25 + def template
  26 + @template ||= RestJson::Template.new('test.json.restjson') { data }
  27 + end
  28 +
  29 +
  30 + it 'should do something' do
  31 + text = template.render(scope)
  32 + pp text
  33 + end
  34 +
  35 +end
  36 +
  37 +__END__
  38 +restjson_for(@article) do |json|
  39 +
  40 + json.href "http://example.com/articles/#{@article.id}"
  41 +
  42 + json.attributes :title, :text
  43 +
  44 + json.link_to :comments, "http://example.com/articles/#{@article.id}/comments"
  45 +
7 46 end
1  spec/spec_helper.rb
@@ -3,6 +3,7 @@
3 3 require 'rest-json-template'
4 4 require 'spec'
5 5 require 'spec/autorun'
  6 +require 'pp'
6 7
7 8 Spec::Runner.configure do |config|
8 9

0 comments on commit 7a802fc

Please sign in to comment.
Something went wrong with that request. Please try again.