class Externals::Configuration::Section

Constants

SETTING_REGEX
SET_SETTING_REGEX

Attributes

body_string[RW]
rows[RW]
title[RW]
title_string[RW]

Public Class Methods

new(title_string, body_string) click to toggle source
# File lib/externals/configuration/configuration.rb, line 10
def initialize title_string, body_string
  self.title_string = title_string
  self.body_string = body_string

  self.title = SECTION_TITLE_REGEX.match(title_string)[1]

  raise "Invalid section title: #{title_string}" unless title

  self.rows = body_string.strip.split(/\n/)
end

Public Instance Methods

[](key) click to toggle source
# File lib/externals/configuration/configuration.rb, line 86
def [] key
  setting(key)
end
[]=(key, value) click to toggle source
# File lib/externals/configuration/configuration.rb, line 89
def []= key, value
  set_setting(key, value)
end
add_row(row) click to toggle source
# File lib/externals/configuration/configuration.rb, line 93
def add_row(row)
  rows << row

  self.body_string = body_string.chomp + "\n#{row}\n"
  #clear_caches
end
attributes() click to toggle source
# File lib/externals/configuration/configuration.rb, line 25
def attributes
  retval = {}
  rows.each do |row|
    if row =~ SETTING_REGEX
      retval[$1.strip] = $2.strip
    end
  end
  retval
end
rm_setting(key) click to toggle source
# File lib/externals/configuration/configuration.rb, line 64
def rm_setting key
  key = key.to_s
  found = nil
  value = nil

  rows.each_with_index do |row, index|
    if row =~ SETTING_REGEX && key == $1
      raise "found #{key} twice!" if found
      found = index
    end
  end

  if found
    value = self[key]
    if rows[found] !~ SET_SETTING_REGEX
      raise "thought I found the row, but didn't"
    end
    rows.delete rows[found]
  end
  value
end
set_setting(key, value) click to toggle source
# File lib/externals/configuration/configuration.rb, line 42
def set_setting key, value
  key = key.to_s
  found = nil

  rows.each_with_index do |row, index|
    if row =~ SETTING_REGEX && key == $1
      raise "found #{key} twice!" if found
      found = index
    end
  end

  if found
    if rows[found] !~ SET_SETTING_REGEX
      raise "thought I found the row, but didn't"
    end
    rows[found] = "#{$1}#{value}#{$2}"
  else
    rows << "#{key} = #{value}"
  end
  value
end
setting(key) click to toggle source
# File lib/externals/configuration/configuration.rb, line 34
def setting key
  rows.each do |row|
    if row =~ SETTING_REGEX && key.to_s == $1
      return $2.strip
    end
  end
  nil
end
to_s() click to toggle source
# File lib/externals/configuration/configuration.rb, line 100
def to_s
  "[#{title}]\n#{rows.join("\n")}"
end