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

truncate.rb « functions « interpolation « config « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3771756ba4864af8d0735f7e2c07d9babf7d4565 (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
# frozen_string_literal: true

module Gitlab
  module Ci
    class Config
      module Interpolation
        module Functions
          class Truncate < Base
            def self.function_expression_pattern
              /^#{name}\(\s*(?<offset>\d+)\s*,\s*(?<length>\d+)\s*\)?$/
            end

            def self.name
              'truncate'
            end

            def execute(input_value)
              if input_value.is_a?(String)
                input_value[offset, length].to_s
              else
                error('invalid input type: truncate can only be used with string inputs')
                nil
              end
            end

            private

            def offset
              function_args[:offset].to_i
            end

            def length
              function_args[:length].to_i
            end
          end
        end
      end
    end
  end
end