Class: Externals::Configuration::Configuration

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_string = nil, empty = false) ⇒ Configuration

Returns a new instance of Configuration.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/externals/configuration/configuration.rb', line 153

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.empty? && !bodies.empty?
    if titles.size + 1 != bodies.size
      # :nocov:
      raise "bodies and sections do not match up"
      # :nocov:
    end

    bodies = bodies[1..]

    (0...(bodies.size)).each do |index|
      sections << Section.new(titles[index], bodies[index])
    end
  end
end

Instance Attribute Details

#file_stringObject

Returns the value of attribute file_string.



108
109
110
# File 'lib/externals/configuration/configuration.rb', line 108

def file_string
  @file_string
end

Class Method Details

.new_emptyObject



149
150
151
# File 'lib/externals/configuration/configuration.rb', line 149

def self.new_empty
  new nil, true
end

Instance Method Details

#[](title) ⇒ Object



114
115
116
117
# File 'lib/externals/configuration/configuration.rb', line 114

def [] title
  title = title.to_s
  sections.detect {|section| section.title == title}
end

#[]=(title, hash) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/externals/configuration/configuration.rb', line 119

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



135
136
137
138
139
# File 'lib/externals/configuration/configuration.rb', line 135

def add_empty_section title
  raise "Section already exists" if self[title]

  sections << Section.new("[#{title}]", "")
end

#all_pathsObject



145
146
147
# File 'lib/externals/configuration/configuration.rb', line 145

def all_paths
  sections.map(&:title)
end

#remove_section(sec) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/externals/configuration/configuration.rb', line 127

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



141
142
143
# File 'lib/externals/configuration/configuration.rb', line 141

def removed_project_paths other_config
  all_paths - other_config.all_paths
end

#sectionsObject



110
111
112
# File 'lib/externals/configuration/configuration.rb', line 110

def sections
  @sections ||= []
end

#to_sObject



184
185
186
# File 'lib/externals/configuration/configuration.rb', line 184

def to_s
  sections.join("\n\n")
end

#write(path = ".externals") ⇒ Object



178
179
180
181
182
# File 'lib/externals/configuration/configuration.rb', line 178

def write path = ".externals"
  raise "no path given" unless path

  File.write(path, to_s)
end