Class: BigIParse::Config::Section

Inherits:
Object
  • Object
show all
Defined in:
lib/bigip_parse/config.rb

Overview

Class to represent a section of a config file

Constant Summary collapse

REGEX =
/
  ^(?<header>([^\s{}#]+[ ]?)+) # Header definition
  ({                         # Opening bracket
    (?<content>(             # Optional content capture everything
        [^\n]*(?=\s})        # not a new-line followed by space bracket
        |                    # OR
        .*?(?=^})            # everything then closing bracket
      )
    )
  )?                         # Make content capture optional
/mx

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header, content = nil) ⇒ Section

Returns a new instance of Section.



22
23
24
25
# File 'lib/bigip_parse/config.rb', line 22

def initialize(header, content = nil)
  @header = header
  @content = content
end

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



8
9
10
# File 'lib/bigip_parse/config.rb', line 8

def header
  @header
end

Instance Method Details

#contentObject



27
28
29
30
31
# File 'lib/bigip_parse/config.rb', line 27

def content
  return nil if @content.nil?
  return nil if @content.strip.empty?
  @content.strip_heredoc.lstrip
end

#subsectionsObject



37
38
39
40
# File 'lib/bigip_parse/config.rb', line 37

def subsections
  return [] if content.nil?
  content.scan(REGEX).map { |h, c| self.class.new(h.strip, c) }
end

#subsections?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/bigip_parse/config.rb', line 33

def subsections?
  !content.nil?
end

#to_sObject



42
43
44
# File 'lib/bigip_parse/config.rb', line 42

def to_s
  "#{self.class.name}(header=#{header}, content=#{content})"
end