Module: FnValidations

Included in:
SocialNetsDB::SocialNet, SocialNetsDB::SocialNet, Support
Defined in:
lib/social_nets_db/fn_validations.rb

Class Method Summary collapse

Class Method Details

.validate_argument_boolean!(argument) ⇒ Object



25
26
27
# File 'lib/social_nets_db/fn_validations.rb', line 25

def validate_argument_boolean! argument
	validate_argument_type! argument, [TrueClass, FalseClass]
end

.validate_argument_positive!(argument) ⇒ Object



37
38
39
40
41
42
# File 'lib/social_nets_db/fn_validations.rb', line 37

def validate_argument_positive! argument
	validate_argument_type! argument, Numeric
	message = "You must pass a positive value, you pass #{argument.inspect}"
	fail ArgumentError, message unless argument.positive?
	argument
end

.validate_argument_presence!(argument) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/social_nets_db/fn_validations.rb', line 29

def validate_argument_presence! argument
	validate_argument_type! argument, [String, Symbol]
	return argument if argument.is_a?(Symbol)
	message = "You must pass a non-empty String, you are passing #{argument.inspect}"
	fail ArgumentError, message unless argument.is_a?(String) && argument != ""
	argument
end

.validate_argument_type!(argument, permitted_types) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/social_nets_db/fn_validations.rb', line 7

def validate_argument_type! argument, permitted_types
	types      = Array(permitted_types)
	type_names = types.map { |klass| "`#{klass}`" }.join(" or ")
	valid      = types.any? { |type| argument.is_a? type }
	message    = "#{type_names} is expected, you pass a `#{argument.class}` (#{argument})"
	fail TypeError, message unless valid
	argument
end

.validate_collection_item_types!(collection, permitted_types) ⇒ Object



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

def validate_collection_item_types! collection, permitted_types
	types      = Array(permitted_types)
	type_names = types.map { |name| "`#{name}`" }.join(" or ")
	valid      = collection.all? { |item| types.include?(item.class) }
	message    = "#{type_names} is expected, one the items you pass is of incorrect type"
	fail TypeError, message unless valid
	collection
end