cliutils 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. checksums.yaml +7 -0
  2. data/.DS_Store +0 -0
  3. data/.gitignore +1 -0
  4. data/Gemfile +4 -0
  5. data/Gemfile.lock +48 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +288 -0
  8. data/README.rdoc +19 -0
  9. data/Rakefile +45 -0
  10. data/bin/cliutils +38 -0
  11. data/cliutils.gemspec +28 -0
  12. data/features/cli_manager.feature +13 -0
  13. data/features/step_definitions/cli_manager_steps.rb +1 -0
  14. data/features/support/env.rb +16 -0
  15. data/html/CLIUtils/Configuration.html +271 -0
  16. data/html/CLIUtils/Configurator.html +504 -0
  17. data/html/CLIUtils/LoggerDelegator.html +344 -0
  18. data/html/CLIUtils/Messenging.html +284 -0
  19. data/html/CLIUtils/Prefs.html +330 -0
  20. data/html/CLIUtils/PrettyIO.html +799 -0
  21. data/html/CLIUtils.html +112 -0
  22. data/html/Hash.html +453 -0
  23. data/html/Logger.html +183 -0
  24. data/html/Object.html +120 -0
  25. data/html/README_md.html +454 -0
  26. data/html/String.html +413 -0
  27. data/html/created.rid +13 -0
  28. data/html/fonts/Lato-Light.ttf +0 -0
  29. data/html/fonts/Lato-LightItalic.ttf +0 -0
  30. data/html/fonts/Lato-Regular.ttf +0 -0
  31. data/html/fonts/Lato-RegularItalic.ttf +0 -0
  32. data/html/fonts/SourceCodePro-Bold.ttf +0 -0
  33. data/html/fonts/SourceCodePro-Regular.ttf +0 -0
  34. data/html/fonts.css +167 -0
  35. data/html/images/add.png +0 -0
  36. data/html/images/arrow_up.png +0 -0
  37. data/html/images/brick.png +0 -0
  38. data/html/images/brick_link.png +0 -0
  39. data/html/images/bug.png +0 -0
  40. data/html/images/bullet_black.png +0 -0
  41. data/html/images/bullet_toggle_minus.png +0 -0
  42. data/html/images/bullet_toggle_plus.png +0 -0
  43. data/html/images/date.png +0 -0
  44. data/html/images/delete.png +0 -0
  45. data/html/images/find.png +0 -0
  46. data/html/images/loadingAnimation.gif +0 -0
  47. data/html/images/macFFBgHack.png +0 -0
  48. data/html/images/package.png +0 -0
  49. data/html/images/page_green.png +0 -0
  50. data/html/images/page_white_text.png +0 -0
  51. data/html/images/page_white_width.png +0 -0
  52. data/html/images/plugin.png +0 -0
  53. data/html/images/ruby.png +0 -0
  54. data/html/images/tag_blue.png +0 -0
  55. data/html/images/tag_green.png +0 -0
  56. data/html/images/transparent.png +0 -0
  57. data/html/images/wrench.png +0 -0
  58. data/html/images/wrench_orange.png +0 -0
  59. data/html/images/zoom.png +0 -0
  60. data/html/index.html +455 -0
  61. data/html/js/darkfish.js +140 -0
  62. data/html/js/jquery.js +18 -0
  63. data/html/js/navigation.js +142 -0
  64. data/html/js/search.js +109 -0
  65. data/html/js/search_index.js +1 -0
  66. data/html/js/searcher.js +228 -0
  67. data/html/rdoc.css +580 -0
  68. data/html/table_of_contents.html +399 -0
  69. data/lib/.DS_Store +0 -0
  70. data/lib/cliutils/configuration.rb +48 -0
  71. data/lib/cliutils/configurator.rb +119 -0
  72. data/lib/cliutils/ext/Hash+Extensions.rb +145 -0
  73. data/lib/cliutils/ext/Logger+Extensions.rb +27 -0
  74. data/lib/cliutils/ext/String+Extensions.rb +26 -0
  75. data/lib/cliutils/logger-delegator.rb +66 -0
  76. data/lib/cliutils/messenging.rb +51 -0
  77. data/lib/cliutils/prefs.rb +116 -0
  78. data/lib/cliutils/pretty-io.rb +255 -0
  79. data/lib/cliutils/version.rb +3 -0
  80. data/lib/cliutils.rb +15 -0
  81. data/res/readme-images/messenger-types-1.png +0 -0
  82. data/res/readme-images/messenger-warn.png +0 -0
  83. data/res/readme-images/multi-logger.png +0 -0
  84. data/res/readme-images/prettyio-color-chart.png +0 -0
  85. data/res/readme-images/prettyio-gnarly-text.png +0 -0
  86. data/res/readme-images/prettyio-red-text.png +0 -0
  87. data/res/readme-images/prompting.png +0 -0
  88. data/res/readme-images/wrapping.png +0 -0
  89. data/test/configurator_test.rb +50 -0
  90. data/test/hash_extensions_test.rb +50 -0
  91. data/test/logger_extensions_test.rb +16 -0
  92. data/test/messenging_test.rb +53 -0
  93. data/test/prefs_test.rb +29 -0
  94. data/test/string_extesions_test.rb +14 -0
  95. data/test/test_files/prefstest.yaml +33 -0
  96. metadata +199 -0
@@ -0,0 +1,399 @@
1
+ <!DOCTYPE html>
2
+
3
+ <html>
4
+ <head>
5
+ <meta charset="UTF-8">
6
+
7
+ <title>Table of Contents - CLIUtils</title>
8
+
9
+ <link href="./fonts.css" rel="stylesheet">
10
+ <link href="./rdoc.css" rel="stylesheet">
11
+
12
+ <script type="text/javascript">
13
+ var rdoc_rel_prefix = "./";
14
+ </script>
15
+
16
+ <script src="./js/jquery.js"></script>
17
+ <script src="./js/navigation.js"></script>
18
+ <script src="./js/search_index.js"></script>
19
+ <script src="./js/search.js"></script>
20
+ <script src="./js/searcher.js"></script>
21
+ <script src="./js/darkfish.js"></script>
22
+
23
+
24
+ <body id="top" class="table-of-contents">
25
+ <main role="main">
26
+ <h1 class="class">Table of Contents - CLIUtils</h1>
27
+
28
+ <h2 id="pages">Pages</h2>
29
+ <ul>
30
+ <li class="file">
31
+ <a href="README_md.html">README</a>
32
+
33
+ <ul>
34
+ <li><a href="README_md.html#label-CLIUtils">CLIUtils</a>
35
+ <li><a href="README_md.html#label-Why%3F">Why?</a>
36
+ <li><a href="README_md.html#label-Installation">Installation</a>
37
+ <li><a href="README_md.html#label-Usage">Usage</a>
38
+ <li><a href="README_md.html#label-Libraries">Libraries</a>
39
+ <li><a href="README_md.html#label-PrettyIO">PrettyIO</a>
40
+ <li><a href="README_md.html#label-Messenging">Messenging</a>
41
+ <li><a href="README_md.html#label-Messenging+Methods">Messenging Methods</a>
42
+ <li><a href="README_md.html#label-Message+Wrapping">Message Wrapping</a>
43
+ <li><a href="README_md.html#label-Prompting">Prompting</a>
44
+ <li><a href="README_md.html#label-Logging">Logging</a>
45
+ <li><a href="README_md.html#label-Configuration">Configuration</a>
46
+ <li><a href="README_md.html#label-Loading+a+Configuration+File">Loading a Configuration File</a>
47
+ <li><a href="README_md.html#label-Adding%2FRemoving+Sections">Adding/Removing Sections</a>
48
+ <li><a href="README_md.html#label-Adding+Data+to+Sections">Adding Data to Sections</a>
49
+ <li><a href="README_md.html#label-Saving+to+a+File">Saving to a File</a>
50
+ <li><a href="README_md.html#label-Known+Issues">Known Issues</a>
51
+ <li><a href="README_md.html#label-Bugs+and+Feature+Requests">Bugs and Feature Requests</a>
52
+ <li><a href="README_md.html#label-Contributing">Contributing</a>
53
+ <li><a href="README_md.html#label-License">License</a>
54
+ </ul>
55
+ </li>
56
+
57
+ </ul>
58
+
59
+ <h2 id="classes">Classes and Modules</h2>
60
+ <ul>
61
+ <li class="module">
62
+ <a href="CLIUtils.html">CLIUtils</a>
63
+ </li>
64
+ <li class="module">
65
+ <a href="CLIUtils/Configuration.html">CLIUtils::Configuration</a>
66
+
67
+ <ul>
68
+ <li><a href="CLIUtils/Configuration.html#label-"></a>
69
+ <li><a href="CLIUtils/Configuration.html#label-"></a>
70
+ </ul>
71
+ </li>
72
+ <li class="class">
73
+ <a href="CLIUtils/Configurator.html">CLIUtils::Configurator</a>
74
+
75
+ <ul>
76
+ <li><a href="CLIUtils/Configurator.html#label-"></a>
77
+ <li><a href="CLIUtils/Configurator.html#label-"></a>
78
+ </ul>
79
+ </li>
80
+ <li class="class">
81
+ <a href="CLIUtils/LoggerDelegator.html">CLIUtils::LoggerDelegator</a>
82
+
83
+ <ul>
84
+ <li><a href="CLIUtils/LoggerDelegator.html#label-"></a>
85
+ <li><a href="CLIUtils/LoggerDelegator.html#label-"></a>
86
+ </ul>
87
+ </li>
88
+ <li class="module">
89
+ <a href="CLIUtils/Messenging.html">CLIUtils::Messenging</a>
90
+
91
+ <ul>
92
+ <li><a href="CLIUtils/Messenging.html#label-"></a>
93
+ <li><a href="CLIUtils/Messenging.html#label-"></a>
94
+ </ul>
95
+ </li>
96
+ <li class="class">
97
+ <a href="CLIUtils/Prefs.html">CLIUtils::Prefs</a>
98
+
99
+ <ul>
100
+ <li><a href="CLIUtils/Prefs.html#label-"></a>
101
+ <li><a href="CLIUtils/Prefs.html#label-"></a>
102
+ </ul>
103
+ </li>
104
+ <li class="module">
105
+ <a href="CLIUtils/PrettyIO.html">CLIUtils::PrettyIO</a>
106
+
107
+ <ul>
108
+ <li><a href="CLIUtils/PrettyIO.html#label-"></a>
109
+ <li><a href="CLIUtils/PrettyIO.html#label-"></a>
110
+ </ul>
111
+ </li>
112
+ <li class="class">
113
+ <a href="Hash.html">Hash</a>
114
+
115
+ <ul>
116
+ <li><a href="Hash.html#label-"></a>
117
+ <li><a href="Hash.html#label-"></a>
118
+ </ul>
119
+ </li>
120
+ <li class="class">
121
+ <a href="Logger.html">Logger</a>
122
+
123
+ <ul>
124
+ <li><a href="Logger.html#label-"></a>
125
+ <li><a href="Logger.html#label-"></a>
126
+ </ul>
127
+ </li>
128
+ <li class="class">
129
+ <a href="String.html">String</a>
130
+
131
+ <ul>
132
+ <li><a href="String.html#label-"></a>
133
+ <li><a href="String.html#label-"></a>
134
+ </ul>
135
+ </li>
136
+ </ul>
137
+
138
+ <h2 id="methods">Methods</h2>
139
+ <ul>
140
+
141
+ <li class="method">
142
+ <a href="Logger.html#method-c-custom_level">::custom_level</a>
143
+ &mdash;
144
+ <span class="container">Logger</span>
145
+
146
+ <li class="method">
147
+ <a href="CLIUtils/LoggerDelegator.html#method-c-delegate">::delegate</a>
148
+ &mdash;
149
+ <span class="container">CLIUtils::LoggerDelegator</span>
150
+
151
+ <li class="method">
152
+ <a href="CLIUtils/PrettyIO.html#method-c-included">::included</a>
153
+ &mdash;
154
+ <span class="container">CLIUtils::PrettyIO</span>
155
+
156
+ <li class="method">
157
+ <a href="CLIUtils/Configuration.html#method-c-included">::included</a>
158
+ &mdash;
159
+ <span class="container">CLIUtils::Configuration</span>
160
+
161
+ <li class="method">
162
+ <a href="CLIUtils/Messenging.html#method-c-included">::included</a>
163
+ &mdash;
164
+ <span class="container">CLIUtils::Messenging</span>
165
+
166
+ <li class="method">
167
+ <a href="CLIUtils/Configurator.html#method-c-new">::new</a>
168
+ &mdash;
169
+ <span class="container">CLIUtils::Configurator</span>
170
+
171
+ <li class="method">
172
+ <a href="CLIUtils/Prefs.html#method-c-new">::new</a>
173
+ &mdash;
174
+ <span class="container">CLIUtils::Prefs</span>
175
+
176
+ <li class="method">
177
+ <a href="CLIUtils/LoggerDelegator.html#method-c-new">::new</a>
178
+ &mdash;
179
+ <span class="container">CLIUtils::LoggerDelegator</span>
180
+
181
+ <li class="method">
182
+ <a href="CLIUtils/PrettyIO.html#method-c-wrap">::wrap</a>
183
+ &mdash;
184
+ <span class="container">CLIUtils::PrettyIO</span>
185
+
186
+ <li class="method">
187
+ <a href="CLIUtils/PrettyIO.html#method-c-wrap_at">::wrap_at</a>
188
+ &mdash;
189
+ <span class="container">CLIUtils::PrettyIO</span>
190
+
191
+ <li class="method">
192
+ <a href="CLIUtils/PrettyIO.html#method-c-wrap_limit">::wrap_limit</a>
193
+ &mdash;
194
+ <span class="container">CLIUtils::PrettyIO</span>
195
+
196
+ <li class="method">
197
+ <a href="CLIUtils/Configurator.html#method-i-add_section">#add_section</a>
198
+ &mdash;
199
+ <span class="container">CLIUtils::Configurator</span>
200
+
201
+ <li class="method">
202
+ <a href="CLIUtils/Prefs.html#method-i-ask">#ask</a>
203
+ &mdash;
204
+ <span class="container">CLIUtils::Prefs</span>
205
+
206
+ <li class="method">
207
+ <a href="CLIUtils/LoggerDelegator.html#method-i-attach">#attach</a>
208
+ &mdash;
209
+ <span class="container">CLIUtils::LoggerDelegator</span>
210
+
211
+ <li class="method">
212
+ <a href="String.html#method-i-blue">#blue</a>
213
+ &mdash;
214
+ <span class="container">String</span>
215
+
216
+ <li class="method">
217
+ <a href="CLIUtils/PrettyIO.html#method-i-color_chart">#color_chart</a>
218
+ &mdash;
219
+ <span class="container">CLIUtils::PrettyIO</span>
220
+
221
+ <li class="method">
222
+ <a href="String.html#method-i-colorize">#colorize</a>
223
+ &mdash;
224
+ <span class="container">String</span>
225
+
226
+ <li class="method">
227
+ <a href="CLIUtils/Configuration.html#method-i-configuration">#configuration</a>
228
+ &mdash;
229
+ <span class="container">CLIUtils::Configuration</span>
230
+
231
+ <li class="method">
232
+ <a href="String.html#method-i-cyan">#cyan</a>
233
+ &mdash;
234
+ <span class="container">String</span>
235
+
236
+ <li class="method">
237
+ <a href="CLIUtils/PrettyIO.html#method-i-debug">#debug</a>
238
+ &mdash;
239
+ <span class="container">CLIUtils::PrettyIO</span>
240
+
241
+ <li class="method">
242
+ <a href="Hash.html#method-i-deep_merge-21">#deep_merge!</a>
243
+ &mdash;
244
+ <span class="container">Hash</span>
245
+
246
+ <li class="method">
247
+ <a href="Hash.html#method-i-deep_stringify_keys">#deep_stringify_keys</a>
248
+ &mdash;
249
+ <span class="container">Hash</span>
250
+
251
+ <li class="method">
252
+ <a href="Hash.html#method-i-deep_stringify_keys-21">#deep_stringify_keys!</a>
253
+ &mdash;
254
+ <span class="container">Hash</span>
255
+
256
+ <li class="method">
257
+ <a href="Hash.html#method-i-deep_symbolize_keys">#deep_symbolize_keys</a>
258
+ &mdash;
259
+ <span class="container">Hash</span>
260
+
261
+ <li class="method">
262
+ <a href="Hash.html#method-i-deep_symbolize_keys-21">#deep_symbolize_keys!</a>
263
+ &mdash;
264
+ <span class="container">Hash</span>
265
+
266
+ <li class="method">
267
+ <a href="Hash.html#method-i-deep_transform_keys">#deep_transform_keys</a>
268
+ &mdash;
269
+ <span class="container">Hash</span>
270
+
271
+ <li class="method">
272
+ <a href="Hash.html#method-i-deep_transform_keys-21">#deep_transform_keys!</a>
273
+ &mdash;
274
+ <span class="container">Hash</span>
275
+
276
+ <li class="method">
277
+ <a href="CLIUtils/Messenging.html#method-i-default_instance">#default_instance</a>
278
+ &mdash;
279
+ <span class="container">CLIUtils::Messenging</span>
280
+
281
+ <li class="method">
282
+ <a href="CLIUtils/Configurator.html#method-i-delete_section">#delete_section</a>
283
+ &mdash;
284
+ <span class="container">CLIUtils::Configurator</span>
285
+
286
+ <li class="method">
287
+ <a href="CLIUtils/LoggerDelegator.html#method-i-detach">#detach</a>
288
+ &mdash;
289
+ <span class="container">CLIUtils::LoggerDelegator</span>
290
+
291
+ <li class="method">
292
+ <a href="CLIUtils/PrettyIO.html#method-i-error">#error</a>
293
+ &mdash;
294
+ <span class="container">CLIUtils::PrettyIO</span>
295
+
296
+ <li class="method">
297
+ <a href="String.html#method-i-green">#green</a>
298
+ &mdash;
299
+ <span class="container">String</span>
300
+
301
+ <li class="method">
302
+ <a href="CLIUtils/PrettyIO.html#method-i-info">#info</a>
303
+ &mdash;
304
+ <span class="container">CLIUtils::PrettyIO</span>
305
+
306
+ <li class="method">
307
+ <a href="CLIUtils/PrettyIO.html#method-i-info_block">#info_block</a>
308
+ &mdash;
309
+ <span class="container">CLIUtils::PrettyIO</span>
310
+
311
+ <li class="method">
312
+ <a href="CLIUtils/Configurator.html#method-i-ingest_prefs">#ingest_prefs</a>
313
+ &mdash;
314
+ <span class="container">CLIUtils::Configurator</span>
315
+
316
+ <li class="method">
317
+ <a href="CLIUtils/Configuration.html#method-i-load_configuration">#load_configuration</a>
318
+ &mdash;
319
+ <span class="container">CLIUtils::Configuration</span>
320
+
321
+ <li class="method">
322
+ <a href="CLIUtils/PrettyIO.html#method-i-log">#log</a>
323
+ &mdash;
324
+ <span class="container">CLIUtils::PrettyIO</span>
325
+
326
+ <li class="method">
327
+ <a href="CLIUtils/Messenging.html#method-i-messenger">#messenger</a>
328
+ &mdash;
329
+ <span class="container">CLIUtils::Messenging</span>
330
+
331
+ <li class="method">
332
+ <a href="CLIUtils/Configurator.html#method-i-method_missing">#method_missing</a>
333
+ &mdash;
334
+ <span class="container">CLIUtils::Configurator</span>
335
+
336
+ <li class="method">
337
+ <a href="CLIUtils/PrettyIO.html#method-i-prompt">#prompt</a>
338
+ &mdash;
339
+ <span class="container">CLIUtils::PrettyIO</span>
340
+
341
+ <li class="method">
342
+ <a href="String.html#method-i-purple">#purple</a>
343
+ &mdash;
344
+ <span class="container">String</span>
345
+
346
+ <li class="method">
347
+ <a href="String.html#method-i-red">#red</a>
348
+ &mdash;
349
+ <span class="container">String</span>
350
+
351
+ <li class="method">
352
+ <a href="CLIUtils/Configurator.html#method-i-reset">#reset</a>
353
+ &mdash;
354
+ <span class="container">CLIUtils::Configurator</span>
355
+
356
+ <li class="method">
357
+ <a href="CLIUtils/Configurator.html#method-i-save">#save</a>
358
+ &mdash;
359
+ <span class="container">CLIUtils::Configurator</span>
360
+
361
+ <li class="method">
362
+ <a href="CLIUtils/PrettyIO.html#method-i-section">#section</a>
363
+ &mdash;
364
+ <span class="container">CLIUtils::PrettyIO</span>
365
+
366
+ <li class="method">
367
+ <a href="CLIUtils/PrettyIO.html#method-i-section_block">#section_block</a>
368
+ &mdash;
369
+ <span class="container">CLIUtils::PrettyIO</span>
370
+
371
+ <li class="method">
372
+ <a href="CLIUtils/PrettyIO.html#method-i-success">#success</a>
373
+ &mdash;
374
+ <span class="container">CLIUtils::PrettyIO</span>
375
+
376
+ <li class="method">
377
+ <a href="CLIUtils/PrettyIO.html#method-i-warn">#warn</a>
378
+ &mdash;
379
+ <span class="container">CLIUtils::PrettyIO</span>
380
+
381
+ <li class="method">
382
+ <a href="String.html#method-i-white">#white</a>
383
+ &mdash;
384
+ <span class="container">String</span>
385
+
386
+ <li class="method">
387
+ <a href="String.html#method-i-yellow">#yellow</a>
388
+ &mdash;
389
+ <span class="container">String</span>
390
+ </ul>
391
+ </main>
392
+
393
+
394
+ <footer id="validator-badges" role="contentinfo">
395
+ <p><a href="http://validator.w3.org/check/referer">Validate</a>
396
+ <p>Generated by <a href="http://rdoc.rubyforge.org">RDoc</a> 4.1.1.
397
+ <p>Based on <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
398
+ </footer>
399
+
data/lib/.DS_Store ADDED
Binary file
@@ -0,0 +1,48 @@
1
+ require 'yaml'
2
+
3
+ module CLIUtils
4
+ # ======================================================
5
+ # Configuration Class
6
+ #
7
+ # Manages any configuration values and the flat YAML file
8
+ # into which they get stored.
9
+ # ======================================================
10
+ module Configuration
11
+ # ====================================================
12
+ # Methods
13
+ # ====================================================
14
+ # ----------------------------------------------------
15
+ # included method
16
+ #
17
+ # Hook called when this module gets mixed in; extends
18
+ # the includer with the methods defined here.
19
+ # @param k The includer
20
+ # @return Void
21
+ # ----------------------------------------------------
22
+ def self.included(k)
23
+ k.extend(self)
24
+ end
25
+
26
+ # ----------------------------------------------------
27
+ # configuration method
28
+ #
29
+ # Singleton method to return (or initialize, if needed)
30
+ # a Configurator.
31
+ # @return Configurator
32
+ # ----------------------------------------------------
33
+ def configuration
34
+ @configuration ||= Configurator.new('~/.default-cliutils')
35
+ end
36
+
37
+ # ----------------------------------------------------
38
+ # load_configuration method
39
+ #
40
+ # Initializes a Configurator with the passed filepath.
41
+ # @param path The path to the config file
42
+ # @return Void
43
+ # ----------------------------------------------------
44
+ def load_configuration(path)
45
+ @configuration = Configurator.new(path)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,119 @@
1
+ require 'yaml'
2
+
3
+ module CLIUtils
4
+ # ======================================================
5
+ # Configuration Class
6
+ #
7
+ # Manages any configuration values and the flat YAML file
8
+ # into which they get stored.
9
+ # ======================================================
10
+ class Configurator
11
+ # ====================================================
12
+ # Attributes
13
+ # ====================================================
14
+ attr_reader :config_path, :data
15
+
16
+ # ====================================================
17
+ # Methods
18
+ # ====================================================
19
+ # ----------------------------------------------------
20
+ # initialize method
21
+ #
22
+ # Initializes configuration from a flat file.
23
+ # @param path The filepath to the config YAML
24
+ # @return void
25
+ # ----------------------------------------------------
26
+ def initialize(path)
27
+ _path = File.expand_path(path)
28
+ @config_path = _path
29
+ @data = {}
30
+
31
+ if File.exists?(_path)
32
+ data = YAML::load_file(_path)
33
+ @data.deep_merge!(data).deep_symbolize_keys!
34
+ end
35
+ end
36
+
37
+ # ----------------------------------------------------
38
+ # add_section method
39
+ #
40
+ # Adds a new section to the config file (if it doesn't
41
+ # already exist).
42
+ # @param section_name The section to add
43
+ # @return Void
44
+ # ----------------------------------------------------
45
+ def add_section(section_name)
46
+ if !@data.key?(section_name)
47
+ @data[section_name] = {}
48
+ else
49
+ fail "Section already exists: #{ section_name }"
50
+ end
51
+ end
52
+
53
+ # ----------------------------------------------------
54
+ # delete_section method
55
+ #
56
+ # Removes a section to the config file (if it exists).
57
+ # @param section_name The section to remove
58
+ # @return Void
59
+ # ----------------------------------------------------
60
+ def delete_section(section_name)
61
+ if @data.key?(section_name)
62
+ @data.delete(section_name)
63
+ else
64
+ fail "Cannot delete nonexistent section: #{ section_name }"
65
+ end
66
+ end
67
+
68
+ # ----------------------------------------------------
69
+ # ingest_prefs method
70
+ #
71
+ # Ingests a Prefs class and adds its answers to the
72
+ # configuration data.
73
+ # @param prefs The Prefs class to examine
74
+ # @return Void
75
+ # ----------------------------------------------------
76
+ def ingest_prefs(prefs)
77
+ fail 'Invaid Prefs class' if !prefs.kind_of?(Prefs) || prefs.answers.nil?
78
+ prefs.answers.each do |p|
79
+ add_section(p[:section]) unless @data.key?(p[:section])
80
+ @data[p[:section]].merge!(p[:key] => p[:answer])
81
+ end
82
+ end
83
+
84
+ # ----------------------------------------------------
85
+ # method_missing method
86
+ #
87
+ # Allows this module to return data from the config
88
+ # Hash when given a method name that matches a key.
89
+ # @param name
90
+ # @param *args
91
+ # @param &block
92
+ # @return Hash
93
+ # ----------------------------------------------------
94
+ def method_missing(name, *args, &block)
95
+ @data[name.to_sym] || @data.merge!(name.to_sym => {})
96
+ end
97
+
98
+ # ----------------------------------------------------
99
+ # reset method
100
+ #
101
+ # Clears the configuration data.
102
+ # @return Void
103
+ # ----------------------------------------------------
104
+ def reset
105
+ @data = {}
106
+ end
107
+
108
+ # ----------------------------------------------------
109
+ # save method
110
+ #
111
+ # Saves the configuration data to the previously
112
+ # stored flat file.
113
+ # @return Void
114
+ # ----------------------------------------------------
115
+ def save
116
+ File.open(@config_path, 'w') { |f| f.write(@data.deep_stringify_keys.to_yaml) }
117
+ end
118
+ end
119
+ end