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

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-attribute-validator/wildcard_expander.rb,
lib/chef-attribute-validator/wildcard_expander/brutal_regex.rb,
lib/chef-attribute-validator/wildcard_expander/no_wildcards.rb

Direct Known Subclasses

BrutalRegex, NoWildcards

Defined Under Namespace

Classes: BrutalRegex, NoWildcards

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a_path_spec, a_node) ⇒ WildcardExpander



10
11
12
13
# File 'lib/chef-attribute-validator/wildcard_expander.rb', line 10

def initialize(a_path_spec, a_node)
  @path_spec = a_path_spec
  @node = a_node
end

Instance Attribute Details

#nodeObject

Returns the value of attribute node.



8
9
10
# File 'lib/chef-attribute-validator/wildcard_expander.rb', line 8

def node
  @node
end

#path_specObject

Returns the value of attribute path_spec.



7
8
9
# File 'lib/chef-attribute-validator/wildcard_expander.rb', line 7

def path_spec
  @path_spec
end

Class Method Details

.choose(path_spec, node) ⇒ Object



15
16
17
18
# File 'lib/chef-attribute-validator/wildcard_expander.rb', line 15

def self.choose(path_spec, node)
  # Ruby is an excellent language in which to write Perl
  @@subclasses.map {|k| k.new(path_spec, node) }.map {|e| [e, e.suitability]}.sort { |a,b| b[1] <=> a[1] }.map {|e| e[0]}.first
end

Instance Method Details

#fetch_val_by_slashpath(slashpath) ⇒ Object

Note: does not handle missing values correctly!



34
35
36
37
38
39
40
41
# File 'lib/chef-attribute-validator/wildcard_expander.rb', line 34

def fetch_val_by_slashpath(slashpath)
  nv = node
  steps = slashpath_to_steps(slashpath)
  while steps.size > 0 
    nv = nv[steps.shift]
  end
  nv
end

#path_contains_wildcards?(path) ⇒ Boolean



75
76
77
78
79
80
81
82
83
# File 'lib/chef-attribute-validator/wildcard_expander.rb', line 75

def path_contains_wildcards?(path)
  [
   /\*/,
   /\*\*/,
   /\?/,
   /\[\w+\]/,
   /\{\w+(\s*,\s*\w+)?\}/, 
  ].any? { |r| r.match(path) }
end

#path_exists_by_slashpath?(slashpath) ⇒ Boolean



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/chef-attribute-validator/wildcard_expander.rb', line 43

def path_exists_by_slashpath? (slashpath)
  nv = node
  steps = slashpath_to_steps(slashpath)
  while steps.size > 0 
    step = steps.shift
    
    if nv.kind_of?(Chef::Node::ImmutableArray) then
      # TODO: what if the step isn't an int?
      
      if nv.size > step then
        nv = nv[step]
      else
        # Array not long enough
        return false
      end
      
    elsif nv.respond_to?(:has_key?) then
      if nv.has_key?(step) then
        nv = nv[step]
      else
        # No such key
        return false
      end
    else
      # Must be a scalar?
      return false
    end
  end
  return true      
end

#slashpath_to_steps(slashpath) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/chef-attribute-validator/wildcard_expander.rb', line 20

def slashpath_to_steps(slashpath)
  steps = slashpath.split('/')

  # Since we begin with /, the first element is ""
  if steps[0] == "" then steps.shift end

  # Promote integer strings to integers
  steps.map! { |s| s.match(/^\d+$/) ? s.to_i : s }

  steps
end