Class: Siba::SibaCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/siba/siba_check.rb

Class Method Summary collapse

Class Method Details

.hash(obj, name, is_optional = false) ⇒ Object

Raises:



39
40
41
42
43
44
45
46
# File 'lib/siba/siba_check.rb', line 39

def hash(obj, name, is_optional=false)
  if obj.nil?
    return nil if is_optional
    raise Siba::CheckError, "'#{name}' option is not defined"
  end
  raise Siba::CheckError, "'#{name}' option should be of [Hash] type" unless obj.is_a? Hash
  obj
end

.options_bool(options, key_name, is_optional = false, default_value = false) ⇒ Object



6
7
8
# File 'lib/siba/siba_check.rb', line 6

def options_bool(options, key_name, is_optional = false, default_value=false)
  check_options options, key_name, [TrueClass, FalseClass], is_optional, default_value
end

.options_hash(options, key_name, is_optional = false, default_value = nil) ⇒ Object



10
11
12
# File 'lib/siba/siba_check.rb', line 10

def options_hash(options, key_name, is_optional = false, default_value=nil)
  check_options options, key_name, Hash, is_optional, default_value
end

.options_string(options, key_name, is_optional = false, default_value = nil) ⇒ Object



14
15
16
# File 'lib/siba/siba_check.rb', line 14

def options_string(options, key_name, is_optional = false, default_value=nil)
  check_options(options, key_name, String, is_optional, default_value)
end

.options_string_array(options, key_name, is_optional = false, default_value = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/siba/siba_check.rb', line 18

def options_string_array(options, key_name, is_optional = false, default_value=nil)
  value = options[key_name]
  if value.nil?
    return default_value if is_optional
    raise Siba::CheckError, "'#{key_name}' option is not defined"
  end

  begin
    if value.is_a? Array
      value.each_index do |i|
        value[i] = try_to_s value[i], key_name
      end
    else
      value = [ try_to_s(value, key_name) ]
    end
  rescue Exception
    raise Siba::CheckError, "'#{key_name}' option should be string or an array of strings"
  end
  value
end

.try_to_s(value, key_name) ⇒ Object

Tries to conver value to string

Raises:



49
50
51
52
53
54
# File 'lib/siba/siba_check.rb', line 49

def try_to_s(value, key_name)
  raise Siba::CheckError, "'#{key_name}' option should be string" if [Array, Hash].any?{|a| value.is_a?(a)}
  value = value.to_s.strip
  raise Siba::CheckError, "'#{key_name}' option should not be empty" if value.empty?
  value
end