confoog 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 426cd876ac4934a3f852d3bd3a488d5eb67e2457
4
- data.tar.gz: 49c365ae6ef73ba9dc8beac001aa2f08d5cd01b1
3
+ metadata.gz: f947a6f8d6ac5314c4cc95283c0f96342073f5f0
4
+ data.tar.gz: ce1050b7ef2b58be9e4aacdee1fe209986474873
5
5
  SHA512:
6
- metadata.gz: 1eeeced36fe38ce14adc23bc85a4502b1f2a7296b32fad3741b585d310ec7a4a830c5b592ca7a49e3b47bc9154fb83b6ba4f1cd16373b9965cf57a797bee7527
7
- data.tar.gz: a9a1993322fb9f67db21274371a97aed463d0deab4d7e506d9c5e9e1d406b26c82882d71b0405bb1d5ca0f5d381d4fe051daf8736bba266e6e1b49991c9ca9fc
6
+ metadata.gz: 88a306dd5acaabd9f402ca4a772f2a9dbff146beb4be81dfec2ece2461c4a953e3d59c018b87cecea7a53fd5a56192183bd109a79aaed33bbf56ddfa7b74ec78
7
+ data.tar.gz: 17f9def6311f6ad7f41ee7fe52b23135d60c1fd41e84d888c6cb02b6e2d9604fdbb937f5191150320d4335a07bce0eb7987c9a016ee9fc373dee0940d1158d81
data/README.md CHANGED
@@ -114,6 +114,7 @@ ERR_CANT_CREATE_FILE = 4 # cannot create the requested configuration file
114
114
  ERR_NOT_WRITING_EMPTY_FILE = 8 # not attempting to save an empty configuration
115
115
  ERR_CANT_SAVE_CONFIGURATION = 16 # Failed to save the configuration file
116
116
  ERR_NOT_LOADING_EMPTY_FILE = 32 # not atempting to load an empty config file
117
+ ERR_CANT_LOAD = 64 # Cannot load configuration data from file.
117
118
 
118
119
  INFO_FILE_CREATED = 256 # Information - specified file was created
119
120
  INFO_FILE_LOADED = 512 # Information - Config file was loaded successfully
data/lib/confoog.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'confoog/version'
2
- require 'confoog/utility'
2
+ require 'confoog/status'
3
3
  require 'yaml'
4
4
 
5
5
  # rubocop:disable LineLength
@@ -10,37 +10,6 @@ module Confoog
10
10
  # The default filename used if none specified when created.
11
11
  DEFAULT_CONFIG = '.confoog'
12
12
 
13
- # Error messages to be returned
14
-
15
- # No error condition exists
16
- ERR_NO_ERROR = 0
17
- # The specified file does not exist
18
- ERR_FILE_NOT_EXIST = 1
19
- # You cannot change location or filename after class is instantiated
20
- ERR_CANT_CHANGE = 2
21
- # Was unable to create the specified file
22
- ERR_CANT_CREATE_FILE = 4
23
- # There are no configuration variables set, so not writing empty file
24
- ERR_NOT_WRITING_EMPTY_FILE = 8
25
- # Cannot save to the specified file for some reason
26
- ERR_CANT_SAVE_CONFIGURATION = 16
27
- # The specified file is empty so not trying to load settings from it
28
- ERR_NOT_LOADING_EMPTY_FILE = 32
29
-
30
- # Info messages to be returned
31
-
32
- # Information - file was created successfully
33
- INFO_FILE_CREATED = 256
34
- # Information - configuration was successfully loaded
35
- INFO_FILE_LOADED = 512
36
-
37
- # Hash containing text versions of the assorted error severity.
38
- OUTPUT_SEVERITY = {
39
- ERR: 'Error',
40
- WARN: 'Warning',
41
- INFO: 'Information'
42
- }
43
-
44
13
  # Hash containing default values of initialization variables
45
14
  DEFAULT_OPTIONS = {
46
15
  create_file: false,
@@ -84,7 +53,6 @@ module Confoog
84
53
  # settings[50][:two]
85
54
  # # => "for the show"
86
55
  class Settings
87
- include ConfoogUtils
88
56
  attr_reader :filename, :location, :status
89
57
 
90
58
  # rubocop:enable LineLength
@@ -100,14 +68,14 @@ module Confoog
100
68
  @options.default = false
101
69
 
102
70
  # Hash containing any error or return from methods
103
- @status = {}
71
+ @status = Status.new(@options[:quiet], @options[:prefix])
104
72
  @location = @options[:location]
105
73
  @filename = @options[:filename]
106
74
 
107
75
  @config = {}
108
76
 
109
77
  # clear the error condition as default.
110
- status_set(errors: ERR_NO_ERROR)
78
+ @status.clear_error
111
79
  # make sure the file exists or can be created...
112
80
  check_exists(options)
113
81
 
@@ -115,7 +83,7 @@ module Confoog
115
83
  load unless @options[:autoload] == false
116
84
  end
117
85
 
118
- # Return the value of the 'auto_save' option.
86
+ # Return the value of the 'autosave' option.
119
87
  # @example
120
88
  # autosave_status = settings.autosave
121
89
  # => true
@@ -125,7 +93,7 @@ module Confoog
125
93
  @options[:autosave]
126
94
  end
127
95
 
128
- # Change the 'auto_save' option.
96
+ # Change the 'autosave' option.
129
97
  # @example
130
98
  # settings.autosave = false
131
99
  # => false
@@ -141,7 +109,7 @@ module Confoog
141
109
  # @param [None]
142
110
  # @return [Boolean] True if we are not writing to the console on error
143
111
  def quiet
144
- @options[:quiet]
112
+ @status.quiet
145
113
  end
146
114
 
147
115
  # Change the 'quiet' option.
@@ -150,7 +118,7 @@ module Confoog
150
118
  # @return [Boolean] The new value [true | false]
151
119
  # @param quiet [Boolean] True to send messages to console for errors.
152
120
  def quiet=(quiet)
153
- @options[:quiet] = quiet
121
+ @status.quiet = quiet
154
122
  end
155
123
 
156
124
  # Save the entire configuration (@config) to the YAML file.
@@ -162,9 +130,7 @@ module Confoog
162
130
  if @config.count > 0
163
131
  save_to_yaml
164
132
  else
165
- console_output("Not saving empty configuration data to #{config_path}",
166
- OUTPUT_SEVERITY[:WARN])
167
- status_set(errors: ERR_NOT_WRITING_EMPTY_FILE)
133
+ @status.set(errors: Status::ERR_NOT_WRITING_EMPTY_FILE)
168
134
  end
169
135
  end
170
136
 
@@ -175,33 +141,28 @@ module Confoog
175
141
  # @return Unspecified
176
142
  def load
177
143
  @config = YAML.load_file(config_path)
178
- status_set(errors: INFO_FILE_LOADED)
144
+ @status.set(errors: Status::INFO_FILE_LOADED)
179
145
  if @config == false
180
- console_output("Configuration file #{config_path} is empty!",
181
- OUTPUT_SEVERITY[:WARN])
182
- status_set(errors: ERR_NOT_LOADING_EMPTY_FILE)
146
+ # console_output("Configuration file #{config_path} is empty!",
147
+ # OUTPUT_SEVERITY[:WARN])
148
+ @status.set(errors: Status::ERR_NOT_LOADING_EMPTY_FILE)
183
149
  end
184
150
  rescue
185
- console_output("Cannot load configuration data from #{config_path}",
186
- OUTPUT_SEVERITY[:ERR])
151
+ @status.set(errors: Status::ERR_CANT_LOAD)
187
152
  end
188
153
 
189
154
  # dummy method currently to stop changing location by caller once created,
190
155
  # @return [hash] an error flag in the ':status' variable.
191
156
  # @param [Optional] Parameter is ignored
192
157
  def location=(*)
193
- status_set(errors: ERR_CANT_CHANGE)
194
- console_output('Cannot change file location after creation',
195
- OUTPUT_SEVERITY[:WARN])
158
+ @status.set(errors: Status::ERR_CANT_CHANGE)
196
159
  end
197
160
 
198
161
  # dummy method currently to stop changing filename by caller once created,
199
162
  # @return [hash] an error flag in the ':status' variable.
200
163
  # @param optional Parameter is ignored
201
164
  def filename=(*)
202
- status_set(errors: ERR_CANT_CHANGE)
203
- console_output('Cannot change filename after creation',
204
- OUTPUT_SEVERITY[:WARN])
165
+ @status.set(errors: Status::ERR_CANT_CHANGE)
205
166
  end
206
167
 
207
168
  # Read the configuration key (key)
@@ -213,7 +174,7 @@ module Confoog
213
174
  end
214
175
 
215
176
  # Set a configuration key.
216
- # If auto_save: true then will also update the config file (default is true)
177
+ # If autosave: true then will also update the config file (default is true)
217
178
  # @example
218
179
  # settings[:key] = "Value"
219
180
  # settings[:array] = ["first", "second", "third"]
@@ -239,37 +200,25 @@ module Confoog
239
200
  file.write(@config.to_yaml)
240
201
  file.close
241
202
  rescue
242
- status_set(errors: ERR_CANT_SAVE_CONFIGURATION)
243
- console_output("Cannot save configuration data to #{config_path}",
244
- OUTPUT_SEVERITY[:ERR])
203
+ @status.set(errors: Status::ERR_CANT_SAVE_CONFIGURATION)
245
204
  end
246
205
 
247
206
  def create_new_file
248
207
  File.new(config_path, 'w').close
249
- status_set(config_exists: true, errors: INFO_FILE_CREATED)
208
+ @status.set(config_exists: true, errors: Status::INFO_FILE_CREATED)
250
209
  rescue
251
- status_set(config_exists: false, errors: ERR_CANT_CREATE_FILE)
252
- console_output('Cannot create the specified Configuration file!',
253
- OUTPUT_SEVERITY[:ERR])
254
- end
255
-
256
- def status_set(status)
257
- status.each do |key, value|
258
- @status[key] = value
259
- end
210
+ @status.set(config_exists: false, errors: Status::ERR_CANT_CREATE_FILE)
260
211
  end
261
212
 
262
213
  def check_exists(options)
263
- status_set(config_exists: true)
214
+ @status[:config_exists] = true
264
215
  return if File.exist?(config_path)
265
216
 
266
217
  # file does not exist so we create if requested otherwise error out
267
218
  if options[:create_file] == true
268
219
  create_new_file
269
220
  else
270
- status_set(config_exists: false, errors: ERR_FILE_NOT_EXIST)
271
- console_output('The specified Configuration file does not exist.',
272
- OUTPUT_SEVERITY[:ERR])
221
+ @status.set(config_exists: false, errors: Status::ERR_FILE_NOT_EXIST)
273
222
  end
274
223
  end
275
224
  end
@@ -0,0 +1,113 @@
1
+ # Provide error messages and console output when required.
2
+ class Status
3
+ # @!attribute prefix
4
+ # @return [String] String to pre-pend on any error put to console
5
+ attr_accessor :prefix
6
+ # @!attribute quiet
7
+ # @return [Boolean] Do we output anything to console or not
8
+ attr_accessor :quiet
9
+ # No error condition exists
10
+ ERR_NO_ERROR = 0
11
+ # The specified file does not exist
12
+ ERR_FILE_NOT_EXIST = 1
13
+ # You cannot change location or filename after class is instantiated
14
+ ERR_CANT_CHANGE = 2
15
+ # Was unable to create the specified file
16
+ ERR_CANT_CREATE_FILE = 4
17
+ # There are no configuration variables set, so not writing empty file
18
+ ERR_NOT_WRITING_EMPTY_FILE = 8
19
+ # Cannot save to the specified file for some reason
20
+ ERR_CANT_SAVE_CONFIGURATION = 16
21
+ # The specified file is empty so not trying to load settings from it
22
+ ERR_NOT_LOADING_EMPTY_FILE = 32
23
+ # Cannot load the specified file for some reason.
24
+ ERR_CANT_LOAD = 64
25
+
26
+ # Information - file was created successfully
27
+ INFO_FILE_CREATED = 256
28
+ # Information - configuration was successfully loaded
29
+ INFO_FILE_LOADED = 512
30
+
31
+ # Hash containing text versions of the assorted error severity.
32
+ OUTPUT_SEVERITY = {
33
+ ERR: 'Error',
34
+ WARN: 'Warning',
35
+ INFO: 'Information'
36
+ }
37
+
38
+ # Hash containing the error messages for each ERR condition. If an ERR does
39
+ # not have a message, there will be no output.
40
+ ERROR_STRINGS = {
41
+ ERR_FILE_NOT_EXIST => 'The specified Configuration file does not exist.',
42
+ ERR_CANT_CHANGE => 'Cannot change filename after creation',
43
+ ERR_CANT_CREATE_FILE => 'Cannot create the specified Configuration file!',
44
+ ERR_NOT_WRITING_EMPTY_FILE => 'Not saving empty configuration data!',
45
+ ERR_CANT_SAVE_CONFIGURATION => 'Cannot save configuration data!',
46
+ ERR_NOT_LOADING_EMPTY_FILE => 'The configuration file is empty!',
47
+ ERR_CANT_LOAD => 'Cannot load configuration Data!'
48
+ }
49
+
50
+ # Class initializer.
51
+ # @example
52
+ # status = Status.new(quiet: true, prefix: "My Fab App")
53
+ # @param quiet [Boolean] Do we output to the console?
54
+ # @param prefix [String] Prefix to put before any error message.
55
+ def initialize(quiet, prefix)
56
+ # Initialize the status container to an empty hash. Will return nil
57
+ # for missing keys by default.
58
+ @status = {}
59
+ @quiet = quiet
60
+ @prefix = prefix
61
+ end
62
+
63
+ # Clear the error status.
64
+ # @example
65
+ # status.clear_error
66
+ # @return [Integer] 0
67
+ def clear_error
68
+ @status[:errors] = ERR_NO_ERROR
69
+ end
70
+
71
+ # Read the value of a status.
72
+ # @example
73
+ # key = status[:key]
74
+ # @return [<various>] Return value depends on the type of variable stored
75
+ def [](key)
76
+ @status[key]
77
+ end
78
+
79
+ # Set the value of status.
80
+ # @example
81
+ # status[:key] = "Value"
82
+ # status[:error] = ERR_NOT_LOADING_EMPTY_FILE
83
+ # @param key [Any] Key name to be added or updated.
84
+ # @param value [Any] New value for the status. May be any type.
85
+ # @return [<various>] Returns the value that was assigned.
86
+ def []=(key, value)
87
+ @status[key] = value
88
+ end
89
+
90
+ # Set one or multiple status variables, optionally outputing a console
91
+ # message if one exists for that status.
92
+ # @example
93
+ # status.set(errors: Status::ERR_CANT_SAVE_CONFIGURATION)
94
+ # @param status [Hash] one or more hash-pairs of status information.
95
+ # @return Unspecified
96
+ def set(status)
97
+ status.each do |key, value|
98
+ @status[key] = value
99
+ end
100
+ return if ERROR_STRINGS[@status[:errors]].nil?
101
+ severity = (@status[:errors] <= 64) ? 'Error' : 'Info'
102
+ console_output(ERROR_STRINGS[@status[:errors]], severity)
103
+ end
104
+
105
+ private
106
+
107
+ # Display output to the console with the severity noted, unless we are quiet.
108
+ # @param [String] message
109
+ def console_output(message, severity)
110
+ return unless @quiet == false
111
+ $stderr.puts "#{@prefix} : #{severity} - #{message}"
112
+ end
113
+ end
@@ -2,5 +2,5 @@
2
2
  module Confoog
3
3
  # Version of this Gem, using Semantic Versioning 2.0.0
4
4
  # http://semver.org/
5
- VERSION = '0.4.0'
5
+ VERSION = '0.4.1'
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: confoog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seapagan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-09-09 00:00:00.000000000 Z
11
+ date: 2015-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -154,7 +154,7 @@ files:
154
154
  - bin/setup
155
155
  - confoog.gemspec
156
156
  - lib/confoog.rb
157
- - lib/confoog/utility.rb
157
+ - lib/confoog/status.rb
158
158
  - lib/confoog/version.rb
159
159
  homepage: https://github.com/seapagan/confoog
160
160
  licenses:
@@ -1,10 +0,0 @@
1
- # A collection of utility functions for the Confoog class
2
- module ConfoogUtils
3
- private
4
-
5
- # Display output to the console with the severity noted, unless we are quiet.
6
- def console_output(message, severity)
7
- return unless @options[:quiet] == false
8
- $stderr.puts "#{@options[:prefix]} : #{severity} - #{message}"
9
- end
10
- end