Welcome to mirror list, hosted at ThFree Co, Russian Federation.

block.rb « interpolation « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 389cbf378a22763b70990f4c6b4faec101fe3501 (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
43
44
45
46
47
48
# frozen_string_literal: true

module Gitlab
  module Ci
    module Interpolation
      class Block
        PREFIX = '$[['
        PATTERN = /(?<block>\$\[\[\s*(?<access>.*?)\s*\]\])/.freeze

        attr_reader :block, :data, :ctx

        def initialize(block, data, ctx)
          @block = block
          @ctx = ctx
          @data = data

          @access = Interpolation::Access.new(@data, ctx)
        end

        def valid?
          errors.none?
        end

        def errors
          @access.errors
        end

        def content
          @access.content
        end

        def value
          raise ArgumentError, 'block invalid' unless valid?

          @access.value
        end

        def self.match(data)
          return data unless data.is_a?(String) && data.include?(PREFIX)

          data.gsub(PATTERN) do
            yield ::Regexp.last_match(1), ::Regexp.last_match(2)
          end
        end
      end
    end
  end
end