Skip to content

Commit d14c5d2

Browse files
committed
Added MultiJson.with_adapter method
1 parent cef92de commit d14c5d2

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

lib/multi_json.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ def dump(object, options={})
117117
# :nodoc:
118118
alias :encode :dump
119119

120+
def with_adapter(new_adapter)
121+
old_adapter, self.adapter = adapter, new_adapter
122+
yield
123+
ensure
124+
self.adapter = old_adapter
125+
end
126+
120127
end
121128

122129
end

spec/multi_json_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@
7878
end
7979
end
8080

81+
it 'can set adapter for a block' do
82+
MultiJson.use :ok_json
83+
MultiJson.with_adapter(:json_pure) do
84+
expect(MultiJson.adapter.name).to eq 'MultiJson::Adapters::JsonPure'
85+
end
86+
expect(MultiJson.adapter.name).to eq 'MultiJson::Adapters::OkJson'
87+
end
88+
8189
%w(json_gem json_pure nsjsonserialization oj ok_json yajl).each do |adapter|
8290
next if !macruby? && adapter == 'nsjsonserialization'
8391
next if jruby? && (adapter == 'oj' || adapter == 'yajl')

0 commit comments

Comments
 (0)