Skip to content

Commit

Permalink
Force Oj install and use for platforms != JRuby
Browse files Browse the repository at this point in the history
- Add a Ruby extension that will install Oj when the platform is not
  JRuby
- Rollbar::JSON will force to use Oj if it's defined. If not it'll use
  the MultiJson.current_adapter

In summary. We'll use Oj for platforms != JRuby and any other one for JRuby.
  • Loading branch information
jondeandres committed Sep 21, 2015
1 parent 63db163 commit e33f10e
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 18 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ gem "racc", :platform => :rbx
gem "minitest", :platform => :rbx
gem "rubinius-developer_tools", :platform => :rbx
gem "rails", "4.2.3"
gem 'oj', '~> 2.12.14', :platform => [:ruby, :rbx]

gemspec
gem 'byebug'
38 changes: 38 additions & 0 deletions ext/mkrf_conf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'rubygems'
require 'rubygems/command.rb'
require 'rubygems/dependency_installer.rb'

OJ_VERSION = '~> 2.12.14'

def jruby?
defined?(JRUBY_VERSION) || (defined?(RUBY_ENGINE) && 'jruby' == RUBY_ENGINE)
end

def install_oj
begin
Gem::Command.build_args = ARGV

unless jruby?
$stdout.write "Installing oj because platform is not JRuby\n"

di = Gem::DependencyInstaller.new
di.install 'oj', OJ_VERSION
end
rescue => e
warn "#{$0}: #{e}"

exit!
end
end

def write_rakefile
$stdout.write "Writing fake Rakefile\n"

# Write fake Rakefile for rake since Makefile isn't used
File.open(File.join(File.dirname(__FILE__), 'Rakefile'), 'w') do |f|
f.write('task :default' + $/)
end
end

install_oj
write_rakefile
1 change: 1 addition & 0 deletions gemfiles/rails30.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ gem 'rubysl-test-unit', :platform => :rbx
gem 'rubinius-developer_tools', :platform => :rbx
gem 'rails', '3.0.20'
gem 'hitimes', '< 1.2.2'
gem 'oj', '~> 2.12.14', :platform => [:ruby, :rbx]

gemspec :path => '../'
1 change: 1 addition & 0 deletions gemfiles/rails31.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ gem "minitest", :platform => :rbx
gem "rubysl-test-unit", :platform => :rbx
gem "rubinius-developer_tools", :platform => :rbx
gem "rails", "3.1.12"
gem 'oj', '~> 2.12.14', :platform => [:ruby, :rbx]

gemspec :path => "../"
1 change: 1 addition & 0 deletions gemfiles/rails32.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ gem "minitest", :platform => :rbx
gem "rubysl-test-unit", :platform => :rbx
gem "rubinius-developer_tools", :platform => :rbx
gem "rails", "3.2.22"
gem 'oj', '~> 2.12.14', :platform => [:ruby, :rbx]

gemspec :path => "../"
1 change: 1 addition & 0 deletions gemfiles/rails40.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ gem "minitest", :platform => :rbx
gem "rubysl-test-unit", :platform => :rbx
gem "rubinius-developer_tools", :platform => :rbx
gem "rails", "4.0.13"
gem 'oj', '~> 2.12.14', :platform => [:ruby, :rbx]

gemspec :path => "../"
1 change: 1 addition & 0 deletions gemfiles/rails41.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ gem "racc", :platform => :rbx
gem "minitest", :platform => :rbx
gem "rubinius-developer_tools", :platform => :rbx
gem "rails", "4.1.12"
gem 'oj', '~> 2.12.14', :platform => [:ruby, :rbx]

gemspec :path => "../"
1 change: 1 addition & 0 deletions gemfiles/rails42.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ gem "racc", :platform => :rbx
gem "minitest", :platform => :rbx
gem "rubinius-developer_tools", :platform => :rbx
gem "rails", "4.2.3"
gem 'oj', '~> 2.12.14', :platform => [:ruby, :rbx]

gemspec :path => "../"
34 changes: 25 additions & 9 deletions lib/rollbar/json.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,46 @@
require 'rollbar/json/oj'
require 'rollbar/json/default'

begin
require 'oj'
rescue LoadError
end

module Rollbar
module JSON
extend self

attr_writer :adapter_module
attr_writer :options_module

def dump(object)
MultiJson.dump(object, adapter_options)
with_adapter { MultiJson.dump(object, adapter_options) }
end

def load(string)
MultiJson.load(string, adapter_options)
with_adapter { MultiJson.load(string, adapter_options) }
end

def with_adapter(&block)
MultiJson.with_adapter(detect_multi_json_adapter, &block)
end

def detect_multi_json_adapter
options = {}
options[:adapter] = :oj if defined?(Oj)

MultiJson.current_adapter(options)
end

def adapter_options
adapter_module.options
options_module.options
end

def adapter_module
@adapter_module ||= find_adapter_module
def options_module
@options_module ||= find_options_module
end

def find_adapter_module
module_name = multi_json_adapter_module
def find_options_module
module_name = multi_json_adapter_module_name

begin
const_get(module_name)
Expand All @@ -40,7 +56,7 @@ def find_adapter_module
# Ex: MultiJson::Adapters::JsonGem
#
# In this method we just get the last module name.
def multi_json_adapter_module
def multi_json_adapter_module_name
MultiJson.current_adapter.name[/^MultiJson::Adapters::(.*)$/, 1]
end
end
Expand Down
3 changes: 2 additions & 1 deletion rollbar.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Gem::Specification.new do |gem|
gem.authors = ["Rollbar, Inc."]
gem.email = ["support@rollbar.com"]
gem.description = %q{Easy and powerful exception tracking for Ruby}
gem.extensions << 'ext/mkrf_conf.rb'

gem.executables = ['rollbar-rails-runner']
gem.summary = %q{Reports exceptions to Rollbar}
gem.homepage = "https://rollbar.com"
Expand All @@ -29,5 +31,4 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'rake', '>= 0.9.0'
gem.add_development_dependency 'redis'
gem.add_runtime_dependency 'multi_json'
gem.add_development_dependency 'oj', '~> 2.12.14'
end
29 changes: 24 additions & 5 deletions spec/rollbar/json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,42 @@ module MissingCustomOptions
end
end

describe '.with_adapter' do
let(:object) { double(:foo => 'bar') }
let(:callback) do
proc { object.foo }
end
let(:adapter) { described_class.detect_multi_json_adapter }

it 'calls mock.something with an adapter' do
expect(MultiJson).to receive(:with_adapter).with(adapter).and_call_original
expect(object).to receive(:foo).once

described_class.with_adapter(&callback)
end
end

describe '.detect_multi_json_adapter' do

end

describe '.adapter_options' do
it 'calls .options in adapter module' do
expect(described_class.adapter_module).to receive(:options)
expect(described_class.options_module).to receive(:options)

described_class.adapter_options
end
end

describe '.adapter_module' do
before { described_class.adapter_module = nil }
describe '.options_module' do
before { described_class.options_module = nil }

context 'with a defined rollbar adapter' do
let(:expected_adapter) { Rollbar::JSON::MockAdapter }

it 'returns the correct options' do
MultiJson.with_adapter(MultiJson::Adapters::MockAdapter) do
expect(described_class.adapter_module).to be(expected_adapter)
expect(described_class.options_module).to be(expected_adapter)
end
end
end
Expand All @@ -81,7 +100,7 @@ module MissingCustomOptions

it 'returns the correct options' do
MultiJson.with_adapter(MultiJson::Adapters::MissingCustomOptions) do
expect(described_class.adapter_module).to be(expected_adapter)
expect(described_class.options_module).to be(expected_adapter)
end
end
end
Expand Down
3 changes: 0 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
require 'database_cleaner'
require 'genspec'
require 'multi_json'
require 'oj'

MultiJson.use(:oj)

namespace :dummy do
load 'spec/dummyapp/Rakefile'
Expand Down

0 comments on commit e33f10e

Please sign in to comment.