Class: ConditionValidationRule
- Inherits:
-
ValidationRule
- Object
- ValidationRule
- ConditionValidationRule
- Defined in:
- lib/validation_profiler/rules/rules.rb
Instance Method Summary collapse
- #confirm_expression?(value_a, expression, value_b) ⇒ Boolean
- #error_message(field, attributes = {}) ⇒ Object
- #validate(obj, field, attributes) ⇒ Object
Methods inherited from ValidationRule
#get_field_value, #is_required?
Instance Method Details
#confirm_expression?(value_a, expression, value_b) ⇒ Boolean
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
# File 'lib/validation_profiler/rules/rules.rb', line 426 def confirm_expression?(value_a, expression, value_b) a = value_a b = value_b if value_a.is_a?(String) a = "'#{value_a}'" elsif value_a == nil a = 'nil' end if value_b.is_a?(String) b = "'#{value_b}'" elsif value_b == nil b = 'nil' end eval("#{a} #{expression} #{b}") end |
#error_message(field, attributes = {}) ⇒ Object
362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/validation_profiler/rules/rules.rb', line 362 def (field, attributes = {}) match_field = attributes[:field] #check if a custom error message has been specified in the attributes if attributes[:message] == nil #no custom error message has been specified so create the default message. "#{field} is not valid" else attributes[:message] end end |
#validate(obj, field, attributes) ⇒ Object
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 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 |
# File 'lib/validation_profiler/rules/rules.rb', line 376 def validate(obj, field, attributes) #attempt to get the field value from the object value = get_field_value(obj, field) condition_field = attributes[:condition_field] if condition_field == nil raise InvalidRuleAttributes.new(ConditionValidationRule, field) end condition_value = attributes[:condition_value] if condition_value == nil raise InvalidRuleAttributes.new(ConditionValidationRule, field) end condition_expression = attributes[:condition_expression] if condition_expression == nil raise InvalidRuleAttributes.new(ConditionValidationRule, field) end field_expression = attributes[:field_expression] if field_expression == nil raise InvalidRuleAttributes.new(ConditionValidationRule, field) end field_value = attributes[:field_value] if field_value == nil raise InvalidRuleAttributes.new(ConditionValidationRule, field) end if !is_required?(value, attributes) return true end condition_field_value = get_field_value(obj, condition_field) #check if the condition is valid if confirm_expression?(condition_field_value, condition_expression, condition_value) #check if the field value is correct for the condition if confirm_expression?(value, field_expression, field_value) return true else return false end end return true end |