Class: Externals::Configuration::Configuration
- Inherits:
-
Object
- Object
- Externals::Configuration::Configuration
- Defined in:
- lib/externals/configuration/configuration.rb
Instance Attribute Summary collapse
-
#file_string ⇒ Object
Returns the value of attribute file_string.
Class Method Summary collapse
Instance Method Summary collapse
- #[](title) ⇒ Object
- #[]=(title, hash) ⇒ Object
- #add_empty_section(title) ⇒ Object
- #all_paths ⇒ Object
-
#initialize(file_string = nil, empty = false) ⇒ Configuration
constructor
A new instance of Configuration.
- #remove_section(sec) ⇒ Object
- #removed_project_paths(other_config) ⇒ Object
- #sections ⇒ Object
- #to_s ⇒ Object
- #write(path = ".externals") ⇒ Object
Constructor Details
#initialize(file_string = nil, empty = false) ⇒ Configuration
Returns a new instance of Configuration.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/externals/configuration/configuration.rb', line 149 def initialize file_string = nil, empty = false self.file_string = file_string return if empty raise "I was given no file_string" unless file_string titles = [] file_string.each_line {|line| titles << line if line =~ SECTION_TITLE_REGEX} bodies = file_string.split SECTION_TITLE_REGEX_NO_GROUPS if titles.size > 0 && bodies.size > 0 if titles.size + 1 != bodies.size raise "bodies and sections do not match up" end bodies = bodies[1..(bodies.size - 1)] (0...(bodies.size)).each do |index| sections << Section.new(titles[index], bodies[index]) end end end |
Instance Attribute Details
#file_string ⇒ Object
Returns the value of attribute file_string.
106 107 108 |
# File 'lib/externals/configuration/configuration.rb', line 106 def file_string @file_string end |
Class Method Details
.new_empty ⇒ Object
145 146 147 |
# File 'lib/externals/configuration/configuration.rb', line 145 def self.new_empty new nil, true end |
Instance Method Details
#[](title) ⇒ Object
112 113 114 115 |
# File 'lib/externals/configuration/configuration.rb', line 112 def [] title title = title.to_s sections.detect {|section| section.title == title} end |
#[]=(title, hash) ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/externals/configuration/configuration.rb', line 117 def []= title, hash add_empty_section title section = self[title] hash.each_pair do |key,value| section[key] = value end end |
#add_empty_section(title) ⇒ Object
132 133 134 135 |
# File 'lib/externals/configuration/configuration.rb', line 132 def add_empty_section title raise "Section already exists" if self[title] sections << Section.new("[#{title.to_s}]", "") end |
#all_paths ⇒ Object
141 142 143 |
# File 'lib/externals/configuration/configuration.rb', line 141 def all_paths sections.map(&:title) end |
#remove_section(sec) ⇒ Object
125 126 127 128 129 130 |
# File 'lib/externals/configuration/configuration.rb', line 125 def remove_section sec sec = sections.detect{|section| section.title == sec} raise "No section found in config file for #{sec}" unless sec sections.delete(sec) end |
#removed_project_paths(other_config) ⇒ Object
137 138 139 |
# File 'lib/externals/configuration/configuration.rb', line 137 def removed_project_paths other_config all_paths - other_config.all_paths end |
#sections ⇒ Object
108 109 110 |
# File 'lib/externals/configuration/configuration.rb', line 108 def sections @sections ||= [] end |
#to_s ⇒ Object
179 180 181 |
# File 'lib/externals/configuration/configuration.rb', line 179 def to_s sections.map(&:to_s).join("\n\n") end |
#write(path = ".externals") ⇒ Object
172 173 174 175 176 177 |
# File 'lib/externals/configuration/configuration.rb', line 172 def write path = ".externals" raise "no path given" unless path open(path, 'w') do |f| f.write to_s end end |