Class: CitizenBudgetModel::EquationValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/citizen_budget_model/equation_validator.rb

Overview

Validates the format of simple mathematical equation.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

._validator_index_offsetsObject

Returns the value of attribute _validator_index_offsets.



51
52
53
# File 'app/validators/citizen_budget_model/equation_validator.rb', line 51

def _validator_index_offsets
  @_validator_index_offsets
end

._validator_indiciesObject

Returns the value of attribute _validator_indicies.



64
65
66
# File 'app/validators/citizen_budget_model/equation_validator.rb', line 64

def _validator_indicies
  @_validator_indicies
end

._validator_key_spansObject

Returns the value of attribute _validator_key_spans.



38
39
40
# File 'app/validators/citizen_budget_model/equation_validator.rb', line 38

def _validator_key_spans
  @_validator_key_spans
end

._validator_trans_actionsObject

Returns the value of attribute _validator_trans_actions.



353
354
355
# File 'app/validators/citizen_budget_model/equation_validator.rb', line 353

def _validator_trans_actions
  @_validator_trans_actions
end

._validator_trans_keysObject

Returns the value of attribute _validator_trans_keys.



13
14
15
# File 'app/validators/citizen_budget_model/equation_validator.rb', line 13

def _validator_trans_keys
  @_validator_trans_keys
end

._validator_trans_targsObject

Returns the value of attribute _validator_trans_targs.



340
341
342
# File 'app/validators/citizen_budget_model/equation_validator.rb', line 340

def _validator_trans_targs
  @_validator_trans_targs
end

.validator_en_mainObject

Returns the value of attribute validator_en_main.



383
384
385
# File 'app/validators/citizen_budget_model/equation_validator.rb', line 383

def validator_en_main
  @validator_en_main
end

.validator_en_restObject

Returns the value of attribute validator_en_rest.



379
380
381
# File 'app/validators/citizen_budget_model/equation_validator.rb', line 379

def validator_en_rest
  @validator_en_rest
end

.validator_errorObject

Returns the value of attribute validator_error.



374
375
376
# File 'app/validators/citizen_budget_model/equation_validator.rb', line 374

def validator_error
  @validator_error
end

.validator_first_finalObject

Returns the value of attribute validator_first_final.



370
371
372
# File 'app/validators/citizen_budget_model/equation_validator.rb', line 370

def validator_first_final
  @validator_first_final
end

.validator_startObject

Returns the value of attribute validator_start.



366
367
368
# File 'app/validators/citizen_budget_model/equation_validator.rb', line 366

def validator_start
  @validator_start
end

Class Method Details

.valid?(value) ⇒ Boolean

Returns whether an equation is valid.

Parameters:

  • value (String)

    an equation

Returns:

  • (Boolean)

    whether the equation is valid

See Also:



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'app/validators/citizen_budget_model/equation_validator.rb', line 396

def self.valid?(value)
  data = value.unpack('c*')
  stack = []

  
# line 402 "app/validators/citizen_budget_model/equation_validator.rb"
begin
	p ||= 0
	pe ||= data.length
	cs = validator_start
	top = 0
end

# line 35 "app/validators/citizen_budget_model/equation_validator.rl"
  
# line 412 "app/validators/citizen_budget_model/equation_validator.rb"
begin
	testEof = false
	_slen, _trans, _keys, _inds, _acts, _nacts = nil
	_goto_level = 0
	_resume = 10
	_eof_trans = 15
	_again = 20
	_test_eof = 30
	_out = 40
	while true
	if _goto_level <= 0
	if p == pe
		_goto_level = _test_eof
		next
	end
	if cs == 0
		_goto_level = _out
		next
	end
	end
	if _goto_level <= _resume
	_keys = cs << 1
	_inds = _validator_index_offsets[cs]
	_slen = _validator_key_spans[cs]
	_wide = data[p].ord
	_trans = if (   _slen > 0 && 
			_validator_trans_keys[_keys] <= _wide && 
			_wide <= _validator_trans_keys[_keys + 1] 
  ) then
			_validator_indicies[ _inds + _wide - _validator_trans_keys[_keys] ] 
		 else 
			_validator_indicies[ _inds + _slen ]
		 end
	cs = _validator_trans_targs[_trans]
	if _validator_trans_actions[_trans] != 0
	case _validator_trans_actions[_trans]
	when 1 then
# line 8 "app/validators/citizen_budget_model/equation_validator.rl"
		begin
 	begin
		stack[top] = cs
		top+= 1
		cs = 18
		_goto_level = _again
		next
	end
 		end
	when 2 then
# line 14 "app/validators/citizen_budget_model/equation_validator.rl"
		begin
 	begin
		top -= 1
		cs = stack[top]
		_goto_level = _again
		next
	end
 		end
# line 470 "app/validators/citizen_budget_model/equation_validator.rb"
	end
	end
	end
	if _goto_level <= _again
	if cs == 0
		_goto_level = _out
		next
	end
	p += 1
	if p != pe
		_goto_level = _resume
		next
	end
	end
	if _goto_level <= _test_eof
	end
	if _goto_level <= _out
		break
	end
end
	end

# line 36 "app/validators/citizen_budget_model/equation_validator.rl"

  # Ensure all parentheses are closed and the state machine is in a final state.
  top.zero? && cs >= validator_first_final
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



498
499
500
# File 'app/validators/citizen_budget_model/equation_validator.rb', line 498

def validate_each(record, attribute, value)
  record.errors.add(attribute, :invalid, options.merge(value: value)) unless self.class.valid?(value)
end