ext 1.1.3 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,6 @@ module Externals
3
3
  SECTION_TITLE_REGEX_NO_GROUPS = /^\s*\[(?:[^\]]*)\]\s*$/
4
4
  SECTION_TITLE_REGEX = /^\s*\[([^\]]*)\]\s*$/
5
5
 
6
-
7
6
  class Section
8
7
  attr_accessor :title_string, :body_string, :title, :rows
9
8
 
@@ -15,12 +14,11 @@ module Externals
15
14
 
16
15
  raise "Invalid section title: #{title_string}" unless title
17
16
 
18
- self.rows = body_string.strip.split(/\n/)
17
+ self.rows = body_string.strip.split("\n")
19
18
  end
20
19
 
21
-
22
- SETTING_REGEX = /^\s*([\.\w-]+)\s*=\s*([^#\n]*)(?:#[^\n]*)?$/
23
- SET_SETTING_REGEX = /^(\s*(?:[\.\w-]+)\s*=\s*)(?:[^#\n]*)(#[^\n]*)?$/
20
+ SETTING_REGEX = /^\s*([.\w-]+)\s*=\s*([^#\n]*)(?:#[^\n]*)?$/
21
+ SET_SETTING_REGEX = /^(\s*(?:[.\w-]+)\s*=\s*)(?:[^#\n]*)(#[^\n]*)?$/
24
22
 
25
23
  def attributes
26
24
  retval = {}
@@ -31,6 +29,7 @@ module Externals
31
29
  end
32
30
  retval
33
31
  end
32
+
34
33
  def setting key
35
34
  rows.each do |row|
36
35
  if row =~ SETTING_REGEX && key.to_s == $1
@@ -39,6 +38,7 @@ module Externals
39
38
  end
40
39
  nil
41
40
  end
41
+
42
42
  def set_setting key, value
43
43
  key = key.to_s
44
44
  found = nil
@@ -46,14 +46,18 @@ module Externals
46
46
  rows.each_with_index do |row, index|
47
47
  if row =~ SETTING_REGEX && key == $1
48
48
  raise "found #{key} twice!" if found
49
+
49
50
  found = index
50
51
  end
51
52
  end
52
53
 
53
54
  if found
54
55
  if rows[found] !~ SET_SETTING_REGEX
56
+ # :nocov:
55
57
  raise "thought I found the row, but didn't"
58
+ # :nocov:
56
59
  end
60
+
57
61
  rows[found] = "#{$1}#{value}#{$2}"
58
62
  else
59
63
  rows << "#{key} = #{value}"
@@ -69,6 +73,7 @@ module Externals
69
73
  rows.each_with_index do |row, index|
70
74
  if row =~ SETTING_REGEX && key == $1
71
75
  raise "found #{key} twice!" if found
76
+
72
77
  found = index
73
78
  end
74
79
  end
@@ -76,8 +81,11 @@ module Externals
76
81
  if found
77
82
  value = self[key]
78
83
  if rows[found] !~ SET_SETTING_REGEX
84
+ # :nocov:
79
85
  raise "thought I found the row, but didn't"
86
+ # :nocov:
80
87
  end
88
+
81
89
  rows.delete rows[found]
82
90
  end
83
91
  value
@@ -86,17 +94,11 @@ module Externals
86
94
  def [] key
87
95
  setting(key)
88
96
  end
97
+
89
98
  def []= key, value
90
99
  set_setting(key, value)
91
100
  end
92
101
 
93
- def add_row(row)
94
- rows << row
95
-
96
- self.body_string = body_string.chomp + "\n#{row}\n"
97
- #clear_caches
98
- end
99
-
100
102
  def to_s
101
103
  "[#{title}]\n#{rows.join("\n")}"
102
104
  end
@@ -117,7 +119,7 @@ module Externals
117
119
  def []= title, hash
118
120
  add_empty_section title
119
121
  section = self[title]
120
- hash.each_pair do |key,value|
122
+ hash.each_pair do |key, value|
121
123
  section[key] = value
122
124
  end
123
125
  end
@@ -126,12 +128,14 @@ module Externals
126
128
  sec = sections.detect{|section| section.title == sec}
127
129
 
128
130
  raise "No section found in config file for #{sec}" unless sec
131
+
129
132
  sections.delete(sec)
130
133
  end
131
134
 
132
- def add_empty_section title
135
+ def add_empty_section title
133
136
  raise "Section already exists" if self[title]
134
- sections << Section.new("[#{title.to_s}]", "")
137
+
138
+ sections << Section.new("[#{title}]", "")
135
139
  end
136
140
 
137
141
  def removed_project_paths other_config
@@ -156,12 +160,14 @@ module Externals
156
160
  file_string.each_line {|line| titles << line if line =~ SECTION_TITLE_REGEX}
157
161
  bodies = file_string.split SECTION_TITLE_REGEX_NO_GROUPS
158
162
 
159
- if titles.size > 0 && bodies.size > 0
163
+ if !titles.empty? && !bodies.empty?
160
164
  if titles.size + 1 != bodies.size
165
+ # :nocov:
161
166
  raise "bodies and sections do not match up"
167
+ # :nocov:
162
168
  end
163
169
 
164
- bodies = bodies[1..(bodies.size - 1)]
170
+ bodies = bodies[1..]
165
171
 
166
172
  (0...(bodies.size)).each do |index|
167
173
  sections << Section.new(titles[index], bodies[index])
@@ -171,13 +177,12 @@ module Externals
171
177
 
172
178
  def write path = ".externals"
173
179
  raise "no path given" unless path
174
- open(path, 'w') do |f|
175
- f.write to_s
176
- end
180
+
181
+ File.write(path, to_s)
177
182
  end
178
183
 
179
184
  def to_s
180
- sections.map(&:to_s).join("\n\n")
185
+ sections.join("\n\n")
181
186
  end
182
187
  end
183
188
  end