Method: Chef::ChefFS::FilePattern#could_match_children?

Defined in:
lib/chef/chef_fs/file_pattern.rb

#could_match_children?(path) ⇒ Boolean

Reports whether this pattern could match children of path. If the pattern doesn’t match the path up to this point or if it matches and doesn’t allow further children, this will return false.

Attributes

  • path - a path to check

Examples

abc/def.could_match_children?('abc') == true
abc.could_match_children?('abc') == false
abc/def.could_match_children?('x') == false
a**z.could_match_children?('ab/cd') == true

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/chef/chef_fs/file_pattern.rb', line 72

def could_match_children?(path)
  return false if path == "" # Empty string is not a path

  argument_is_absolute = Chef::ChefFS::PathUtils.is_absolute?(path)
  return false if is_absolute != argument_is_absolute

  path = path[1, path.length - 1] if argument_is_absolute

  path_parts = Chef::ChefFS::PathUtils.split(path)
  # If the pattern is shorter than the path (or same size), children will be larger than the pattern, and will not match.
  return false if regexp_parts.length <= path_parts.length && !has_double_star
  # If the path doesn't match up to this point, children won't match either.
  return false if path_parts.zip(regexp_parts).any? { |part, regexp| !regexp.nil? && !regexp.match(part) }

  # Otherwise, it's possible we could match: the path matches to this point, and the pattern is longer than the path.
  # TODO There is one edge case where the double star comes after some characters like abc**def--we could check whether the next
  # bit of path starts with abc in that case.
  true
end