trabajo-afectivo/lib/application_lib.rb

64 lines
1 KiB
Ruby
Raw Normal View History

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
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
def load_adapter_by_setting(setting)
adapter = Setting.get(setting)
return if !adapter
return if adapter.empty?
# load backend
load_adapter(adapter)
end
2013-08-15 21:15:05 +00:00
=begin
load adapter
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
def load_adapter(adapter)
# load adapter
# will only work on ruby 2.0
#Object.const_get(adapter)
# 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
# will work with active_support
adapter = adapter.constantize
2013-08-17 16:09:06 +00:00
if !adapter
fail "Can't load adapter '#{adapter_name}'"
end
2013-08-17 16:09:06 +00:00
adapter
end
end
end