diff options
author | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-12-18 23:39:51 +0100 |
---|---|---|
committer | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-12-18 23:39:51 +0100 |
commit | 14d47884dff6844625c2e65b247fd773d78f5ea2 (patch) | |
tree | 10fba027e61521df236fd6eec7ba829c5fe2c8ec /lib/gitlab/ci/config/entry/configurable.rb | |
parent | 9fd775def2d89500cf291fe675458b68ead7cd2c (diff) | |
parent | 546fa165ff728bc2d25ed9b55b95dd1d48139d4a (diff) | |
download | gitlab-ce-14d47884dff6844625c2e65b247fd773d78f5ea2.tar.gz |
Merge remote-tracking branch 'origin/master' into dockerfile-templatesdockerfile-templates
Diffstat (limited to 'lib/gitlab/ci/config/entry/configurable.rb')
-rw-r--r-- | lib/gitlab/ci/config/entry/configurable.rb | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/lib/gitlab/ci/config/entry/configurable.rb b/lib/gitlab/ci/config/entry/configurable.rb new file mode 100644 index 00000000000..833ae4a0ff3 --- /dev/null +++ b/lib/gitlab/ci/config/entry/configurable.rb @@ -0,0 +1,76 @@ +module Gitlab + module Ci + class Config + module Entry + ## + # This mixin is responsible for adding DSL, which purpose is to + # simplifly process of adding child nodes. + # + # This can be used only if parent node is a configuration entry that + # holds a hash as a configuration value, for example: + # + # job: + # script: ... + # artifacts: ... + # + module Configurable + extend ActiveSupport::Concern + include Validatable + + included do + validations do + validates :config, type: Hash + end + end + + def compose!(deps = nil) + return unless valid? + + self.class.nodes.each do |key, factory| + factory + .value(@config[key]) + .with(key: key, parent: self) + + @entries[key] = factory.create! + end + + yield if block_given? + + @entries.each_value do |entry| + entry.compose!(deps) + end + end + + class_methods do + def nodes + Hash[(@nodes || {}).map { |key, factory| [key, factory.dup] }] + end + + private # rubocop:disable Lint/UselessAccessModifier + + def entry(key, entry, metadata) + factory = Entry::Factory.new(entry) + .with(description: metadata[:description]) + + (@nodes ||= {}).merge!(key.to_sym => factory) + end + + def helpers(*nodes) + nodes.each do |symbol| + define_method("#{symbol}_defined?") do + @entries[symbol].specified? if @entries[symbol] + end + + define_method("#{symbol}_value") do + return unless @entries[symbol] && @entries[symbol].valid? + + @entries[symbol].value + end + end + end + end + end + end + end + end +end |