2014-05-03 12:34:36 +00:00
|
|
|
module ApplicationLib
|
|
|
|
def self.included(base)
|
|
|
|
base.extend(ClassMethods)
|
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
2013-08-15 21:15:05 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
load adapter based on setting option
|
|
|
|
|
2013-08-15 22:11:10 +00:00
|
|
|
result = self.load_adapter_by_setting( 'some_setting_with_class_name' )
|
2013-08-15 21:15:05 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
2013-08-17 16:09:06 +00:00
|
|
|
result = Some::Classname
|
2013-08-15 21:15:05 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2014-05-03 12:34:36 +00:00
|
|
|
def load_adapter_by_setting(setting)
|
|
|
|
adapter = Setting.get( setting )
|
|
|
|
return if !adapter
|
2013-08-15 20:40:22 +00:00
|
|
|
|
2014-05-03 12:34:36 +00:00
|
|
|
# load backend
|
|
|
|
self.load_adapter(adapter)
|
|
|
|
end
|
2013-08-15 21:15:05 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
load adapter
|
|
|
|
|
2013-08-17 16:09:06 +00:00
|
|
|
result = self.load_adapter( 'Some::Classname' )
|
2013-08-15 21:15:05 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
2013-08-17 16:09:06 +00:00
|
|
|
result = Some::Classname
|
2013-08-15 21:15:05 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2014-05-03 12:34:36 +00:00
|
|
|
def load_adapter(adapter)
|
|
|
|
|
|
|
|
# load adapter
|
|
|
|
|
|
|
|
# will only work on ruby 2.0
|
|
|
|
# Object.const_get(adapter)
|
2013-08-15 20:40:22 +00:00
|
|
|
|
2014-05-03 12:34:36 +00:00
|
|
|
# will work on ruby 1.9 and 2.0
|
|
|
|
# adapter.split('::').inject(Object) do |mod, class_name|
|
|
|
|
# mod.const_get(class_name)
|
|
|
|
# end
|
2013-08-17 16:09:06 +00:00
|
|
|
|
2014-05-03 12:34:36 +00:00
|
|
|
# will work with active_support
|
|
|
|
adapter = adapter.constantize
|
2013-08-17 16:09:06 +00:00
|
|
|
|
2014-05-03 12:34:36 +00:00
|
|
|
if !adapter
|
2015-05-07 11:27:07 +00:00
|
|
|
fail "Can't load adapter '#{adapter_name}'"
|
2014-05-03 12:34:36 +00:00
|
|
|
end
|
2013-08-17 16:09:06 +00:00
|
|
|
|
2014-05-03 12:34:36 +00:00
|
|
|
adapter
|
|
|
|
end
|
2013-08-15 20:40:22 +00:00
|
|
|
end
|
|
|
|
end
|