Module: Mathematical::Validator

Included in:
Mathematical
Defined in:
lib/mathematical/validator.rb

Constant Summary collapse

FORMAT_TYPES =
[:svg, :png, :mathml].freeze
RENDER_TYPES =
[:parse, :filter, :text_filter, :strict_filter].freeze

Instance Method Summary collapse

Instance Method Details

#validate_config(config) ⇒ Object

Raises:

  • (TypeError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mathematical/validator.rb', line 8

def validate_config(config)
  raise(TypeError, "maxsize must be an integer!") unless config[:maxsize].is_a?(Integer)
  raise(TypeError, "maxsize cannot be less than 0!") if config[:maxsize] < 0
  raise(TypeError, "format must be a symbol!") unless config[:format].is_a?(Symbol)
  raise(TypeError, "format type must be one of the following formats: #{FORMAT_TYPES.join(", ")}") unless FORMAT_TYPES.include?(config[:format])

  if config[:delimiter].is_a?(Symbol)
    Configuration::Delimiters.option_exists?(config[:delimiter])
  elsif config[:delimiter].is_a?(Array)
    config[:delimiter] = [nil] if config[:delimiter].empty?

    config[:delimiter].each do |delim|
      Configuration::Delimiters.option_exists?(delim)
    end
  else
    raise(TypeError, "delimiter type must be a valid symbol or array of symbols")
  end
end

#validate_content(maths) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/mathematical/validator.rb', line 27

def validate_content(maths)
  if maths.is_a?(Array)
    maths.map { |m| validate_string(m) }
  else
    validate_string(maths)
  end
end

#validate_string(maths) ⇒ Object

Raises:

  • (ArgumentError)


35
36
37
38
39
# File 'lib/mathematical/validator.rb', line 35

def validate_string(maths)
  raise(ArgumentError, "input must be string!") unless maths.is_a?(String)

  maths.strip
end