Skip to content

Instantly share code, notes, and snippets.

@cpetschnig
Created January 7, 2015 16:45
Show Gist options
  • Save cpetschnig/71d1323dda3276d55414 to your computer and use it in GitHub Desktop.
Save cpetschnig/71d1323dda3276d55414 to your computer and use it in GitHub Desktop.
Configuration concept
module MyEngine
class Engine < ::Rails::Engine
# Autoload from the lib directory
config.autoload_paths << File.expand_path('../../', __FILE__)
config.my_engine = MyEngine::Configuration.new
# default configuration
config.my_engine.foo.class_for_bar = MyEngine::Bar
# ...
end
end
module MyEngine
class Configuration
FooConfig = Struct.new(:class_for_bar, :proc_baz)
attr_accessor :value_0, :value_1
attr_reader :foo
def initialize
@foo = FooConfig.new
end
#...
end
end
module MyEngine
class Foo
def calculate_blah_blub
obj = bar_class.new
obj.calculate
end
private
def bar_class
Rails.application.config.my_engine.foo.class_for_bar
end
end
end
# host_app/config/initializers/my_engine.rb
HostApp::Application.configure do
config.my_engine.foo = MyOtherBetterEngine::TheOtherBar
# ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment