Class: TorigoyaKit::TypeUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/torigoya_kit/typeutil.rb

Class Method Summary collapse

Class Method Details

.array_type_check(tag, name, elem, expect) ⇒ Object



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

def self.array_type_check(tag, name, elem, expect)
  return if elem.nil?
  type_check(tag, name, elem, Array)
  elem.each do |e|
    type_check(tag, "element of #{name}", e, expect)
  end
end

.boolean_type_check(tag, name, elem) ⇒ Object

Raises:



20
21
22
23
# File 'lib/torigoya_kit/typeutil.rb', line 20

def self.boolean_type_check(tag, name, elem)
  return if elem.nil?
  raise InvalidFormatError.new("#{tag.class}: type of `#{name}` must be Boolean (but #{elem.class})") unless elem.is_a?(TrueClass) || elem.is_a?(FalseClass)
end

.nil_check(tag, name, elem) ⇒ Object

Raises:



6
7
8
# File 'lib/torigoya_kit/typeutil.rb', line 6

def self.nil_check(tag, name, elem)
  raise InvalidFormatError.new("#{tag.class}: type of `#{name}` must NOT be nil") if elem.nil?
end

.nonnull_array_type_check(tag, name, elem, expect) ⇒ Object



38
39
40
41
42
43
# File 'lib/torigoya_kit/typeutil.rb', line 38

def self.nonnull_array_type_check(tag, name, elem, expect)
  nonnull_type_check(tag, name, elem, Array)
  elem.each do |e|
    nonnull_type_check(tag, "element of #{name}", e, expect)
  end
end

.nonnull_boolean_type_check(tag, name, elem) ⇒ Object



25
26
27
28
# File 'lib/torigoya_kit/typeutil.rb', line 25

def self.nonnull_boolean_type_check(tag, name, elem)
  nil_check(tag, name, elem)
  boolean_type_check(tag, name, elem)
end

.nonnull_type_check(tag, name, elem, expect) ⇒ Object



15
16
17
18
# File 'lib/torigoya_kit/typeutil.rb', line 15

def self.nonnull_type_check(tag, name, elem, expect)
  nil_check(tag, name, elem)
  type_check(tag, name, elem, expect)
end

.type_check(tag, name, elem, expect) ⇒ Object

Raises:



10
11
12
13
# File 'lib/torigoya_kit/typeutil.rb', line 10

def self.type_check(tag, name, elem, expect)
  return if elem.nil?
  raise InvalidFormatError.new("#{tag.class}: type of `#{name}` must be #{expect} (but #{elem.class})") unless elem.is_a?(expect)
end