n_x_l
n_x_l

Reputation: 1610

Configuring Rails initializers depending on Gemfile specified platform (Ruby, jRruby..)

I currently use both Ruby and jRuby on my rails app as specified in my Gemfile:

platform :mri do
  # MRI gems here.
end

platform :jruby do
  # jRuby gems here.
end

I have only two gems on my jRuby block, so naturally most initializers (config/initializers) will be MRI specific. Is there a way I can make these initializers platform specific without having to go to each file and conditionally load it based on the platform?

Upvotes: 2

Views: 145

Answers (1)

kares
kares

Reputation: 7166

first, you should expect most initializers to be shared :) ... but if you really want this you need to implement it yourself, usually the easiest thing is to distinguish whether you're on JRuby in the initializer (or anywhere else) :

unless defined? JRUBY_VERSION
  # on MRI
end

it's probably cleaner (assuming the gems get auto-required with Bundler) to do a defined? const_defined? check in the initializer before configuring the Gem e.g. :

if defined? Sample
  # assuming gem 'sample' defines a Sample constant
end

if all of this is not enough you can have a meta-initialize that loads other initializers based on platform (I would probably rather not do that) ... something of a :

if defined? JRUBY_VERSION
  # glob, sort and load all files from initializers/mri/*.rb
else
  # glob, sort and load all files from initializers/jruby/*.rb
end

Upvotes: 1

Related Questions