Class: SoftValidation::SoftValidations
- Inherits:
-
Object
- Object
- SoftValidation::SoftValidations
- Defined in:
- lib/soft_validation/soft_validations.rb
Overview
A SoftValidations instance contains a set of SoftValidation(s). It tracks whether the validations (set) have been run and fixed.
Instance Attribute Summary collapse
-
#fixes_run ⇒ Object
Returns the value of attribute fixes_run.
-
#instance ⇒ Object
the object being validated, an instance of an ActiveRecord model.
-
#soft_validations ⇒ Array
the set of SoftValidations (i.e. problems with a record/instance).
-
#validated ⇒ Boolean
True if the soft validations methods have been called.
Instance Method Summary collapse
-
#add(attribute, message, options = {}) ⇒ Object
Add a soft validation to a data instance.
-
#complete? ⇒ Boolean
Soft validations run and none were generated.
-
#fix_messages ⇒ Hash<attribute><Array>
A hash listing the results of the fixes.
-
#fixes_run? ⇒ Boolean
Fixes on resultant soft validations have been run.
-
#initialize(instance) ⇒ SoftValidations
constructor
@param a instance of some ActiveRecord model.
- #messages ⇒ Array
- #messages_on(attribute) ⇒ Array
- #on(attribute) ⇒ Array
- #size ⇒ Object
-
#validated? ⇒ Boolean
Soft validations have been run.
Constructor Details
#initialize(instance) ⇒ SoftValidations
@param a instance of some ActiveRecord model
24 25 26 27 28 29 |
# File 'lib/soft_validation/soft_validations.rb', line 24 def initialize(instance) @validated = false @fixes_run = false @instance = instance # Klass from here <- stupid @soft_validations = [] end |
Instance Attribute Details
#fixes_run ⇒ Object
Returns the value of attribute fixes_run.
17 18 19 |
# File 'lib/soft_validation/soft_validations.rb', line 17 def fixes_run @fixes_run end |
#instance ⇒ Object
the object being validated, an instance of an ActiveRecord model
21 |
# File 'lib/soft_validation/soft_validations.rb', line 21 attr_writer :instance |
#soft_validations ⇒ Array
the set of SoftValidations (i.e. problems with a record/instance)
10 11 12 |
# File 'lib/soft_validation/soft_validations.rb', line 10 def soft_validations @soft_validations end |
#validated ⇒ Boolean
True if the soft validations methods have been called.
15 16 17 |
# File 'lib/soft_validation/soft_validations.rb', line 15 def validated @validated end |
Instance Method Details
#add(attribute, message, options = {}) ⇒ Object
Add a soft validation to a data instance.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/soft_validation/soft_validations.rb', line 39 def add(attribute, , = {}) # this is impossible to test. method = caller[0][/`(block\ in\ )*([^']*)'/, 2].to_sym # janky, the caller of this method, that is the method referenced in `soft_validate()`, used to get the fix for this Instance added raise SoftValidationError, "can not add soft validation to [#{attribute}] - not a column name or 'base'" if !(['base'] + @instance.class.column_names).include?(attribute.to_s) raise SoftValidationError, 'no :attribute or message provided to soft validation' if attribute.nil? || .nil? || .length == 0 .merge!( method_instance: @instance.class.soft_validation_methods[method], # inspected to expose method values attribute: attribute, message: , ) sv = ::SoftValidation::SoftValidation.new() @soft_validations << sv end |
#complete? ⇒ Boolean
Returns soft validations run and none were generated.
71 72 73 |
# File 'lib/soft_validation/soft_validations.rb', line 71 def complete? validated? && soft_validations.count == 0 end |
#fix_messages ⇒ Hash<attribute><Array>
Returns a hash listing the results of the fixes.
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/soft_validation/soft_validations.rb', line 77 def = {} if fixes_run? soft_validations.each do |v| [v.attribute] ||= [] [v.attribute].push << v. end end end |
#fixes_run? ⇒ Boolean
Returns fixes on resultant soft validations have been run.
65 66 67 |
# File 'lib/soft_validation/soft_validations.rb', line 65 def fixes_run? @fixes_run end |
#messages ⇒ Array
95 96 97 |
# File 'lib/soft_validation/soft_validations.rb', line 95 def soft_validations.collect{ |v| v.} end |
#messages_on(attribute) ⇒ Array
101 102 103 |
# File 'lib/soft_validation/soft_validations.rb', line 101 def (attribute) on(attribute).collect{|v| v.} end |
#on(attribute) ⇒ Array
90 91 92 |
# File 'lib/soft_validation/soft_validations.rb', line 90 def on(attribute) soft_validations.select{|v| v.attribute == attribute} end |
#size ⇒ Object
105 106 107 |
# File 'lib/soft_validation/soft_validations.rb', line 105 def size soft_validations.size end |
#validated? ⇒ Boolean
Returns soft validations have been run.
59 60 61 |
# File 'lib/soft_validation/soft_validations.rb', line 59 def validated? @validated end |