jeckyl 0.2.7 → 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,316 @@
1
+ #
2
+ #
3
+ # = Jeckyl Helpers
4
+ #
5
+ # == Test methods for parameters
6
+ #
7
+ # Author:: Robert Sharp
8
+ # Copyright:: Copyright (c) 2013 Robert Sharp
9
+ # License:: Open Software Licence v3.0
10
+ #
11
+ # This software is licensed for use under the Open Software Licence v. 3.0
12
+ # The terms of this licence can be found at http://www.opensource.org/licenses/osl-3.0.php
13
+ # and in the file copyright.txt. Under the terms of this licence, all derivative works
14
+ # must themselves be licensed under the Open Software Licence v. 3.0
15
+ #
16
+ #
17
+ #
18
+
19
+ module Jeckyl
20
+
21
+ module Helpers
22
+
23
+ # the following are all helper methods to parse values and raise exceptions if the values are not correct
24
+
25
+ # file helpers - meanings should be apparent
26
+
27
+ # check that the parameter is a directory and that the directory is writable
28
+ #
29
+ # Jeckyl checking method to be used in parameter methods to check the validity of
30
+ # given parameters, returning the parameter if valid or else raising an exception
31
+ # which is either ConfigError if the parameter fails the check or ConfigSyntaxError if
32
+ # the parameter is not validly formed
33
+ #
34
+ # @param [String] path to directory
35
+ #
36
+ def a_writable_dir(path)
37
+ if FileTest.directory?(path) && FileTest.writable?(path) then
38
+ path
39
+ else
40
+ raise_config_error(path, "directory is not writable or does not exist")
41
+ end
42
+ end
43
+
44
+ # check that the directory is at least readable
45
+ #
46
+ # Jeckyl checking method to be used in parameter methods to check the validity of
47
+ # given parameters, returning the parameter if valid or else raising an exception
48
+ # which is either ConfigError if the parameter fails the check or ConfigSyntaxError if
49
+ # the parameter is not validly formed
50
+ #
51
+ # @param [String] path to the directory to be checked
52
+ def a_readable_dir(path)
53
+ if FileTest.directory?(path) && FileTest.readable?(path) then
54
+ path
55
+ else
56
+ raise_config_error(path, "directory is not readable or does not exist")
57
+ end
58
+ end
59
+
60
+ # check parameter is a readable file
61
+ #
62
+ # Jeckyl checking method to be used in parameter methods to check the validity of
63
+ # given parameters, returning the parameter if valid or else raising an exception
64
+ # which is either ConfigError if the parameter fails the check or ConfigSyntaxError if
65
+ # the parameter is not validly formed
66
+ #
67
+ # @param [String] path to file
68
+ #
69
+ def a_readable_file(path)
70
+ if FileTest.readable?(path) then
71
+ path
72
+ else
73
+ raise_config_error(path, "file does not exist")
74
+ end
75
+ end
76
+
77
+ # check parameter is an executable file
78
+ #
79
+ # Jeckyl checking method to be used in parameter methods to check the validity of
80
+ # given parameters, returning the parameter if valid or else raising an exception
81
+ # which is either ConfigError if the parameter fails the check or ConfigSyntaxError if
82
+ # the parameter is not validly formed
83
+ #
84
+ # @param [String] path to executable
85
+ #
86
+ def an_executable(path)
87
+ a_readable_file(path)
88
+ if FileTest.executable?(path) then
89
+ path
90
+ else
91
+ raise_config_error(path, "file is not executable")
92
+ end
93
+ end
94
+
95
+ # simple type helpers
96
+
97
+ # check the parameter is of the required type
98
+ #
99
+ # Jeckyl checking method to be used in parameter methods to check the validity of
100
+ # given parameters, returning the parameter if valid or else raising an exception
101
+ # which is either ConfigError if the parameter fails the check or ConfigSyntaxError if
102
+ # the parameter is not validly formed
103
+ #
104
+ # @param [Object] obj to check type of
105
+ # @param [Class] type being a class constant such as Numeric, String
106
+ #
107
+ def a_type_of(obj, type)
108
+ if obj.kind_of?(type) then
109
+ obj
110
+ else
111
+ raise_config_error(obj, "value is not of required type: #{type}")
112
+ end
113
+ end
114
+
115
+ # number helpers
116
+
117
+ # check the parameter is a number
118
+ #
119
+ # @param [Numeric] numb to check
120
+ def a_number(numb)
121
+ return numb if numb.kind_of?(Numeric)
122
+ raise_config_error numb, "value is not a number: #{numb}"
123
+ end
124
+
125
+ # check the parameter is a positive number (or zero)
126
+ #
127
+ # @param [Numeric] numb to check
128
+ def a_positive_number(numb)
129
+ return numb if numb.kind_of?(Numeric) && numb >= 0
130
+ raise_config_error numb, "value is not a positive number: #{numb}"
131
+ end
132
+
133
+ # check that the parameter is within the required range
134
+ #
135
+ # Jeckyl checking method to be used in parameter methods to check the validity of
136
+ # given parameters, returning the parameter if valid or else raising an exception
137
+ # which is either ConfigError if the parameter fails the check or ConfigSyntaxError if
138
+ # the parameter is not validly formed
139
+ #
140
+ # @param [Numeric] val to check
141
+ # @param [Numeric] lower bound of range
142
+ # @param [Numeric] upper bound of range
143
+ #
144
+ def in_range(val, lower, upper)
145
+ raise_syntax_error("#{lower.to_s}..#{upper.to_s} is not a range") unless (lower .. upper).kind_of?(Range)
146
+ if (lower .. upper) === val then
147
+ val
148
+ else
149
+ raise_config_error(val, "value is not within required range: #{lower.to_s}..#{upper.to_s}")
150
+ end
151
+ end
152
+
153
+
154
+ # boolean helpers
155
+
156
+ # check parameter is a boolean, true or false but not strings "true" or "false"
157
+ #
158
+ # Jeckyl checking method to be used in parameter methods to check the validity of
159
+ # given parameters, returning the parameter if valid or else raising an exception
160
+ # which is either ConfigError if the parameter fails the check or ConfigSyntaxError if
161
+ # the parameter is not validly formed
162
+ #
163
+ # @param [Boolean] val to check
164
+ #
165
+ def a_boolean(val)
166
+ if val.kind_of?(TrueClass) || val.kind_of?(FalseClass) then
167
+ val
168
+ else
169
+ raise_config_error(val, "Value is not a Boolean")
170
+ end
171
+ end
172
+
173
+ # check the parameter is a flag, being "true", "false", "yes", "no", "on", "off", or 1 , 0
174
+ # and return a proper boolean
175
+ #
176
+ # Jeckyl checking method to be used in parameter methods to check the validity of
177
+ # given parameters, returning the parameter if valid or else raising an exception
178
+ # which is either ConfigError if the parameter fails the check or ConfigSyntaxError if
179
+ # the parameter is not validly formed
180
+ #
181
+ # @param [String] val to check
182
+ #
183
+ def a_flag(val)
184
+ val = val.downcase if val.kind_of?(String)
185
+ case val
186
+ when "true", "yes", "on", 1
187
+ true
188
+ when "false", "no", "off", 0
189
+ false
190
+ else
191
+ raise_config_error(val, "Cannot convert to Boolean")
192
+ end
193
+ end
194
+
195
+
196
+ # compound objects
197
+
198
+ # check the parameter is an array
199
+ #
200
+ # Jeckyl checking method to be used in parameter methods to check the validity of
201
+ # given parameters, returning the parameter if valid or else raising an exception
202
+ # which is either ConfigError if the parameter fails the check or ConfigSyntaxError if
203
+ # the parameter is not validly formed
204
+ #
205
+ # @param [Array] ary to check
206
+ #
207
+ def an_array(ary)
208
+ if ary.kind_of?(Array) then
209
+ ary
210
+ else
211
+ raise_config_error(ary, "value is not an Array")
212
+ end
213
+ end
214
+
215
+ # check the parameter is an array and the array is of the required type
216
+ #
217
+ # Jeckyl checking method to be used in parameter methods to check the validity of
218
+ # given parameters, returning the parameter if valid or else raising an exception
219
+ # which is either ConfigError if the parameter fails the check or ConfigSyntaxError if
220
+ # the parameter is not validly formed
221
+ #
222
+ # @param [Array] ary of values to check
223
+ # @param [Class] type being the class that the values must belong to
224
+ #
225
+ def an_array_of(ary, type)
226
+ raise_syntax_error("Provided a value that is a type: #{type.to_s}") unless type.class == Class
227
+ if ary.kind_of?(Array) then
228
+ ary.each do |element|
229
+ unless element.kind_of?(type) then
230
+ raise_config_error(element, "element of array is not of type: #{type}")
231
+ end
232
+ end
233
+ return ary
234
+ else
235
+ raise_config_error(ary, "value is not an Array")
236
+ end
237
+ end
238
+
239
+ # check the parameter is a hash
240
+ #
241
+ # Jeckyl checking method to be used in parameter methods to check the validity of
242
+ # given parameters, returning the parameter if valid or else raising an exception
243
+ # which is either ConfigError if the parameter fails the check or ConfigSyntaxError if
244
+ # the parameter is not validly formed
245
+ #
246
+ # @param [Hash] hsh to check
247
+ #
248
+ def a_hash(hsh)
249
+ if hsh.kind_of?(Hash) then
250
+ hsh
251
+ else
252
+ raise_config_error(hsh, "value is not a Hash")
253
+ end
254
+ end
255
+
256
+ # strings and text and stuff
257
+
258
+ # check the parameter is a string
259
+ #
260
+ # Jeckyl checking method to be used in parameter methods to check the validity of
261
+ # given parameters, returning the parameter if valid or else raising an exception
262
+ # which is either ConfigError if the parameter fails the check or ConfigSyntaxError if
263
+ # the parameter is not validly formed
264
+ #
265
+ # @param [String] str to check
266
+ #
267
+ def a_string(str)
268
+ if str.kind_of?(String) then
269
+ str
270
+ else
271
+ raise_config_error(str.to_s, "is not a String")
272
+ end
273
+ end
274
+
275
+ # check the parameter is a string and matches the required pattern
276
+ #
277
+ # Jeckyl checking method to be used in parameter methods to check the validity of
278
+ # given parameters, returning the parameter if valid or else raising an exception
279
+ # which is either ConfigError if the parameter fails the check or ConfigSyntaxError if
280
+ # the parameter is not validly formed
281
+ #
282
+ # @param [String] str to match against the pattern
283
+ # @param [Regexp] pattern to match with
284
+ #
285
+ def a_matching_string(str, pattern)
286
+ raise_syntax_error("Attempt to pattern match without a Regexp") unless pattern.kind_of?(Regexp)
287
+ if pattern =~ a_string(str) then
288
+ str
289
+ else
290
+ raise_config_error(str, "does not match required pattern: #{pattern.source}")
291
+ end
292
+ end
293
+
294
+ # set membership - set is an array of members, usually symbols
295
+ #
296
+ # Jeckyl checking method to be used in parameter methods to check the validity of
297
+ # given parameters, returning the parameter if valid or else raising an exception
298
+ # which is either ConfigError if the parameter fails the check or ConfigSyntaxError if
299
+ # the parameter is not validly formed
300
+ #
301
+ # @param [Symbol] symb being the symbol to check
302
+ # @param [Array] set containing the valid symbols that symb should belong to
303
+ #
304
+ def a_member_of(symb, set)
305
+ raise_syntax_error("Sets to test membership must be arrays") unless set.kind_of?(Array)
306
+ if set.include?(symb) then
307
+ symb
308
+ else
309
+ raise_config_error(symb, "is not a member of: #{set.join(', ')}")
310
+ end
311
+ end
312
+
313
+
314
+ end
315
+
316
+ end
@@ -1,13 +1,14 @@
1
1
  # Created by Jevoom
2
2
  #
3
- # 15-Nov-2012
4
- # Change base directory for configs to /etc/jerbil consistent with the main jerbil gem
3
+ # 19-Sep-2013
4
+ # Corrected some minor yard issues
5
+ #
5
6
 
6
7
  module Jeckyl
7
- # version set to 0.2.7
8
- Version = '0.2.7'
9
- # date set to 15-Nov-2012
10
- Version_Date = '15-Nov-2012'
11
- #ident string set to: jeckyl-0.2.7 15-Nov-2012
12
- Ident = 'jeckyl-0.2.7 15-Nov-2012'
8
+ # version set to 0.3.7
9
+ Version = '0.3.7'
10
+ # date set to 19-Sep-2013
11
+ Version_Date = '19-Sep-2013'
12
+ #ident string set to: jeckyl-0.3.7 19-Sep-2013
13
+ Ident = 'jeckyl-0.3.7 19-Sep-2013'
13
14
  end