Module: Solanum::Config
- Defined in:
- lib/solanum/config.rb
Class Method Summary collapse
-
.clear_type_cache! ⇒ Object
Helper method to clear the type cache.
-
.construct_type(namespace, type, args) ⇒ Object
Resolves a type config string and constructs a new instance of it.
-
.load_file(path) ⇒ Object
Load the given configuration file.
-
.resolve_type(namespace, type, lib_path = nil, class_name = nil) ⇒ Object
Resolve a type based on a library path.
Class Method Details
.clear_type_cache! ⇒ Object
Helper method to clear the type cache.
7 8 9 |
# File 'lib/solanum/config.rb', line 7 def self.clear_type_cache! @@type_classes = {} end |
.construct_type(namespace, type, args) ⇒ Object
Resolves a type config string and constructs a new instance of it. Memoizes the results of loading the class in the ‘@@type_classes` field.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/solanum/config.rb', line 44 def self.construct_type(namespace, type, args) cls = resolve_type(namespace, type, args['lib_path'], args['class']) if cls.nil? STDERR.puts "Skipping construction of failed #{namespace} type #{type}" nil else begin #puts "#{cls}.new(#{args.inspect})" # DEBUG cls.new(args) rescue => e STDERR.puts "Error constructing #{namespace} type #{type}: #{args.inspect} #{e}" nil end end end |
.load_file(path) ⇒ Object
Load the given configuration file. Returns a map with initialized :sources and :outputs.
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 |
# File 'lib/solanum/config.rb', line 63 def self.load_file(path) config = File.open(path) {|f| YAML.load(f) } defaults = config['defaults'] || {} # Construct sources from config. source_configs = config['sources'] || [] sources = source_configs.map do |conf| self.construct_type('source', conf['type'], conf) end sources.reject!(&:nil?) # Construct outputs from config. output_configs = config['outputs'] || [] outputs = output_configs.map do |conf| self.construct_type('output', conf['type'], conf) end outputs.reject!(&:nil?) { defaults: defaults, sources: sources, outputs: outputs, } end |
.resolve_type(namespace, type, lib_path = nil, class_name = nil) ⇒ Object
Resolve a type based on a library path.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/solanum/config.rb', line 13 def self.resolve_type(namespace, type, lib_path=nil, class_name=nil) @@type_classes ||= {} type_key = "#{namespace}:#{type}" return @@type_classes[type_key] if @@type_classes.include?(type_key) lib_path ||= type.include?('/') ? type : "solanum/#{namespace}/#{type}" if class_name cls_path = class_name.split('::') else cls_path = lib_path.split('/').map {|w| w.capitalize } end begin require lib_path cls = cls_path.inject(Object) do |mod, class_name| mod.const_get(class_name) if mod end STDERR.puts "Unable to resolve class #{cls_path.join('::')}" unless cls @@type_classes[type_key] = cls rescue LoadError => e STDERR.puts "Unable to load code for #{type_key} type: #{e}" @@type_classes[type_key] = nil end @@type_classes[type_key] end |