Class: Externals::Configuration::Section

Inherits:
Object
  • Object
show all
Defined in:
lib/externals/configuration/configuration.rb

Constant Summary collapse

SETTING_REGEX =
/^\s*([.\w-]+)\s*=\s*([^#\n]*)(?:#[^\n]*)?$/
SET_SETTING_REGEX =
/^(\s*(?:[.\w-]+)\s*=\s*)(?:[^#\n]*)(#[^\n]*)?$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title_string, body_string) ⇒ Section

Returns a new instance of Section.



9
10
11
12
13
14
15
16
17
18
# File 'lib/externals/configuration/configuration.rb', line 9

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

Instance Attribute Details

#body_stringObject

Returns the value of attribute body_string.



7
8
9
# File 'lib/externals/configuration/configuration.rb', line 7

def body_string
  @body_string
end

#rowsObject

Returns the value of attribute rows.



7
8
9
# File 'lib/externals/configuration/configuration.rb', line 7

def rows
  @rows
end

#titleObject

Returns the value of attribute title.



7
8
9
# File 'lib/externals/configuration/configuration.rb', line 7

def title
  @title
end

#title_stringObject

Returns the value of attribute title_string.



7
8
9
# File 'lib/externals/configuration/configuration.rb', line 7

def title_string
  @title_string
end

Instance Method Details

#[](key) ⇒ Object



94
95
96
# File 'lib/externals/configuration/configuration.rb', line 94

def [] key
  setting(key)
end

#[]=(key, value) ⇒ Object



98
99
100
# File 'lib/externals/configuration/configuration.rb', line 98

def []= key, value
  set_setting(key, value)
end

#attributesObject



23
24
25
26
27
28
29
30
31
# File 'lib/externals/configuration/configuration.rb', line 23

def attributes
  retval = {}
  rows.each do |row|
    if row =~ SETTING_REGEX
      retval[$1.strip] = $2.strip
    end
  end
  retval
end

#rm_setting(key) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/externals/configuration/configuration.rb', line 68

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
      # :nocov:
      raise "thought I found the row, but didn't"
      # :nocov:
    end

    rows.delete rows[found]
  end
  value
end

#set_setting(key, value) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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
      # :nocov:
      raise "thought I found the row, but didn't"
      # :nocov:
    end

    rows[found] = "#{$1}#{value}#{$2}"
  else
    rows << "#{key} = #{value}"
  end
  value
end

#setting(key) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/externals/configuration/configuration.rb', line 33

def setting key
  rows.each do |row|
    if row =~ SETTING_REGEX && key.to_s == $1
      return $2.strip
    end
  end
  nil
end

#to_sObject



102
103
104
# File 'lib/externals/configuration/configuration.rb', line 102

def to_s
  "[#{title}]\n#{rows.join("\n")}"
end