class PackerFiles::Finder
Public Class Methods
new()
click to toggle source
Dummy constructor
# File lib/PackerFiles/OS/Finder.rb, line 10 def initialize end
Public Instance Methods
class_name(os_name, version=nil)
click to toggle source
Return the correct class name that matches the given OS name and Version
# File lib/PackerFiles/OS/Finder.rb, line 15 def class_name(os_name, version=nil) generic = [] specific = [] load_file_list(os_name) class_list(os_name).each do |cl| if !cl.respond_to? :versions next elsif cl.name.gsub('PackerFiles::','') != os_name next elsif cl.versions.empty? generic.push(cl) else specific.push(cl) if cl.versions.index(version) end end return specific.first if !specific.empty? return generic.first if !generic.empty? raise NotImplementedError.new(os_name) end
class_names()
click to toggle source
Return the list of OSes that can be found by the finder.
# File lib/PackerFiles/OS/Finder.rb, line 36 def class_names # Get list of directories under OS folder. We can find # files to load from this location. classes = PackerFiles.DirPath("OS/*/").map {|dir| Dir.glob(File.join(dir, '*.rb')) { |filename| load(filename) } begin class_list(File.basename(dir)) rescue NotImplementedError nil end } classes.flatten.keep_if {|cl| cl.respond_to? :versions} end
Private Instance Methods
class_list(os_name)
click to toggle source
Given the OS Name, return the list of classes that are defined for this OS. (Note: OS Name is a combination of class name and a module name: e.g. A::B, A => Module name B is class name.). There can be many classes that can be defined under the module, but only some are loaded if their file names match the os_name.
# File lib/PackerFiles/OS/Finder.rb, line 60 def class_list(os_name) # Module name is everything before the last '::' m_name = module_name(os_name) # Do a top-level instance eval on the object to get the # module name m_name = 'PackerFiles::' + m_name begin m_obj = Object.instance_eval(m_name) m_obj.constants.map {|c| m_obj.const_get(c) } rescue raise NotImplementedError.new(os_name) end end
load_file_list(os_name)
click to toggle source
Convert the given OS name into a relative directory and load all files under them
# File lib/PackerFiles/OS/Finder.rb, line 81 def load_file_list(os_name) PackerFiles.DirPath(os_name.gsub('::', '/') + '*.rb').each do |file| load(file) end end
module_name(os_name)
click to toggle source
Given a OS Name, return it's module name. Module name is everything before the last '::'
# File lib/PackerFiles/OS/Finder.rb, line 90 def module_name(os_name) return os_name if os_name.match('::').nil? os_name.match(/(.*::)(.*)/)[1].gsub(/::$/, '') end