Class: Wonk::Concretizer

Inherits:
Object
  • Object
show all
Defined in:
lib/wonk/concretizer.rb

Defined Under Namespace

Classes: DirectiveEvaluator

Constant Summary collapse

CAPTURE_REGEXP =
/!<(?<directive>.*?)>/

Instance Method Summary collapse

Instance Method Details

#concretize(text, env) ⇒ Object



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
# File 'lib/wonk/concretizer.rb', line 17

def concretize(text, env)
  raise "'text' must be a String." unless text.is_a?(String)
  raise "'env' must be a Hash." unless env.is_a?(Hash)

  env = env.deep_dup.deep_symbolize_keys

  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
  working.reject! { |t| t.empty? }

  evaluator = DirectiveEvaluator.new(env)

  working.map do |token|
    match = CAPTURE_REGEXP.match(token)
    if match.nil?
      token
    else
      evaluator.evaluate(match['directive'])
    end
  end
end