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
|
|
|
|
|
2015-12-16 21:47: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)
|
2015-12-16 21:47:10 +00:00
|
|
|
adapter = Setting.get(setting)
|
2017-11-23 08:09:44 +00:00
|
|
|
return if adapter.blank?
|
2013-08-15 20:40:22 +00:00
|
|
|
|
2014-05-03 12:34:36 +00:00
|
|
|
# load backend
|
2015-05-07 12:10:38 +00:00
|
|
|
load_adapter(adapter)
|
2014-05-03 12:34:36 +00:00
|
|
|
end
|
2013-08-15 21:15:05 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
load adapter
|
|
|
|
|
2015-12-16 21:47:10 +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
|
2015-05-07 20:49:15 +00:00
|
|
|
#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
|
2015-05-07 20:49:15 +00:00
|
|
|
#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
|
2016-03-01 14:26:46 +00:00
|
|
|
raise "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
|