summaryrefslogtreecommitdiff
path: root/lib/gitlab/static_site_editor/config/file_config.rb
blob: 315c603c1dd5fea2731b953671a59e4fb0deefdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# frozen_string_literal: true

module Gitlab
  module StaticSiteEditor
    module Config
      #
      # Base GitLab Static Site Editor Configuration facade
      #
      class FileConfig
        ConfigError = Class.new(StandardError)

        def initialize(yaml)
          content_hash = content_hash(yaml)
          @global = Entry::Global.new(content_hash)
          @global.compose!
        rescue Gitlab::Config::Loader::FormatError => e
          raise FileConfig::ConfigError, e.message
        end

        def valid?
          @global.valid?
        end

        def errors
          @global.errors
        end

        def to_hash_with_defaults
          # NOTE: The current approach of simply mapping all the descendents' keys and values ('config')
          #       into a flat hash may need to be enhanced as we add more complex, non-scalar entries.
          @global.descendants.map { |descendant| [descendant.key, descendant.config] }.to_h
        end

        private

        def content_hash(yaml)
          Gitlab::Config::Loader::Yaml.new(yaml).load!
        end
      end
    end
  end
end