Class: Externals::Configuration::Section
- Inherits:
-
Object
- Object
- Externals::Configuration::Section
- 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
-
#body_string ⇒ Object
Returns the value of attribute body_string.
-
#rows ⇒ Object
Returns the value of attribute rows.
-
#title ⇒ Object
Returns the value of attribute title.
-
#title_string ⇒ Object
Returns the value of attribute title_string.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #add_row(row) ⇒ Object
- #attributes ⇒ Object
-
#initialize(title_string, body_string) ⇒ Section
constructor
A new instance of Section.
- #rm_setting(key) ⇒ Object
- #set_setting(key, value) ⇒ Object
- #setting(key) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(title_string, body_string) ⇒ Section
Returns a new instance of Section.
10 11 12 13 14 15 16 17 18 19 |
# 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 |
Instance Attribute Details
#body_string ⇒ Object
Returns the value of attribute body_string.
8 9 10 |
# File 'lib/externals/configuration/configuration.rb', line 8 def body_string @body_string end |
#rows ⇒ Object
Returns the value of attribute rows.
8 9 10 |
# File 'lib/externals/configuration/configuration.rb', line 8 def rows @rows end |
#title ⇒ Object
Returns the value of attribute title.
8 9 10 |
# File 'lib/externals/configuration/configuration.rb', line 8 def title @title end |
#title_string ⇒ Object
Returns the value of attribute title_string.
8 9 10 |
# File 'lib/externals/configuration/configuration.rb', line 8 def title_string @title_string end |
Instance Method Details
#[](key) ⇒ Object
86 87 88 |
# File 'lib/externals/configuration/configuration.rb', line 86 def [] key setting(key) end |
#[]=(key, value) ⇒ Object
89 90 91 |
# File 'lib/externals/configuration/configuration.rb', line 89 def []= key, value set_setting(key, value) end |
#add_row(row) ⇒ Object
93 94 95 96 97 98 |
# 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 ⇒ Object
25 26 27 28 29 30 31 32 33 |
# 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) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# 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) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# 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) ⇒ Object
34 35 36 37 38 39 40 41 |
# 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 ⇒ Object
100 101 102 |
# File 'lib/externals/configuration/configuration.rb', line 100 def to_s "[#{title}]\n#{rows.join("\n")}" end |