From 336519b21da62656278370ce87e5b6b216a207bf Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Wed, 8 May 2024 21:31:55 +0300 Subject: [PATCH] Refactor CodeOcean::Config class The new architecture memorizes settings (which we mostly did after reading the config so far) and also exposes the resulting file path as well as further settings. This change is a prerequisite to define a dependency with Sprockets. --- app/assets/javascripts/editor/editor.js.erb | 2 +- lib/code_ocean/config.rb | 12 ++++++++---- lib/runner/strategy/docker_container_pool.rb | 2 +- spec/lib/code_ocean/config_spec.rb | 2 +- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/editor/editor.js.erb b/app/assets/javascripts/editor/editor.js.erb index 29cbc5ea..02438818 100644 --- a/app/assets/javascripts/editor/editor.js.erb +++ b/app/assets/javascripts/editor/editor.js.erb @@ -27,7 +27,7 @@ var CodeOceanEditor = { lastCopyText: null, - <% @config ||= CodeOcean::Config.new(:code_ocean).read(erb: false) %> + <% @config ||= CodeOcean::Config.new(:code_ocean, erb: false).read %> // Important notice: Changing the config values requires any content-wise // modification for this file in the development environment. Lacking to do so // will result in the old, server-side cached serving of this file even across diff --git a/lib/code_ocean/config.rb b/lib/code_ocean/config.rb index c8055a54..92e8b313 100644 --- a/lib/code_ocean/config.rb +++ b/lib/code_ocean/config.rb @@ -2,12 +2,16 @@ module CodeOcean class Config - def initialize(filename) - @filename = filename + attr_reader :path, :read + + def initialize(filename, options = {}) + @path = Rails.root.join('config', "#{filename}.yml#{options[:erb] ? '.erb' : ''}") + @read = parse(options) end - def read(options = {}) - path = Rails.root.join('config', "#{@filename}.yml#{options[:erb] ? '.erb' : ''}") + private + + def parse(options) if ::File.exist?(path) yaml_content = ::File.new(path, 'r').read || '' yaml_content = ERB.new(yaml_content).result if options[:erb] diff --git a/lib/runner/strategy/docker_container_pool.rb b/lib/runner/strategy/docker_container_pool.rb index 70851435..363f4c4e 100644 --- a/lib/runner/strategy/docker_container_pool.rb +++ b/lib/runner/strategy/docker_container_pool.rb @@ -146,7 +146,7 @@ class Runner::Strategy::DockerContainerPool < Runner::Strategy def self.config @config ||= begin # Since the docker configuration file contains code that must be executed, we use ERB templating. - docker_config = CodeOcean::Config.new(:docker).read(erb: true) + docker_config = CodeOcean::Config.new(:docker, erb: true).read codeocean_config = CodeOcean::Config.new(:code_ocean).read[:runner_management] || {} # All keys in `docker_config` take precedence over those in `codeocean_config` docker_config.merge codeocean_config diff --git a/spec/lib/code_ocean/config_spec.rb b/spec/lib/code_ocean/config_spec.rb index f8ca6c3c..639ad1de 100644 --- a/spec/lib/code_ocean/config_spec.rb +++ b/spec/lib/code_ocean/config_spec.rb @@ -30,7 +30,7 @@ RSpec.describe CodeOcean::Config do context 'with a .yml.erb file' do let(:path) { Rails.root.join('config', "#{filename}.yml.erb") } - let(:read) { described_class.new(filename).read(erb: true) } + let(:read) { described_class.new(filename, erb: true).read } context 'when the file is present' do before { File.write(path, {Rails.env.to_s => content}.to_yaml) }