From dd5d853ce591b818acdeb81ec5e99c7294d8033d Mon Sep 17 00:00:00 2001 From: Sebastian Celis Date: Thu, 10 May 2012 14:43:29 -0500 Subject: [PATCH] Exit nicely if requiring json fails. --- lib/twine/formatters/jquery.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/twine/formatters/jquery.rb b/lib/twine/formatters/jquery.rb index 098f546..14f12f4 100644 --- a/lib/twine/formatters/jquery.rb +++ b/lib/twine/formatters/jquery.rb @@ -1,5 +1,3 @@ -require "json" - module Twine module Formatters class JQuery < Abstract @@ -28,6 +26,12 @@ module Twine end def read_file(path, lang) + begin + require "json" + rescue LoadError + raise Twine::Error.new "You must run 'gem install json' in order to read or write jquery-localize files." + end + open(path) do |io| json = JSON.load(io) json.each do |key, value| @@ -37,6 +41,12 @@ module Twine end def write_file(path, lang) + begin + require "json" + rescue LoadError + raise Twine::Error.new "You must run 'gem install json' in order to read or write jquery-localize files." + end + default_lang = @strings.language_codes[0] encoding = @options[:output_encoding] || 'UTF-8' File.open(path, "w:#{encoding}") do |f|