Module: Jeckyl::Helpers
- Included in:
- Config
- Defined in:
- lib/jeckyl/helpers.rb
Instance Method Summary collapse
-
#a_boolean(val) ⇒ Object
check parameter is a boolean, true or false but not strings “true” or “false”.
-
#a_flag(val) ⇒ Object
check the parameter is a flag, being “true”, “false”, “yes”, “no”, “on”, “off”, or 1 , 0 and return a proper boolean.
-
#a_hash(hsh) ⇒ Object
check the parameter is a hash.
-
#a_matching_string(str, pattern) ⇒ Object
check the parameter is a string and matches the required pattern.
-
#a_member_of(symb, set) ⇒ Object
set membership - set is an array of members, usually symbols.
-
#a_number(numb) ⇒ Object
check the parameter is a number.
-
#a_positive_number(numb) ⇒ Object
check the parameter is a positive number (or zero).
-
#a_readable_dir(path) ⇒ Object
check that the directory is at least readable.
-
#a_readable_file(path) ⇒ Object
check parameter is a readable file.
-
#a_string(str) ⇒ Object
check the parameter is a string.
-
#a_type_of(obj, type) ⇒ Object
check the parameter is of the required type.
-
#a_writable_dir(path) ⇒ Object
check that the parameter is a directory and that the directory is writable.
-
#an_array(ary) ⇒ Object
check the parameter is an array.
-
#an_array_of(ary, type) ⇒ Object
check the parameter is an array and the array is of the required type.
-
#an_executable(path) ⇒ Object
check parameter is an executable file.
-
#in_range(val, lower, upper) ⇒ Object
check that the parameter is within the required range.
Instance Method Details
#a_boolean(val) ⇒ Object
check parameter is a boolean, true or false but not strings “true” or “false”
Jeckyl checking method to be used in parameter methods to check the validity of given parameters, returning the parameter if valid or else raising an exception which is either ConfigError if the parameter fails the check or ConfigSyntaxError if the parameter is not validly formed
165 166 167 168 169 170 171 |
# File 'lib/jeckyl/helpers.rb', line 165 def a_boolean(val) if val.kind_of?(TrueClass) || val.kind_of?(FalseClass) then val else raise_config_error(val, "Value is not a Boolean") end end |
#a_flag(val) ⇒ Object
check the parameter is a flag, being “true”, “false”, “yes”, “no”, “on”, “off”, or 1 , 0 and return a proper boolean
Jeckyl checking method to be used in parameter methods to check the validity of given parameters, returning the parameter if valid or else raising an exception which is either ConfigError if the parameter fails the check or ConfigSyntaxError if the parameter is not validly formed
183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/jeckyl/helpers.rb', line 183 def a_flag(val) val = val.downcase if val.kind_of?(String) case val when "true", "yes", "on", 1 true when "false", "no", "off", 0 false else raise_config_error(val, "Cannot convert to Boolean") end end |
#a_hash(hsh) ⇒ Object
check the parameter is a hash
Jeckyl checking method to be used in parameter methods to check the validity of given parameters, returning the parameter if valid or else raising an exception which is either ConfigError if the parameter fails the check or ConfigSyntaxError if the parameter is not validly formed
248 249 250 251 252 253 254 |
# File 'lib/jeckyl/helpers.rb', line 248 def a_hash(hsh) if hsh.kind_of?(Hash) then hsh else raise_config_error(hsh, "value is not a Hash") end end |
#a_matching_string(str, pattern) ⇒ Object
check the parameter is a string and matches the required pattern
Jeckyl checking method to be used in parameter methods to check the validity of given parameters, returning the parameter if valid or else raising an exception which is either ConfigError if the parameter fails the check or ConfigSyntaxError if the parameter is not validly formed
285 286 287 288 289 290 291 292 |
# File 'lib/jeckyl/helpers.rb', line 285 def a_matching_string(str, pattern) raise_syntax_error("Attempt to pattern match without a Regexp") unless pattern.kind_of?(Regexp) if pattern =~ a_string(str) then str else raise_config_error(str, "does not match required pattern: #{pattern.source}") end end |
#a_member_of(symb, set) ⇒ Object
set membership - set is an array of members, usually symbols
Jeckyl checking method to be used in parameter methods to check the validity of given parameters, returning the parameter if valid or else raising an exception which is either ConfigError if the parameter fails the check or ConfigSyntaxError if the parameter is not validly formed
304 305 306 307 308 309 310 311 |
# File 'lib/jeckyl/helpers.rb', line 304 def a_member_of(symb, set) raise_syntax_error("Sets to test membership must be arrays") unless set.kind_of?(Array) if set.include?(symb) then symb else raise_config_error(symb, "is not a member of: #{set.join(', ')}") end end |
#a_number(numb) ⇒ Object
check the parameter is a number
120 121 122 123 |
# File 'lib/jeckyl/helpers.rb', line 120 def a_number(numb) return numb if numb.kind_of?(Numeric) raise_config_error numb, "value is not a number: #{numb}" end |
#a_positive_number(numb) ⇒ Object
check the parameter is a positive number (or zero)
128 129 130 131 |
# File 'lib/jeckyl/helpers.rb', line 128 def a_positive_number(numb) return numb if numb.kind_of?(Numeric) && numb >= 0 raise_config_error numb, "value is not a positive number: #{numb}" end |
#a_readable_dir(path) ⇒ Object
check that the directory is at least readable
Jeckyl checking method to be used in parameter methods to check the validity of given parameters, returning the parameter if valid or else raising an exception which is either ConfigError if the parameter fails the check or ConfigSyntaxError if the parameter is not validly formed
52 53 54 55 56 57 58 |
# File 'lib/jeckyl/helpers.rb', line 52 def a_readable_dir(path) if FileTest.directory?(path) && FileTest.readable?(path) then path else raise_config_error(path, "directory is not readable or does not exist") end end |
#a_readable_file(path) ⇒ Object
check parameter is a readable file
Jeckyl checking method to be used in parameter methods to check the validity of given parameters, returning the parameter if valid or else raising an exception which is either ConfigError if the parameter fails the check or ConfigSyntaxError if the parameter is not validly formed
69 70 71 72 73 74 75 |
# File 'lib/jeckyl/helpers.rb', line 69 def a_readable_file(path) if FileTest.readable?(path) then path else raise_config_error(path, "file does not exist") end end |
#a_string(str) ⇒ Object
check the parameter is a string
Jeckyl checking method to be used in parameter methods to check the validity of given parameters, returning the parameter if valid or else raising an exception which is either ConfigError if the parameter fails the check or ConfigSyntaxError if the parameter is not validly formed
267 268 269 270 271 272 273 |
# File 'lib/jeckyl/helpers.rb', line 267 def a_string(str) if str.kind_of?(String) then str else raise_config_error(str.to_s, "is not a String") end end |
#a_type_of(obj, type) ⇒ Object
check the parameter is of the required type
Jeckyl checking method to be used in parameter methods to check the validity of given parameters, returning the parameter if valid or else raising an exception which is either ConfigError if the parameter fails the check or ConfigSyntaxError if the parameter is not validly formed
107 108 109 110 111 112 113 |
# File 'lib/jeckyl/helpers.rb', line 107 def a_type_of(obj, type) if obj.kind_of?(type) then obj else raise_config_error(obj, "value is not of required type: #{type}") end end |
#a_writable_dir(path) ⇒ Object
check that the parameter is a directory and that the directory is writable
Jeckyl checking method to be used in parameter methods to check the validity of given parameters, returning the parameter if valid or else raising an exception which is either ConfigError if the parameter fails the check or ConfigSyntaxError if the parameter is not validly formed
36 37 38 39 40 41 42 |
# File 'lib/jeckyl/helpers.rb', line 36 def a_writable_dir(path) if FileTest.directory?(path) && FileTest.writable?(path) then path else raise_config_error(path, "directory is not writable or does not exist") end end |
#an_array(ary) ⇒ Object
check the parameter is an array
Jeckyl checking method to be used in parameter methods to check the validity of given parameters, returning the parameter if valid or else raising an exception which is either ConfigError if the parameter fails the check or ConfigSyntaxError if the parameter is not validly formed
207 208 209 210 211 212 213 |
# File 'lib/jeckyl/helpers.rb', line 207 def an_array(ary) if ary.kind_of?(Array) then ary else raise_config_error(ary, "value is not an Array") end end |
#an_array_of(ary, type) ⇒ Object
check the parameter is an array and the array is of the required type
Jeckyl checking method to be used in parameter methods to check the validity of given parameters, returning the parameter if valid or else raising an exception which is either ConfigError if the parameter fails the check or ConfigSyntaxError if the parameter is not validly formed
225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/jeckyl/helpers.rb', line 225 def an_array_of(ary, type) raise_syntax_error("Provided a value that is a type: #{type.to_s}") unless type.class == Class if ary.kind_of?(Array) then ary.each do |element| unless element.kind_of?(type) then raise_config_error(element, "element of array is not of type: #{type}") end end return ary else raise_config_error(ary, "value is not an Array") end end |
#an_executable(path) ⇒ Object
check parameter is an executable file
Jeckyl checking method to be used in parameter methods to check the validity of given parameters, returning the parameter if valid or else raising an exception which is either ConfigError if the parameter fails the check or ConfigSyntaxError if the parameter is not validly formed
86 87 88 89 90 91 92 93 |
# File 'lib/jeckyl/helpers.rb', line 86 def an_executable(path) a_readable_file(path) if FileTest.executable?(path) then path else raise_config_error(path, "file is not executable") end end |
#in_range(val, lower, upper) ⇒ Object
check that the parameter is within the required range
Jeckyl checking method to be used in parameter methods to check the validity of given parameters, returning the parameter if valid or else raising an exception which is either ConfigError if the parameter fails the check or ConfigSyntaxError if the parameter is not validly formed
144 145 146 147 148 149 150 151 |
# File 'lib/jeckyl/helpers.rb', line 144 def in_range(val, lower, upper) raise_syntax_error("#{lower.to_s}..#{upper.to_s} is not a range") unless (lower .. upper).kind_of?(Range) if (lower .. upper) === val then val else raise_config_error(val, "value is not within required range: #{lower.to_s}..#{upper.to_s}") end end |