Class: Spellr::ConfigValidator

Inherits:
Object
  • Object
show all
Includes:
Validations
Defined in:
lib/spellr/config_validator.rb

Instance Method Summary collapse

Methods included from Validations

#errors, included

Instance Method Details

#interactive_is_interactiveObject

rubocop:disable Metrics/MethodLength



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/spellr/config_validator.rb', line 21

def interactive_is_interactive # rubocop:disable Metrics/MethodLength
  return unless Spellr.config.reporter.class.name == 'Spellr::Interactive'

  # I have no idea how to check for this other than call it
  Timeout.timeout(0.0000000000001) do
    Spellr.config.output.stdin.getch
  end
rescue Errno::ENOTTY, Errno::ENODEV, IOError
  errors << 'CLI error: --interactive is unavailable in a non-interactive terminal'
rescue Timeout::Error
  nil
end

#keys_are_single_charactersObject



54
55
56
57
58
59
60
61
# File 'lib/spellr/config_validator.rb', line 54

def keys_are_single_characters
  bad_languages = Spellr.config.languages.select { |l| l.key.length > 1 }

  bad_languages.each do |lang|
    errors << "Config error: #{lang.name} defines a key that is too long (#{lang.key}). "\
      'Please change it to be a single character'
  end
end

#languages_with_conflicting_keysObject



48
49
50
51
52
# File 'lib/spellr/config_validator.rb', line 48

def languages_with_conflicting_keys
  Spellr.config.languages.select(&:addable?).group_by(&:key).values.select do |g|
    g.length > 1
  end
end

#not_interactive_and_parallelObject



34
35
36
37
38
39
# File 'lib/spellr/config_validator.rb', line 34

def not_interactive_and_parallel
  return unless Spellr.config.reporter.class.name == 'Spellr::Interactive' &&
    Spellr.config.parallel

  errors << 'CLI error: --interactive is incompatible with --parallel'
end

#only_has_one_key_per_languageObject



41
42
43
44
45
46
# File 'lib/spellr/config_validator.rb', line 41

def only_has_one_key_per_language
  languages_with_conflicting_keys.each do |conflicts|
    errors << "Config error: #{conflicts.map(&:name).join(' & ')} share the same language key "\
    "(#{conflicts.first.key}). Please define one to be different with `key:`"
  end
end

#valid?Boolean

Returns:

  • (Boolean)

Raises:



17
18
19
# File 'lib/spellr/config_validator.rb', line 17

def valid?
  raise ::Spellr::Config::Invalid, errors.join("\n") unless super
end