Module: Cfer

Defined in:
lib/cfer/cfizer.rb,
lib/cfer/provisioning.rb,
lib/cfer/provisioning/version.rb

Defined Under Namespace

Modules: Provisioning Classes: Cfizer

Constant Summary collapse

DEFAULT_CAPTURE_REGEXP =
/C\{(?<directive>.*?)\}/

Class Method Summary collapse

Class Method Details

.cfize(text, capture_regexp: nil) ⇒ Object



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
# File 'lib/cfer/cfizer.rb', line 18

def self.cfize(text, capture_regexp: nil)
  raise "'text' must be a string." unless text.is_a?(String)

  capture_regexp ||= DEFAULT_CAPTURE_REGEXP

  raise "'capture_regexp' must be a Regexp." unless capture_regexp.is_a?(Regexp)
  raise "'capture_regexp' must include a 'contents' named 'directive'." \
    unless capture_regexp.named_captures.key?('directive')

  working = []
  until(working[-2] == "" && working[-1] == "") do
    if (working.size == 0)
      working = text.partition(capture_regexp)
    else
      working[-1] = working[-1].partition(capture_regexp)
      working = working.flatten
    end
  end

  cfizer = Cfizer.new
  Cfizer::Fn.join("", working.map do |token|
    match = capture_regexp.match(token)
    if (match.nil?)
      token
    else
      cfizer.cfize(match['directive'])
    end
  end.reject { |t| t == ""})
end