Module: Kaal::SchedulerPlaceholderSupport

Included in:
SchedulerFileLoader
Defined in:
lib/kaal/scheduler_file/placeholder_support.rb,
sig/kaal/scheduler_file/placeholder_support.rbs

Overview

Placeholder parsing/resolution for scheduler args and kwargs.

Instance Method Summary collapse

Instance Method Details

#placeholder_token_anchors ⇒ Kaal::rbs_any

Returns:

  • (Kaal::rbs_any)


77
78
79
80
81
82
# File 'lib/kaal/scheduler_file/placeholder_support.rb', line 77

def placeholder_token_anchors
  @placeholder_token_anchors ||= begin
    pattern = self.class::PLACEHOLDER_PATTERN
    Regexp.new("\\A#{pattern.source}\\z", pattern.options)
  end
end

#replace_placeholders(text, context) ⇒ Kaal::rbs_any

Parameters:

  • text (Kaal::rbs_any)
  • context (Kaal::rbs_any)

Returns:

  • (Kaal::rbs_any)


44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kaal/scheduler_file/placeholder_support.rb', line 44

def replace_placeholders(text, context)
  pattern = self.class::PLACEHOLDER_PATTERN
  anchored_pattern = Regexp.new("\\A#{pattern.source}\\z", pattern.options)
  if (match = text.match(anchored_pattern))
    return @placeholder_resolvers.fetch(match[1]).call(context)
  end

  text.gsub(pattern) do
    token = Regexp.last_match(1)
    @placeholder_resolvers.fetch(token).call(context).to_s
  end
end

#resolve_placeholders(template, context) ⇒ Kaal::rbs_any

Parameters:

  • template (Kaal::rbs_any)
  • context (Kaal::rbs_any)

Returns:

  • (Kaal::rbs_any)


31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/kaal/scheduler_file/placeholder_support.rb', line 31

def resolve_placeholders(template, context)
  case template
  when String
    replace_placeholders(template, context)
  when Array
    template.map { |item| resolve_placeholders(item, context) }
  when Hash
    template.transform_values { |child| resolve_placeholders(child, context) }
  else
    template
  end
end

#validate_placeholder_key(hash_key, key:) ⇒ nil, Kaal::rbs_any

Parameters:

  • hash_key (Kaal::rbs_any)
  • key: (Kaal::rbs_any)

Returns:

  • (nil, Kaal::rbs_any)

Raises:



57
58
59
60
61
62
63
64
65
66
# File 'lib/kaal/scheduler_file/placeholder_support.rb', line 57

def validate_placeholder_key(hash_key, key:)
  return unless hash_key.is_a?(String)

  validate_placeholder_syntax(hash_key, key:)

  token = hash_key.scan(self.class::PLACEHOLDER_PATTERN).flatten.first
  return unless token

  raise SchedulerConfigError, "Placeholders are not supported in hash keys (got '{{#{token}}}' under '#{key}')"
end

#validate_placeholder_syntax(input, key:) ⇒ Kaal::rbs_any

Parameters:

  • input (Kaal::rbs_any)
  • key: (Kaal::rbs_any)

Returns:

  • (Kaal::rbs_any)


68
69
70
71
72
73
74
75
# File 'lib/kaal/scheduler_file/placeholder_support.rb', line 68

def validate_placeholder_syntax(input, key:)
  raw_placeholders = input.scan(/\{\{.*?\}\}/)
  raw_placeholders.each do |raw_placeholder|
    next if raw_placeholder.match?(placeholder_token_anchors)

    raise SchedulerConfigError, "Malformed placeholder '#{raw_placeholder}' for key '#{key}'"
  end
end

#validate_placeholders(input, key:) ⇒ Kaal::rbs_any

Parameters:

  • input (Kaal::rbs_any)
  • key: (Kaal::rbs_any)

Returns:

  • (Kaal::rbs_any)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kaal/scheduler_file/placeholder_support.rb', line 12

def validate_placeholders(input, key:)
  case input
  when String
    validate_placeholder_syntax(input, key:)
    input.scan(self.class::PLACEHOLDER_PATTERN).flatten.each do |token|
      next if @placeholder_resolvers.key?(token)

      raise SchedulerConfigError, "Unknown placeholder '{{#{token}}}' for key '#{key}'"
    end
  when Array
    input.each { |item| validate_placeholders(item, key:) }
  when Hash
    input.each_pair do |hash_key, child|
      validate_placeholder_key(hash_key, key:)
      validate_placeholders(child, key:)
    end
  end
end