Permalink
Browse files

Add ability to set JSON backend to a class.

  • Loading branch information...
Michael Bleigh
Michael Bleigh committed Jan 21, 2010
1 parent ea69372 commit 7e9ea56da397eb52babe2915d8864058aa26a324
Showing with 20 additions and 4 deletions.
  1. +4 −2 README.rdoc
  2. +6 −2 lib/mustache_json.rb
  3. +10 −0 spec/mustache_json_spec.rb
View
@@ -50,8 +50,10 @@ toward views that can be rendered either server-side (in Ruby) or client-side
This library doesn't depend on any specific JSON library and is instead built
to be able to have a swappable JSON encoding back-end. Back-ends are settable
-by calling <tt>Mustache::JSON.backend = :new_backend</tt>. Currently it defaults
-to the JSON gem but the following back-ends are available:
+by calling <tt>Mustache::JSON.backend = :new_backend</tt>. You can also set it
+to a class, which simply expects that there be a class method <tt>encode</tt>
+that can be called with a Hash. Currently it defaults to the JSON gem but the
+following back-ends are available:
* JSON Gem (<tt>:json_gem</tt>)
* JSON Pure (<tt>:json_pure</tt>)
View
@@ -44,8 +44,12 @@ def self.backend #:nodoc:
#
# The default backend is the JSON gem.
def self.backend=(backend)
- require "mustache/json/backends/#{backend.to_s.downcase}.rb"
- @backend = class_for(backend)
+ if backend.is_a?(Class)
+ @backend = backend
+ else
+ require "mustache/json/backends/#{backend.to_s.downcase}.rb"
+ @backend = class_for(backend)
+ end
end
# Generic JSON encoder that will use the specified back-end.
View
@@ -56,6 +56,16 @@ def really_secret
@stache = MustachioedPerson.new
end
+ describe ' swappable backends' do
+ require 'mustache/json/backends/json_gem'
+ it 'should accept a strings, symbols, and classes' do
+ [:json_gem, 'json_gem', Mustache::JSON::Backends::JsonGem].each do |backend|
+ Mustache::JSON.backend = backend
+ Mustache::JSON.backend.should == Mustache::JSON::Backends::JsonGem
+ end
+ end
+ end
+
[:json_gem, :json_pure, :yajl, :active_support].each do |backend|
describe " with the #{backend} backend" do
before do

0 comments on commit 7e9ea56

Please sign in to comment.