Class: ActA::Validator
- Inherits:
-
Object
- Object
- ActA::Validator
- Defined in:
- lib/act_a.rb
Instance Attribute Summary collapse
-
#keys ⇒ Object
Returns the value of attribute keys.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#record ⇒ Object
Returns the value of attribute record.
Instance Method Summary collapse
- #apply(key_and_values = {}) ⇒ Object
- #errors ⇒ Object
-
#initialize(klass) ⇒ Validator
constructor
A new instance of Validator.
- #messages ⇒ Object
- #valid? ⇒ Boolean
- #valid_brutally? ⇒ Boolean
- #validate ⇒ Object
- #validate! ⇒ Object
- #validate_brutally ⇒ Object
- #validate_brutally! ⇒ Object
Constructor Details
#initialize(klass) ⇒ Validator
Returns a new instance of Validator.
27 28 29 |
# File 'lib/act_a.rb', line 27 def initialize(klass) self.klass = klass end |
Instance Attribute Details
#keys ⇒ Object
Returns the value of attribute keys.
25 26 27 |
# File 'lib/act_a.rb', line 25 def keys @keys end |
#klass ⇒ Object
Returns the value of attribute klass.
25 26 27 |
# File 'lib/act_a.rb', line 25 def klass @klass end |
#record ⇒ Object
Returns the value of attribute record.
25 26 27 |
# File 'lib/act_a.rb', line 25 def record @record end |
Instance Method Details
#apply(key_and_values = {}) ⇒ Object
31 32 33 34 35 |
# File 'lib/act_a.rb', line 31 def apply(key_and_values = {}) self.keys = key_and_values.keys self.record = klass.new(key_and_values) self end |
#errors ⇒ Object
37 38 39 |
# File 'lib/act_a.rb', line 37 def errors record.errors end |
#messages ⇒ Object
41 42 43 |
# File 'lib/act_a.rb', line 41 def record.errors.try(:messages) end |
#valid? ⇒ Boolean
45 46 47 48 |
# File 'lib/act_a.rb', line 45 def valid? validate errors.empty? end |
#valid_brutally? ⇒ Boolean
50 51 52 53 |
# File 'lib/act_a.rb', line 50 def valid_brutally? validate_brutally errors.empty? end |
#validate ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/act_a.rb', line 55 def validate keys.each do |attribute_name| validators(attribute_name).each do |validator| duplicate_validator(validator, attribute_name).validate(record) end end rescue # do nothing ensure reject_not_target return self end |
#validate! ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/act_a.rb', line 68 def validate! if valid? self else raise ActiveRecord::RecordInvalid, record end end |
#validate_brutally ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/act_a.rb', line 76 def validate_brutally record.valid? rescue # do nothing ensure reject_not_target return self end |
#validate_brutally! ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/act_a.rb', line 85 def validate_brutally! if valid_brutally? self else raise ActiveRecord::RecordInvalid, record end end |