Class: Interferon::DynamicLoader
- Inherits:
-
Object
- Object
- Interferon::DynamicLoader
- Includes:
- Logging
- Defined in:
- lib/interferon/loaders.rb
Overview
this is a type of class that can dynamically load other classes based on a ‘type’ string
Direct Known Subclasses
Instance Method Summary collapse
- #get_all(sources) ⇒ Object
- #get_klass(type) ⇒ Object
-
#initialize(custom_paths) ⇒ DynamicLoader
constructor
A new instance of DynamicLoader.
-
#initialize_attributes ⇒ Object
this should be overridden in specific loaders.
Methods included from Logging
configure_logger_for, #log, #statsd
Constructor Details
#initialize(custom_paths) ⇒ DynamicLoader
Returns a new instance of DynamicLoader.
13 14 15 16 17 18 19 20 |
# File 'lib/interferon/loaders.rb', line 13 def initialize(custom_paths) @paths = [] custom_paths.each do |custom_path| @paths << File.(custom_path) end initialize_attributes end |
Instance Method Details
#get_all(sources) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/interferon/loaders.rb', line 29 def get_all(sources) instances = [] sources.each_with_index do |source, idx| type = source['type'] enabled = !!source['enabled'] = source['options'] || {} if type.nil? log.warn("#{@loader_for} ##{idx} does not have a 'type' set; 'type' is required") next end unless enabled log.info("skipping #{@loader_for} #{type} because it's not enabled") next end instance = get_klass(type).new() instances << instance end instances end |
#get_klass(type) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/interferon/loaders.rb', line 54 def get_klass(type) # figure out what we're getting based on the type filename = type.downcase type_parts = filename.split('_') class_name = type_parts.map(&:capitalize).join # ideally, we'll put a constant into this variable klass = nil # first, try getting from custom paths @paths.each do |path| full_path = "#{path}/#{@type_path}/#{filename}" begin require full_path klass = @module.const_get(class_name) rescue LoadError => e log.debug("LoadError looking for #{@loader_for} file #{type} at #{full_path}: #{e}") rescue NameError => e log.debug( "NameError looking for #{@loader_for} class #{class_name} in #{full_path}: #{e}" ) end break if klass end # if that doesn't work, try getting from this repo via require_relative if klass.nil? begin relative_filename = "./#{@type_path}/#{filename}" require_relative relative_filename klass = @module.const_get(class_name) rescue LoadError => e raise ArgumentError,\ "Loading Error; interferon does not define #{@loader_for} #{type}: #{e}" rescue NameError => e raise ArgumentError,\ "Name Error; class #{class_name} is not defined in #{relative_filename}: #{e}" end end klass end |
#initialize_attributes ⇒ Object
this should be overridden in specific loaders
23 24 25 26 27 |
# File 'lib/interferon/loaders.rb', line 23 def initialize_attributes @loader_for = 'class' @type_path = '' @module = ::Interferon end |