Skip to content

Commit e7438e7

Browse files
committed
Print a warning if outdated gem versions are used
1 parent d441ec6 commit e7438e7

5 files changed

Lines changed: 40 additions & 1 deletion

File tree

lib/multi_json.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ def adapter
8787
# * <tt>:gson</tt> (JRuby only)
8888
# * <tt>:jr_jackson</tt> (JRuby only)
8989
def use(new_adapter)
90-
@adapter = load_adapter(new_adapter)
90+
adapter = load_adapter(new_adapter)
91+
adapter.activate! if adapter.respond_to?(:activate!)
92+
@adapter = adapter
9193
end
9294
alias adapter= use
9395
alias engine= use

lib/multi_json/adapters/json_common.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ module Adapters
55
class JsonCommon < Adapter
66
defaults :load, :create_additions => false, :quirks_mode => true
77

8+
GEM_VERSION = '1.7.7'
9+
10+
def self.activate!
11+
if JSON::VERSION < GEM_VERSION
12+
Kernel.warn "You are using an old or stdlib version of #{gem_name} gem\n" +
13+
"Please upgrade to the recent version by adding this to your Gemfile:\n\n" +
14+
" gem '#{gem_name}', '~> #{GEM_VERSION}'\n"
15+
end
16+
end
17+
818
def load(string, options={})
919
string = string.read if string.respond_to?(:read)
1020

lib/multi_json/adapters/json_gem.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ module Adapters
66
# Use the JSON gem to dump/load.
77
class JsonGem < JsonCommon
88
ParseError = ::JSON::ParserError
9+
10+
def self.gem_name
11+
'json'
12+
end
913
end
1014
end
1115
end

lib/multi_json/adapters/json_pure.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ module Adapters
66
# Use JSON pure to dump/load.
77
class JsonPure < JsonCommon
88
ParseError = ::JSON::ParserError
9+
10+
def self.gem_name
11+
'json_pure'
12+
end
913
end
1014
end
1115
end

spec/multi_json_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,25 @@
5050
end
5151
end
5252

53+
context 'with stdlib version' do
54+
around do |example|
55+
version = JSON::VERSION
56+
silence_warnings{ JSON::VERSION = '1.5.5' }
57+
example.call
58+
silence_warnings{ JSON::VERSION = version }
59+
end
60+
61+
it 'should warn about json' do
62+
Kernel.should_receive(:warn).with(/'json', '~> 1.7.7'/)
63+
MultiJson.use :json_gem
64+
end
65+
66+
it 'should warb about json/pure' do
67+
Kernel.should_receive(:warn).with(/'json_pure', '~> 1.7.7'/)
68+
MultiJson.use :json_pure
69+
end
70+
end
71+
5372
it 'defaults to the best available gem' do
5473
# Clear cache variable already set by previous tests
5574
MultiJson.send(:remove_instance_variable, :@adapter)

0 commit comments

Comments
 (0)