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.



56
57
58
59
60
61
# File 'lib/puppet/parser/ast/leaf.rb', line 56

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.



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

def evaluate(scope)
  self
end

#match(value) ⇒ Object



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

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

#to_sObject



81
82
83
# File 'lib/puppet/parser/ast/leaf.rb', line 81

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