Class: Puppet::Parser::AST::Regex

Inherits:
Leaf show all
Defined in:
lib/puppet/parser/ast/leaf.rb

Constant Summary

Constants inherited from Puppet::Parser::AST

AST

Instance Attribute Summary

Attributes inherited from Leaf

#type, #value

Attributes inherited from Puppet::Parser::AST

#file, #line, #parent, #pos, #scope

Instance Method Summary collapse

Methods inherited from Puppet::Parser::AST

#inspect, #safeevaluate

Methods included from Util::Errors

#adderrorcontext, #devfail, #error_context, error_location, error_location_with_space, error_location_with_unknowns, #exceptwrap, #fail

Constructor Details

#initialize(value: nil, file: nil, line: nil, pos: nil) ⇒ Regex

Returns a new instance of Regex.


53
54
55
56
57
58
# File 'lib/puppet/parser/ast/leaf.rb', line 53

def initialize(value: nil, file: nil, line: nil, pos: nil)
  super(value: value, file: file, line: line, pos: pos)

  # transform value from hash options unless it is already a regular expression
  @value = Regexp.new(@value) unless @value.is_a?(Regexp)
end

Instance Method Details

#evaluate(scope) ⇒ Object

we're returning self here to wrap the regexp and to be used in places where a string would have been used, without modifying any client code. For instance, in many places we have the following code snippet:

val = @val.safeevaluate(@scope)
if val.match(otherval)
    ...
end

this way, we don't have to modify this test specifically for handling regexes.


70
71
72
# File 'lib/puppet/parser/ast/leaf.rb', line 70

def evaluate(scope)
  self
end

#match(value) ⇒ Object


74
75
76
# File 'lib/puppet/parser/ast/leaf.rb', line 74

def match(value)
  @value.match(value)
end

#to_sObject


78
79
80
# File 'lib/puppet/parser/ast/leaf.rb', line 78

def to_s
  Puppet::Pops::Types::PRegexpType.regexp_to_s_with_delimiters(@value)
end