class CLI::Mastermind::Loader
Loader
handles loading planfiles (not
masterplans) and is used directly by Mastermind
. Subclasses handle the actual parsing of plans, this class is primarily concerned with finding the correct loader to load a particular file.
Loader
subclasses are automatically added to the list of loaders. Thus, adding a new loader is as simple as subclassing and adding the appropriate methods.
Attributes
Public Class Methods
@param extension [String] the extension to check @return [Boolean] if the extension
is loadable
# File lib/cli/mastermind/loader.rb, line 39 def can_load?(extension) @loadable_extensions.include? extension end
Finds the correct loader for a given extension
@param extension [String] the extensions to search with @raise [UnsupportedFileTypeError] if no compatible loader is found @return [Loader] the loader for the given extension
.
# File lib/cli/mastermind/loader.rb, line 24 def find_loader(extension) loader = @@loaders.find { |l| l.can_load? extension } raise UnsupportedFileTypeError.new(extension) unless loader loader end
Adds a newly subclasses loader into the set of loaders.
# File lib/cli/mastermind/loader.rb, line 15 def inherited(subclass) @@loaders << subclass end
@abstract Used to load a given plan.
@param filename [String] the path to the planfile to be loaded
# File lib/cli/mastermind/loader.rb, line 47 def load(filename) raise NotImplementedError end
Loads plans from the filesystem.
The returned ParentPlan
is intended for use by Mastermind
itself.
@param files [Array<String>] the list of planfiles to load @return [ParentPlan] a ParentPlan
containing all the loaded plans @private
# File lib/cli/mastermind/loader.rb, line 58 def load_all(files) temp_plan = ParentPlan.new('INTERNAL PLAN HOLDER') plans = files.map do |file| ext = File.extname(file) loader = Loader.find_loader(ext) temp_plan.add_children loader.load(file) end temp_plan end
@return [Array<String>] all loadable extensions
# File lib/cli/mastermind/loader.rb, line 33 def supported_extensions @@loaders.flat_map { |l| l.loadable_extensions } end