Module: SciolyFF::Validator::Sections

Included in:
Events, Penalties, Placings, Raws, Subdivisions, Teams, TopLevel, Tournament
Defined in:
lib/sciolyff/validator/sections.rb

Overview

Generic tests for (sub-)sections and types. Including classes must have two hashes REQUIRED and OPTIONAL (see other files in this dir for examples)

Instance Method Summary collapse

Instance Method Details

#all_required_sections?(rep, logger) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
# File 'lib/sciolyff/validator/sections.rb', line 13

def all_required_sections?(rep, logger)
  missing = self.class::REQUIRED.keys - rep.keys
  return true if missing.empty?

  logger.error "missing required sections: #{missing.join ', '}"
end

#no_extra_sections?(rep, logger) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
# File 'lib/sciolyff/validator/sections.rb', line 20

def no_extra_sections?(rep, logger)
  extra = rep.keys - (self.class::REQUIRED.keys + self.class::OPTIONAL.keys)
  return true if extra.empty?

  logger.error "extra section(s) found: #{extra.join ', '}"
end

#rep_is_hash?(rep, logger) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
# File 'lib/sciolyff/validator/sections.rb', line 7

def rep_is_hash?(rep, logger)
  return true if rep.instance_of? Hash

  logger.error "entry in #{section_name} is not a Hash"
end

#sections_are_correct_type?(rep, logger) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
# File 'lib/sciolyff/validator/sections.rb', line 27

def sections_are_correct_type?(rep, logger)
  correct_types = self.class::REQUIRED.merge self.class::OPTIONAL
  rep.all? do |key, value|
    type = correct_types[key]
    next true if correct_type?(type, value)

    logger.error "#{key}: #{value} is not #{type}"
  end
end