lib/prick/ext/expand_variables.rb in prick-0.42.1 vs lib/prick/ext/expand_variables.rb in prick-0.43.0
- old
+ new
@@ -12,11 +12,11 @@
# present in the template string
#
def expand_variables(str, variables) # ChatGPT
# Replace escaped bashslashes and dollar signs
str = str.gsub('\\\\', "\x00").gsub('\\$', "\x01")
-
+
# Expand variables
str.gsub!(/\$(\w+)\b|\$\{(\w+)\}/) do |match| # Strange that '\b' is necessary
key = $1 || $2
# variables[key] || match
variables[key] || ""
@@ -28,13 +28,20 @@
# Return true if any of the variables will be expanded
def expand_variables?(str, variables)
# Replace escaped bashslashes and dollar signs
str = str.gsub('\\\\', "\x00").gsub('\\$', "\x01")
-
+
# Look for expansion
str.gsub!(/\$(\w+)\b|\$\{(\w+)\}/) do |match| # Strange that '\b' is necessary
return true if variables.include?($1 || $2)
end
return false
+end
+
+# Return true if the string contains a variable
+def has_variables?(str)
+ # Replace escaped bashslashes and dollar signs
+ str = str.gsub('\\\\', "\x00").gsub('\\$', "\x01")
+ !(str =~ /\$[a-zA-Z{]/).nil?
end