Class: Chef::Attribute::Validator::WildcardExpander::BrutalRegex

Inherits:
Chef::Attribute::Validator::WildcardExpander show all
Defined in:
lib/chef-attribute-validator/wildcard_expander/brutal_regex.rb

Instance Attribute Summary

Attributes inherited from Chef::Attribute::Validator::WildcardExpander

#node, #path_spec

Instance Method Summary collapse

Methods inherited from Chef::Attribute::Validator::WildcardExpander

choose, #fetch_val_by_slashpath, #initialize, #path_contains_wildcards?, #path_exists_by_slashpath?, #slashpath_to_steps

Constructor Details

This class inherits a constructor from Chef::Attribute::Validator::WildcardExpander

Instance Method Details

#convert_path_spec_to_regexObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/chef-attribute-validator/wildcard_expander/brutal_regex.rb', line 51

def convert_path_spec_to_regex
  re = path_spec.dup

  # Anchor everything
  re = '^' + re + '$'

  # * => "anything but a slash"
  re.gsub!(/([^*])\*(?!\*)/, '\1[^\/]*')
  
  # ? => "any single char other than a slash"
  re.gsub!(/\?/, '[^\/]')

  # ** => "anything"
  re.gsub!(/\*\*/, '.*')

  # {,} =>  alternatives # TODO

  Regexp.new(re)

end

#expand_allObject



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/chef-attribute-validator/wildcard_expander/brutal_regex.rb', line 9

def expand_all
  # Create a massive list of all attribute paths, in slashpath format
  all_slashpaths = find_all_slashpaths

  # Convert the path_spec into a terrifying regex
  regex_spec = convert_path_spec_to_regex

  # Filter the list by grepping
  all_slashpaths.grep(regex_spec)

end

#find_all_slashpaths(prefix = '', node_cursor = nil) ⇒ Object

TODO: maybe we could cache this on the node, or something?



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chef-attribute-validator/wildcard_expander/brutal_regex.rb', line 28

def find_all_slashpaths (prefix='', node_cursor = nil)
  node_cursor ||= node
  child_paths = []

  
  if node_cursor.kind_of?(Array)
    node_cursor.each_index do |idx|
      child_paths.push prefix + '/' + idx.to_s
      if node_cursor[idx].kind_of?(Mash) || node_cursor[idx].kind_of?(Array)
        child_paths += find_all_slashpaths(prefix + '/' + idx.to_s, node_cursor[idx])
      end                
    end
  else
    node_cursor.keys.each do |key|
      child_paths.push prefix + '/' + key.to_s
      if node_cursor[key].kind_of?(Mash) || node_cursor[key].kind_of?(Array)
        child_paths += find_all_slashpaths(prefix + '/' + key, node_cursor[key])
      end
    end
  end
  child_paths
end

#suitabilityObject



21
22
23
24
# File 'lib/chef-attribute-validator/wildcard_expander/brutal_regex.rb', line 21

def suitability
  # I can do anything, but I'll do it badly
  0.1
end