Module: Jeckyl::Helpers

Included in:
Config
Defined in:
lib/jeckyl/helpers.rb

Instance Method Summary collapse

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

Parameters:

  • val (Boolean)

    to check



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

Parameters:

  • val (String)

    to check



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

Parameters:

  • hsh (Hash)

    to check



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

Parameters:

  • str (String)

    to match against the pattern

  • pattern (Regexp)

    to match with



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

Parameters:

  • symb (Symbol)

    being the symbol to check

  • set (Array)

    containing the valid symbols that symb should belong to



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

Parameters:

  • numb (Numeric)

    to check



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)

Parameters:

  • numb (Numeric)

    to check



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

Parameters:

  • path (String)

    to the directory to be checked



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

Parameters:

  • path (String)

    to file



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

Parameters:

  • str (String)

    to check



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

Parameters:

  • obj (Object)

    to check type of

  • type (Class)

    being a class constant such as Numeric, String



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

Parameters:

  • path (String)

    to directory



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

Parameters:

  • ary (Array)

    to check



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

Parameters:

  • ary (Array)

    of values to check

  • type (Class)

    being the class that the values must belong to



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

Parameters:

  • path (String)

    to executable



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

Parameters:

  • val (Numeric)

    to check

  • lower (Numeric)

    bound of range

  • upper (Numeric)

    bound of range



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