Skip to content

Commit

Permalink
[json_engine] Add comments and more specific rescue and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nesquena committed Sep 10, 2012
1 parent 0b488c8 commit 3076232
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
19 changes: 15 additions & 4 deletions lib/rabl/json_engine.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Defines the default JSON engine for RABL when rendering JSON is invoked on a template.
# You can define your own json engine by creating an object that responds to the `encode` method
# and setting the corresponding configuration option:
#
# config.json_engine = ActiveSupport::JSON
#

require 'multi_json'
require 'singleton'

Expand All @@ -13,10 +20,14 @@ def initialize

def set(engine_name_or_class)
@current_engine = begin
MultiJson.respond_to?(:use) ? MultiJson.use(engine_name_or_class) : MultiJson.engine = engine_name_or_class
rescue
MultiJson.respond_to?(:use) ?
MultiJson.use(engine_name_or_class) :
MultiJson.engine = engine_name_or_class
rescue RuntimeError => e #
# Re-raise if engine_name_or_class is invalid
raise e unless engine_name_or_class.respond_to?(:encode)
engine_name_or_class
end
end
end
end
end # JsonEngine
end # Rabl
13 changes: 10 additions & 3 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require File.expand_path('../teststrap', __FILE__)
require File.expand_path('../../lib/rabl', __FILE__)

context 'Rabl::Configuration' do
context 'defaults' do
Expand All @@ -24,7 +23,7 @@
end

asserts('uses a custom JSON engine') { topic.json_engine.to_s =~ /MultiJson.*::Yajl/ }
end
end # custom json, symbol

context 'custom JSON engine configured as Class' do
setup do
Expand All @@ -34,5 +33,13 @@
end

asserts('uses a custom JSON engine') { topic.json_engine.to_s == 'ActiveSupport::JSON' }
end
end # custom JSON, class

context 'invalid JSON engine configured' do
asserts {
Rabl.configure do |c|
c.json_engine = Kernel
end
}.raises(RuntimeError)
end # invalid
end

0 comments on commit 3076232

Please sign in to comment.