Module: AbideDevUtils::Validate

Defined in:
lib/abide_dev_utils/validate.rb

Overview

Methods used for validating data

Class Method Summary collapse

Class Method Details

.directory(path) ⇒ Object



25
26
27
28
# File 'lib/abide_dev_utils/validate.rb', line 25

def self.directory(path)
  filesystem_path(path)
  raise AbideDevUtils::Errors::PathNotDirectoryError, path unless File.directory?(path)
end

.file(path, extension: nil) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/abide_dev_utils/validate.rb', line 16

def self.file(path, extension: nil)
  filesystem_path(path)
  raise AbideDevUtils::Errors::PathNotFileError, path unless File.file?(path)
  return if extension.nil?

  file_ext = extension.match?(/^\.[A-Za-z0-9]+$/) ? extension : ".#{extension}"
  raise AbideDevUtils::Errors::FileExtensionIncorrectError, extension unless File.extname(path) == file_ext
end

.filesystem_path(path) ⇒ Object



12
13
14
# File 'lib/abide_dev_utils/validate.rb', line 12

def self.filesystem_path(path)
  raise AbideDevUtils::Errors::FileNotFoundError, path unless File.exist?(path)
end

.hashable(obj) ⇒ Object



51
52
53
54
55
# File 'lib/abide_dev_utils/validate.rb', line 51

def self.hashable(obj)
  return if obj.respond_to?(:to_hash) || obj.respond_to?(:to_h)

  raise AbideDevUtils::Errors::NotHashableError, obj
end

.not_empty(thing, msg) ⇒ Object



47
48
49
# File 'lib/abide_dev_utils/validate.rb', line 47

def self.not_empty(thing, msg)
  raise AbideDevUtils::Errors::ObjectEmptyError, msg if thing.empty?
end

.populated_string(thing) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/abide_dev_utils/validate.rb', line 38

def self.populated_string(thing)
  raise AbideDevUtils::Errors::NotPopulatedStringError, 'Object is nil' if thing.nil?

  unless thing.instance_of?(String)
    raise AbideDevUtils::Errors::NotPopulatedStringError, "Object is not a String. Type: #{thing.class}"
  end
  raise AbideDevUtils::Errors::NotPopulatedStringError, 'String is empty' if thing.empty?
end

.populated_string?(thing) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
# File 'lib/abide_dev_utils/validate.rb', line 30

def self.populated_string?(thing)
  return false if thing.nil?
  return false unless thing.instance_of?(String)
  return false if thing.empty?

  true
end

.puppet_module_directory(path = Dir.pwd) ⇒ Object



8
9
10
# File 'lib/abide_dev_utils/validate.rb', line 8

def self.puppet_module_directory(path = Dir.pwd)
  raise AbideDevUtils::Errors::Ppt::NotModuleDirError, path unless File.file?(File.join(path, 'metadata.json'))
end