Method: Cisco::Client.to_regexp

Defined in:
lib/cisco_node_utils/client/utils.rb

.to_regexp(input) ⇒ Object

Helper method for CLI getters

Convert a string or array of strings to a Regexp or array thereof



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cisco_node_utils/client/utils.rb', line 107

def self.to_regexp(input)
  if input.is_a?(Regexp)
    return input
  elsif input.is_a?(Array)
    return input.map { |item| to_regexp(item) }
  else
    # The string might be explicitly formatted as a regexp
    # Dynamically handle modifiers
    input.match(%r{(?<regex>^\/.*\/)(?<options>[imx]*)?}) do |m|
      options = []
      m['options'].each_char do |c|
        case c
        when 'i'
          options << Regexp::IGNORECASE
        when 'm'
          options << Regexp::MULTILINE
        when 'x'
          options << Regexp::EXTENDED
        end
      end
      return Regexp.new(m['regex'][1..-2], options.reduce(:|))
    end
    # otherwise this value is a regular string
    # convert to case insensitive regex
    # 'foo' => %r{^foo$}i
    return Regexp.new("^#{input}$", Regexp::IGNORECASE)

  end
end