Class: CLI::Mastermind::Loader

Inherits:
Object
  • Object
show all
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

PlanfileLoader

Defined Under Namespace

Classes: PlanfileLoader

Constant Summary collapse

@@loaders =
[]

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loadable_extensionsObject (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.

Parameters:

  • extension (String)

    the extension to check

Returns:

  • (Boolean)

    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

Parameters:

  • extension (String)

    the extensions to search with

Returns:

  • (Loader)

    the loader for the given extension.

Raises:



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

This method is abstract.

Used to load a given plan.

Parameters:

  • filename (String)

    the path to the planfile to be loaded

Raises:

  • (NotImplementedError)


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.

Parameters:

  • files (Array<String>)

    the list of planfiles to load

Returns:

  • (ParentPlan)

    a ParentPlan containing all the loaded plans



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_extensionsArray<String>

Returns all loadable extensions.

Returns:

  • (Array<String>)

    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