Class: CLI::Mastermind::Loader
- Inherits:
-
Object
- Object
- CLI::Mastermind::Loader
- Defined in:
- lib/cli/mastermind/loader.rb,
lib/cli/mastermind/loader/planfile_loader.rb
Overview
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.
Direct Known Subclasses
Defined Under Namespace
Classes: PlanfileLoader
Constant Summary collapse
- @@loaders =
[]
Class Attribute Summary collapse
-
.loadable_extensions ⇒ Object
readonly
Returns the value of attribute loadable_extensions.
Class Method Summary collapse
-
.can_load?(extension) ⇒ Boolean
If the
extension
is loadable. -
.find_loader(extension) ⇒ Loader
Finds the correct loader for a given extension.
-
.inherited(subclass) ⇒ Object
Adds a newly subclasses loader into the set of loaders.
-
.load(filename) ⇒ Object
abstract
Used to load a given plan.
-
.load_all(files) ⇒ ParentPlan
Loads plans from the filesystem.
-
.supported_extensions ⇒ Array<String>
All loadable extensions.
Class Attribute Details
.loadable_extensions ⇒ Object (readonly)
Returns the value of attribute loadable_extensions.
11 12 13 |
# File 'lib/cli/mastermind/loader.rb', line 11 def loadable_extensions @loadable_extensions end |
Class Method Details
.can_load?(extension) ⇒ Boolean
Returns if the extension
is loadable.
39 40 41 |
# File 'lib/cli/mastermind/loader.rb', line 39 def can_load?(extension) @loadable_extensions.include? extension end |
.find_loader(extension) ⇒ Loader
Finds the correct loader for a given extension
24 25 26 27 28 29 30 |
# 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 |
.inherited(subclass) ⇒ Object
Adds a newly subclasses loader into the set of loaders.
15 16 17 |
# File 'lib/cli/mastermind/loader.rb', line 15 def inherited(subclass) @@loaders << subclass end |
.load(filename) ⇒ Object
Used to load a given plan.
47 48 49 |
# File 'lib/cli/mastermind/loader.rb', line 47 def load(filename) raise NotImplementedError end |
.load_all(files) ⇒ ParentPlan
Loads plans from the filesystem.
The returned ParentPlan is intended for use by Mastermind itself.
58 59 60 61 62 63 64 65 66 67 68 |
# 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 |
.supported_extensions ⇒ Array<String>
Returns all loadable extensions.
33 34 35 |
# File 'lib/cli/mastermind/loader.rb', line 33 def supported_extensions @@loaders.flat_map { |l| l.loadable_extensions } end |