Class: Fop::Compiler::Instructions::ExpressionMatch

Inherits:
Object
  • Object
show all
Defined in:
lib/fop/compiler.rb

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ ExpressionMatch

Returns a new instance of ExpressionMatch.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fop/compiler.rb', line 39

def initialize(node)
  @regex = node.regex&.regex
  @op = node.operator ? OPERATIONS.fetch(node.operator) : nil
  @regex_match = node.regex_match
  if node.arg&.any? { |a| a.is_a? Integer }
    @arg, @arg_with_caps = nil, node.arg
  else
    @arg = node.arg&.join("")
    @arg_with_caps = nil
  end
end

Instance Method Details

#call(input) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/fop/compiler.rb', line 51

def call(input)
  if (match = @regex.match(input))
    val = match.to_s
    blank = val == BLANK
    input.sub!(val, BLANK) unless blank
    found_val = @regex_match || !blank
    arg = @arg_with_caps ? sub_caps(@arg_with_caps, match.captures) : @arg
    @op && found_val ? @op.call(val, arg) : val
  end
end