class BrocadeVRouter::Configuration

Constants

ARRAY_FIELDS
CBRKT
LTSEP
OBRKT
ParseError

Public Class Methods

new(data) click to toggle source
# File lib/brocade_vrouter/configuration.rb, line 15
def initialize(data)
  @conf = parse_lines! data.lines, []
end

Public Instance Methods

to_h() click to toggle source
# File lib/brocade_vrouter/configuration.rb, line 19
def to_h
  @conf
end

Private Instance Methods

array_field?(path, name) click to toggle source
# File lib/brocade_vrouter/configuration.rb, line 58
def array_field?(path, name)
  ARRAY_FIELDS[name] && path.include?(ARRAY_FIELDS[name])
end
parse_lines!(lines, path) click to toggle source
# File lib/brocade_vrouter/configuration.rb, line 25
def parse_lines!(lines, path)
  conf = {}
  while lines.any?
    begin
      literals = lines.shift.split(LTSEP)
      name = literals.shift

      if name == CBRKT
        return conf
      elsif literals.last == OBRKT
        if literals.first != OBRKT
          conf[name] ||= {}
          conf[name][literals.first] = parse_lines! lines, path + [name, literals.first]
        else
          conf[name] = parse_lines! lines, path + [name]
        end
      else
        value = literals.join(LTSEP).tr('\'"', '')
        if array_field?(path, name)
          conf[name] ||= []
          conf[name] << value
        else
          conf[name] = value.empty? ? nil : value
        end
      end
    rescue StandardError => e
      raise ParseError.new("#{e.message}: #{literals}")
    end
  end

  conf
end