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

Returns a new instance of 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

#convert_step_to_integer(step) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/chef-attribute-validator/wildcard_expander.rb', line 45

def convert_step_to_integer(step)
  if step.match(/^\d+$/) then 
    # Treat things that start with leading zeros as strings, not as integers.  This supports 
    # having attributes like '00', and calling that an error if you try to access the 00 element of an array.
    return nil if step.match(/^0+\d+$/)
    return step.to_i
  else
    return nil
  end          
end

#fetch_val_by_slashpath(slashpath) ⇒ Object

Note: does not handle missing values correctly!



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

def fetch_val_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
      step = convert_step_to_integer(step)
      return nil if step.nil? # Given a non-int path step for an array...
    end
    nv = nv[step]
  end
  nv
end

#path_contains_wildcards?(path) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
98
# File 'lib/chef-attribute-validator/wildcard_expander.rb', line 90

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

#path_exists_by_slashpath?(slashpath) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/chef-attribute-validator/wildcard_expander.rb', line 56

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

      step_as_int = convert_step_to_integer(step) 
      return false if step_as_int.nil? # Given a non-int path step for an array...
      
      if nv.size > step_as_int then
        nv = nv[step_as_int]
      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
# 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

  steps
end