Class: Judge::Validation
- Inherits:
-
Object
- Object
- Judge::Validation
- Defined in:
- lib/judge/validation.rb
Instance Method Summary collapse
- #amv ⇒ Object
- #as_json(options = {}) ⇒ Object
-
#initialize(params) ⇒ Validation
constructor
A new instance of Validation.
- #record ⇒ Object
- #validate! ⇒ Object
Constructor Details
#initialize(params) ⇒ Validation
Returns a new instance of Validation.
4 5 6 7 8 9 10 11 |
# File 'lib/judge/validation.rb', line 4 def initialize(params) @klass = params[:klass] @attribute = params[:attribute] @value = params[:value] @kind = params[:kind] @original_value = params[:original_value] validate! end |
Instance Method Details
#amv ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/judge/validation.rb', line 13 def amv @amv ||= begin validators = @klass.validators_on(@attribute) validators.keep_if { |amv| amv.kind == @kind } validators.first end end |
#as_json(options = {}) ⇒ Object
35 36 37 |
# File 'lib/judge/validation.rb', line 35 def as_json( = {}) record.errors[@attribute] || [] end |
#record ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/judge/validation.rb', line 21 def record @record ||= begin rec = @klass.new rec[@attribute] = @value rec end end |
#validate! ⇒ Object
29 30 31 32 33 |
# File 'lib/judge/validation.rb', line 29 def validate! record.errors.delete(@attribute) amv.validate_each(record, @attribute, @value) unless amv.kind == :uniqueness && @value == @original_value && @original_value != "" self end |