Class: Objecheck::Validator::Collector
- Inherits:
-
Object
- Object
- Objecheck::Validator::Collector
- Defined in:
- lib/objecheck/validator/collector.rb
Overview
Collector is used in Validator#validate to aggregate errors
Instance Attribute Summary collapse
-
#current_rule_name ⇒ Object
readonly
Returns the value of attribute current_rule_name.
-
#current_t ⇒ Object
readonly
Returns the value of attribute current_t.
-
#prefix_stack ⇒ Object
readonly
Returns the value of attribute prefix_stack.
Instance Method Summary collapse
- #add_error(msg) ⇒ Object
- #add_prefix_in(prefix) ⇒ Object
- #commit(t) ⇒ Object
- #errors ⇒ Object
-
#initialize(validator, parent = nil) ⇒ Collector
constructor
A new instance of Collector.
- #rollback(t) ⇒ Object
- #transaction ⇒ Object
- #validate(target, rules) ⇒ Object
Constructor Details
#initialize(validator, parent = nil) ⇒ Collector
Returns a new instance of Collector.
22 23 24 25 26 27 28 29 |
# File 'lib/objecheck/validator/collector.rb', line 22 def initialize(validator, parent = nil) @validator = validator @current_validation_result = true @current_rule_name = parent ? parent.current_rule_name : nil @prefix_stack = parent ? [parent.prefix_stack.join('')] : [] @errors = [] @current_t = nil end |
Instance Attribute Details
#current_rule_name ⇒ Object (readonly)
Returns the value of attribute current_rule_name.
20 21 22 |
# File 'lib/objecheck/validator/collector.rb', line 20 def current_rule_name @current_rule_name end |
#current_t ⇒ Object (readonly)
Returns the value of attribute current_t.
20 21 22 |
# File 'lib/objecheck/validator/collector.rb', line 20 def current_t @current_t end |
#prefix_stack ⇒ Object (readonly)
Returns the value of attribute prefix_stack.
20 21 22 |
# File 'lib/objecheck/validator/collector.rb', line 20 def prefix_stack @prefix_stack end |
Instance Method Details
#add_error(msg) ⇒ Object
38 39 40 41 |
# File 'lib/objecheck/validator/collector.rb', line 38 def add_error(msg) @current_validation_result = false @errors << "#{@prefix_stack.join('')}: #{@current_rule_name}: #{msg}" end |
#add_prefix_in(prefix) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/objecheck/validator/collector.rb', line 31 def add_prefix_in(prefix) @prefix_stack.push(prefix) yield ensure @prefix_stack.pop end |
#commit(t) ⇒ Object
66 67 68 69 70 |
# File 'lib/objecheck/validator/collector.rb', line 66 def commit(t) check_transaction(t) @current_t = nil @errors.concat(t.errors) end |
#errors ⇒ Object
56 57 58 |
# File 'lib/objecheck/validator/collector.rb', line 56 def errors @errors.dup end |
#rollback(t) ⇒ Object
72 73 74 75 |
# File 'lib/objecheck/validator/collector.rb', line 72 def rollback(t) check_transaction(t) @current_t = nil end |
#transaction ⇒ Object
60 61 62 63 64 |
# File 'lib/objecheck/validator/collector.rb', line 60 def transaction raise Objecheck::Error, 'another transaction is already created' if @current_t @current_t = self.class.new(@validator, self) end |
#validate(target, rules) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/objecheck/validator/collector.rb', line 43 def validate(target, rules) prev_rule_name = @current_rule_name prev_validation_result = @current_validation_result rules.each do |name, rule| @current_rule_name = name rule.validate(target, self) end @current_validation_result ensure @current_rule_name = prev_rule_name @current_validation_result = prev_validation_result end |