Class: Ea::Spa::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/ea/spa/generator.rb

Overview

Orchestrator: takes an Ea::Model::Document and an output mode, dispatches to the matching Output::Strategy. Single source of truth for which modes exist; new modes = one entry in OUTPUT_STRATEGIES.

Constant Summary collapse

OUTPUT_STRATEGIES =
{
  single_file: Output::SingleFileStrategy,
  sharded: Output::ShardedMultiFileStrategy
}.freeze
DEFAULT_MODE =
:single_file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, output:, mode: nil, shard_url_for: nil, configuration: nil) ⇒ Generator

Returns a new instance of Generator.



19
20
21
22
23
24
25
26
# File 'lib/ea/spa/generator.rb', line 19

def initialize(document, output:, mode: nil, shard_url_for: nil,
               configuration: nil)
  @document = document
  @output_path = output
  @mode = (mode || DEFAULT_MODE).to_sym
  @shard_url_for = shard_url_for
  @configuration = configuration
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



17
18
19
# File 'lib/ea/spa/generator.rb', line 17

def configuration
  @configuration
end

#documentObject (readonly)

Returns the value of attribute document.



17
18
19
# File 'lib/ea/spa/generator.rb', line 17

def document
  @document
end

#modeObject (readonly)

Returns the value of attribute mode.



17
18
19
# File 'lib/ea/spa/generator.rb', line 17

def mode
  @mode
end

#output_pathObject (readonly)

Returns the value of attribute output_path.



17
18
19
# File 'lib/ea/spa/generator.rb', line 17

def output_path
  @output_path
end

#shard_url_forObject (readonly)

Returns the value of attribute shard_url_for.



17
18
19
# File 'lib/ea/spa/generator.rb', line 17

def shard_url_for
  @shard_url_for
end

Instance Method Details

#generateObject



28
29
30
# File 'lib/ea/spa/generator.rb', line 28

def generate
  strategy_class.new(output_path).render(projector)
end

#projectorObject



32
33
34
35
36
# File 'lib/ea/spa/generator.rb', line 32

def projector
  @projector ||= Projector.new(document,
                                shard_url_for: shard_url_for,
                                configuration: configuration)
end

#strategy_classObject



38
39
40
41
42
# File 'lib/ea/spa/generator.rb', line 38

def strategy_class
  OUTPUT_STRATEGIES[mode] ||
    raise(ArgumentError, "Unknown SPA mode: #{mode.inspect}. " \
                          "Use :single_file or :sharded.")
end