Class: SoftValidation::SoftValidations
- Inherits:
-
Object
- Object
- SoftValidation::SoftValidations
- Defined in:
- lib/soft_validation/soft_validations.rb
Overview
A SoftValidations instance contains a set of SoftValidations and some code that tracks whether those validations have been fixed, etc.
Instance Attribute Summary collapse
-
#fixed ⇒ Symbol
True if fix() has been called.
-
#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
-
#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
- #resolution_for(method) ⇒ Array
- #size ⇒ Object
-
#validated? ⇒ Boolean
Soft validations have been run.
Constructor Details
#initialize(instance) ⇒ SoftValidations
@param a instance of some ActiveRecord model
23 24 25 26 27 28 |
# File 'lib/soft_validation/soft_validations.rb', line 23 def initialize(instance) @validated = false @fixes_run = false @instance = instance # Klass from here @soft_validations = [] end |
Instance Attribute Details
#fixed ⇒ Symbol
True if fix() has been called. Note that this does not imply that all SoftValidations have been fixed!
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/soft_validation/soft_validations.rb', line 19 class SoftValidations attr_accessor :soft_validations, :instance, :validated, :fixes_run # @param[ActiveRecord] a instance of some ActiveRecord model def initialize(instance) @validated = false @fixes_run = false @instance = instance # Klass from here @soft_validations = [] end # @param [Symbol] attribute a column attribute or :base # @param [String] message a message describing the soft validation to the user, i.e. what has gone wrong # @param [Hash{fix: :method_name, success_message: String, failure_message: String }] options the method identified by :fix should fully resolve the SoftValidation. def add(attribute, , = {}) 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, 'invalid :fix_trigger' if ![:fix_trigger].blank? && ![:all, :automatic, :requested].include?([:fix_trigger]) return false if attribute.nil? || .nil? || .length == 0 return false if ([:success_message] || [:failure_message]) && ![:fix] [:attribute] = attribute [:message] = [:resolution] = resolution_for([:resolution_with]) .delete(:resolution_with) sv = SoftValidation.new() sv.fix_trigger ||= :automatic @soft_validations << sv end # def soft_validations(scope = :all) # set = ( scope == :all ? [:automatic, :requested] : [scope] ) # @soft_validations.select{|v| set.include?(v.fix_trigger)} # end # @return [Boolean] # soft validations have been run def validated? @validated end # @param [Symbol, String] method # @return [Array] def resolution_for(method) return [] if method.nil? self.instance.class.soft_validation_methods[self.instance.class.name][method].resolution end # @return [Boolean] # fixes on resultant soft validations have been run def fixes_run? @fixes_run end # @return [Boolean] # soft validations run and none were generated def complete? validated? && soft_validations.count == 0 end # @return [Hash<attribute><Array>] # a hash listing the results of the fixes def = {} if fixes_run? soft_validations.each do |v| [v.attribute] ||= [] [v.attribute] << (v.) end end end # @param [Symbol] attribute # @return [Array] def on(attribute) soft_validations.select{|v| v.attribute == attribute} end # @return [Array] def soft_validations.collect{ |v| v.} end # @param [Symbol] attribute # @return [Array] def (attribute) on(attribute).collect{|v| v.} end def size soft_validations.size end end |
#fixes_run ⇒ Object
Returns the value of attribute fixes_run.
20 21 22 |
# File 'lib/soft_validation/soft_validations.rb', line 20 def fixes_run @fixes_run end |
#instance ⇒ Object
the object being validated, an instance of an ActiveRecord model
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/soft_validation/soft_validations.rb', line 19 class SoftValidations attr_accessor :soft_validations, :instance, :validated, :fixes_run # @param[ActiveRecord] a instance of some ActiveRecord model def initialize(instance) @validated = false @fixes_run = false @instance = instance # Klass from here @soft_validations = [] end # @param [Symbol] attribute a column attribute or :base # @param [String] message a message describing the soft validation to the user, i.e. what has gone wrong # @param [Hash{fix: :method_name, success_message: String, failure_message: String }] options the method identified by :fix should fully resolve the SoftValidation. def add(attribute, , = {}) 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, 'invalid :fix_trigger' if ![:fix_trigger].blank? && ![:all, :automatic, :requested].include?([:fix_trigger]) return false if attribute.nil? || .nil? || .length == 0 return false if ([:success_message] || [:failure_message]) && ![:fix] [:attribute] = attribute [:message] = [:resolution] = resolution_for([:resolution_with]) .delete(:resolution_with) sv = SoftValidation.new() sv.fix_trigger ||= :automatic @soft_validations << sv end # def soft_validations(scope = :all) # set = ( scope == :all ? [:automatic, :requested] : [scope] ) # @soft_validations.select{|v| set.include?(v.fix_trigger)} # end # @return [Boolean] # soft validations have been run def validated? @validated end # @param [Symbol, String] method # @return [Array] def resolution_for(method) return [] if method.nil? self.instance.class.soft_validation_methods[self.instance.class.name][method].resolution end # @return [Boolean] # fixes on resultant soft validations have been run def fixes_run? @fixes_run end # @return [Boolean] # soft validations run and none were generated def complete? validated? && soft_validations.count == 0 end # @return [Hash<attribute><Array>] # a hash listing the results of the fixes def = {} if fixes_run? soft_validations.each do |v| [v.attribute] ||= [] [v.attribute] << (v.) end end end # @param [Symbol] attribute # @return [Array] def on(attribute) soft_validations.select{|v| v.attribute == attribute} end # @return [Array] def soft_validations.collect{ |v| v.} end # @param [Symbol] attribute # @return [Array] def (attribute) on(attribute).collect{|v| v.} end def size soft_validations.size end end |
#soft_validations ⇒ Array
the set of SoftValidations (i.e. problems with a record/instance)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/soft_validation/soft_validations.rb', line 19 class SoftValidations attr_accessor :soft_validations, :instance, :validated, :fixes_run # @param[ActiveRecord] a instance of some ActiveRecord model def initialize(instance) @validated = false @fixes_run = false @instance = instance # Klass from here @soft_validations = [] end # @param [Symbol] attribute a column attribute or :base # @param [String] message a message describing the soft validation to the user, i.e. what has gone wrong # @param [Hash{fix: :method_name, success_message: String, failure_message: String }] options the method identified by :fix should fully resolve the SoftValidation. def add(attribute, , = {}) 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, 'invalid :fix_trigger' if ![:fix_trigger].blank? && ![:all, :automatic, :requested].include?([:fix_trigger]) return false if attribute.nil? || .nil? || .length == 0 return false if ([:success_message] || [:failure_message]) && ![:fix] [:attribute] = attribute [:message] = [:resolution] = resolution_for([:resolution_with]) .delete(:resolution_with) sv = SoftValidation.new() sv.fix_trigger ||= :automatic @soft_validations << sv end # def soft_validations(scope = :all) # set = ( scope == :all ? [:automatic, :requested] : [scope] ) # @soft_validations.select{|v| set.include?(v.fix_trigger)} # end # @return [Boolean] # soft validations have been run def validated? @validated end # @param [Symbol, String] method # @return [Array] def resolution_for(method) return [] if method.nil? self.instance.class.soft_validation_methods[self.instance.class.name][method].resolution end # @return [Boolean] # fixes on resultant soft validations have been run def fixes_run? @fixes_run end # @return [Boolean] # soft validations run and none were generated def complete? validated? && soft_validations.count == 0 end # @return [Hash<attribute><Array>] # a hash listing the results of the fixes def = {} if fixes_run? soft_validations.each do |v| [v.attribute] ||= [] [v.attribute] << (v.) end end end # @param [Symbol] attribute # @return [Array] def on(attribute) soft_validations.select{|v| v.attribute == attribute} end # @return [Array] def soft_validations.collect{ |v| v.} end # @param [Symbol] attribute # @return [Array] def (attribute) on(attribute).collect{|v| v.} end def size soft_validations.size end end |
#validated ⇒ Boolean
True if the soft validations methods have been called.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/soft_validation/soft_validations.rb', line 19 class SoftValidations attr_accessor :soft_validations, :instance, :validated, :fixes_run # @param[ActiveRecord] a instance of some ActiveRecord model def initialize(instance) @validated = false @fixes_run = false @instance = instance # Klass from here @soft_validations = [] end # @param [Symbol] attribute a column attribute or :base # @param [String] message a message describing the soft validation to the user, i.e. what has gone wrong # @param [Hash{fix: :method_name, success_message: String, failure_message: String }] options the method identified by :fix should fully resolve the SoftValidation. def add(attribute, , = {}) 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, 'invalid :fix_trigger' if ![:fix_trigger].blank? && ![:all, :automatic, :requested].include?([:fix_trigger]) return false if attribute.nil? || .nil? || .length == 0 return false if ([:success_message] || [:failure_message]) && ![:fix] [:attribute] = attribute [:message] = [:resolution] = resolution_for([:resolution_with]) .delete(:resolution_with) sv = SoftValidation.new() sv.fix_trigger ||= :automatic @soft_validations << sv end # def soft_validations(scope = :all) # set = ( scope == :all ? [:automatic, :requested] : [scope] ) # @soft_validations.select{|v| set.include?(v.fix_trigger)} # end # @return [Boolean] # soft validations have been run def validated? @validated end # @param [Symbol, String] method # @return [Array] def resolution_for(method) return [] if method.nil? self.instance.class.soft_validation_methods[self.instance.class.name][method].resolution end # @return [Boolean] # fixes on resultant soft validations have been run def fixes_run? @fixes_run end # @return [Boolean] # soft validations run and none were generated def complete? validated? && soft_validations.count == 0 end # @return [Hash<attribute><Array>] # a hash listing the results of the fixes def = {} if fixes_run? soft_validations.each do |v| [v.attribute] ||= [] [v.attribute] << (v.) end end end # @param [Symbol] attribute # @return [Array] def on(attribute) soft_validations.select{|v| v.attribute == attribute} end # @return [Array] def soft_validations.collect{ |v| v.} end # @param [Symbol] attribute # @return [Array] def (attribute) on(attribute).collect{|v| v.} end def size soft_validations.size end end |
Instance Method Details
#add(attribute, message, options = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/soft_validation/soft_validations.rb', line 33 def add(attribute, , = {}) 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, 'invalid :fix_trigger' if ![:fix_trigger].blank? && ![:all, :automatic, :requested].include?([:fix_trigger]) return false if attribute.nil? || .nil? || .length == 0 return false if ([:success_message] || [:failure_message]) && ![:fix] [:attribute] = attribute [:message] = [:resolution] = resolution_for([:resolution_with]) .delete(:resolution_with) sv = SoftValidation.new() sv.fix_trigger ||= :automatic @soft_validations << sv end |
#complete? ⇒ Boolean
Returns soft validations run and none were generated.
77 78 79 |
# File 'lib/soft_validation/soft_validations.rb', line 77 def complete? validated? && soft_validations.count == 0 end |
#fix_messages ⇒ Hash<attribute><Array>
Returns a hash listing the results of the fixes.
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/soft_validation/soft_validations.rb', line 83 def = {} if fixes_run? soft_validations.each do |v| [v.attribute] ||= [] [v.attribute] << (v.) end end end |
#fixes_run? ⇒ Boolean
Returns fixes on resultant soft validations have been run.
71 72 73 |
# File 'lib/soft_validation/soft_validations.rb', line 71 def fixes_run? @fixes_run end |
#messages ⇒ Array
101 102 103 |
# File 'lib/soft_validation/soft_validations.rb', line 101 def soft_validations.collect{ |v| v.} end |
#messages_on(attribute) ⇒ Array
107 108 109 |
# File 'lib/soft_validation/soft_validations.rb', line 107 def (attribute) on(attribute).collect{|v| v.} end |
#on(attribute) ⇒ Array
96 97 98 |
# File 'lib/soft_validation/soft_validations.rb', line 96 def on(attribute) soft_validations.select{|v| v.attribute == attribute} end |
#resolution_for(method) ⇒ Array
64 65 66 67 |
# File 'lib/soft_validation/soft_validations.rb', line 64 def resolution_for(method) return [] if method.nil? self.instance.class.soft_validation_methods[self.instance.class.name][method].resolution end |
#size ⇒ Object
111 112 113 |
# File 'lib/soft_validation/soft_validations.rb', line 111 def size soft_validations.size end |
#validated? ⇒ Boolean
Returns soft validations have been run.
58 59 60 |
# File 'lib/soft_validation/soft_validations.rb', line 58 def validated? @validated end |