Class: Romanesco::Validators::ParenthesisCountValidator

Inherits:
Validator
  • Object
show all
Defined in:
lib/romanesco/validators/parenthesis_count_validator.rb

Instance Attribute Summary

Attributes inherited from Validator

#next_step

Instance Method Summary collapse

Methods inherited from Validator

#execute, #initialize

Constructor Details

This class inherits a constructor from Romanesco::Validators::Validator

Instance Method Details

#validate(raw_expression) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/romanesco/validators/parenthesis_count_validator.rb', line 7

def validate(raw_expression)
  open_parenthesis = /(\()/
  close_parenthesis = /(\))/

  open_parenthesis_count = raw_expression.scan(open_parenthesis).size
  close_parenthesis_count = raw_expression.scan(close_parenthesis).size

  raise InvalidExpressionError.new('Uneven number of parentheses') unless open_parenthesis_count == close_parenthesis_count

  raw_expression
end