Class: CrossPlane::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/crossplane/utils.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Utils

Returns a new instance of Utils.



10
11
12
# File 'lib/crossplane/utils.rb', line 10

def initialize(*args)
	self.logger = configure_logger()
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/crossplane/utils.rb', line 8

def logger
  @logger
end

Instance Method Details

#configure_logger(debug: nil) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/crossplane/utils.rb', line 24

def configure_logger(debug: nil)
	logger = Logger.new(STDOUT)
	logger.datetime_format = '%Y-%m-%d %H:%M:%S'
	logger.formatter = proc do |severity, datetime, progname, msg|
		format("[%s] %s\n", severity.capitalize, msg)
	end
	return logger
end

#generate_random(length: 64) ⇒ Object



18
19
20
21
22
# File 'lib/crossplane/utils.rb', line 18

def generate_random(length: 64)
	o = [('a'..'z'), ('A'..'Z')].map(&:to_a).flatten
	string = (0...length).map { o[rand(o.length)] }.join
	string
end

#isspace(string) ⇒ Object



14
15
16
# File 'lib/crossplane/utils.rb', line 14

def isspace(string)
	return string =~ /^\s+$/ ? true : false
end

#validate_constructor(client: nil, args: nil, required: nil, conflicts: nil, requires: nil, valid: {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/crossplane/utils.rb', line 33

def validate_constructor(client: nil, args: nil, required: nil, conflicts: nil, requires: nil, valid: {})
	content = {}
	errors = []
	booleans = ['catch_errors', 'combine', 'comments', 'single', 'strict']
	hashes = []
	arrays = ['ignore']
	ints = ['indent']
	args = Hash[args.map{ |k, v| [k.to_s, v] }]
	args.each do |k, v|
		args.delete(k) if v == nil
	end

	if ((required - args.keys).length != 0)
		errors.push(missing_opts_error(required - args.keys))
	end

	if conflicts.length > 0
		conflicts.each do |conflict_item|
			if (0 && (conflict_item & args.keys).length > 1)
				errors.push(conflicting_opts_error(conflict_item))
			end
		end
	end

	if requires.keys.length > 0
		requires.each do |key, required|
			if args.keys.include?(key)
				intersection = required & args.keys
				unless (required & args.keys).length == required.length
					missing = required - intersection
					if missing.length > 0
						errors.push(missing_requires_error(key, missing))
					end
				end
			end
		end
	end

	if valid['params']
		valid['params'].each do |param|
			if (args.has_key?(param)) and (valid['params'].include?(param))
				if ints.include?(param)
					begin
						content[param.to_sym] = args[param].to_i
					rescue
						errors.push(format('%s must be an integer', param))
					end
				end

				if booleans.include?(param)
					if args[param].is_a?(TrueClass) or args[param].is_a?(FalseClass)
						content[param.to_sym] = args[param] == true ? true : false
					elsif args[param].is_a?(String)
						content[param.to_sym] = args[param].downcase == 'true' ? true : false
					else
						content[param.to_sym] = false
					end
				end
				content[param.to_sym] = args[param]
			end
		end
	end

	if errors.length > 0
		raise CrossPlane::ConstructorError.new(errors: errors)
	else
		return content
	end
end