Class: Ea::Spa::Configuration

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

Overview

SPA-side configuration. A YAML file with the shape:

metadata:
title: ...
description: ...
version: ...
author: ...
license: ...
homepage: ...
repository: ...
ui:
title: ...
description: ...

Values under metadata override fields on the model's Ea::Model::Metadata. Values under ui (and other sections like appearance) are surfaced to the SPA view via the skeleton's metadata hash so the frontend can use them.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, data) ⇒ Configuration

Returns a new instance of Configuration.



37
38
39
40
# File 'lib/ea/spa/configuration.rb', line 37

def initialize(path, data)
  @path = path
  @data = data.is_a?(Hash) ? data : {}
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



26
27
28
# File 'lib/ea/spa/configuration.rb', line 26

def data
  @data
end

#pathObject (readonly)

Returns the value of attribute path.



26
27
28
# File 'lib/ea/spa/configuration.rb', line 26

def path
  @path
end

Class Method Details

.load(path) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/ea/spa/configuration.rb', line 28

def self.load(path)
  return nil if path.nil? || path.empty?
  unless File.exist?(path)
    raise ArgumentError, "SPA config not found: #{path}"
  end

  new(path, YAML.safe_load(File.read(path)) || {})
end

Instance Method Details

#appearanceObject



50
51
52
# File 'lib/ea/spa/configuration.rb', line 50

def appearance
  @data["appearance"] || {}
end

#apply_to_metadata(model_metadata) ⇒ Object

Apply overrides to a model Metadata instance, returning a new one. The original is left untouched.



56
57
58
59
60
61
62
# File 'lib/ea/spa/configuration.rb', line 56

def ()
  return  unless 

  merged = ()
                               .merge(stringify_keys())
  (merged)
end

#metadata_overrideObject



42
43
44
# File 'lib/ea/spa/configuration.rb', line 42

def 
  (@data["metadata"] || {}).transform_keys(&:to_sym)
end

#uiObject



46
47
48
# File 'lib/ea/spa/configuration.rb', line 46

def ui
  @data["ui"] || {}
end

#view_extrasObject

Surface the relevant config (ui, appearance) into a hash the SPA skeleton embeds in its metadata payload.



66
67
68
# File 'lib/ea/spa/configuration.rb', line 66

def view_extras
  { "ui" => ui, "appearance" => appearance }.reject { |_, v| v.nil? || v.empty? }
end