application_lib.rb 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. module ApplicationLib
  2. def self.included(base)
  3. base.extend(ClassMethods)
  4. end
  5. module ClassMethods
  6. =begin
  7. load adapter based on setting option
  8. result = self.load_adapter_by_setting('some_setting_with_class_name')
  9. returns
  10. result = Some::Classname
  11. =end
  12. def load_adapter_by_setting(setting)
  13. adapter = Setting.get(setting)
  14. return if !adapter
  15. return if adapter.empty?
  16. # load backend
  17. load_adapter(adapter)
  18. end
  19. =begin
  20. load adapter
  21. result = self.load_adapter('Some::Classname')
  22. returns
  23. result = Some::Classname
  24. =end
  25. def load_adapter(adapter)
  26. # load adapter
  27. # will only work on ruby 2.0
  28. #Object.const_get(adapter)
  29. # will work on ruby 1.9 and 2.0
  30. #adapter.split('::').inject(Object) do |mod, class_name|
  31. # mod.const_get(class_name)
  32. #end
  33. # will work with active_support
  34. adapter = adapter.constantize
  35. if !adapter
  36. raise "Can't load adapter '#{adapter_name}'"
  37. end
  38. adapter
  39. end
  40. end
  41. end