Module: SoftValidation::ClassMethods
- Defined in:
- lib/soft_validation.rb
Instance Method Summary collapse
- #add_method(method, options) ⇒ SoftValidationMethod
- #add_to_set(method, options) ⇒ Object
- #ancestor_klasses_with_validation ⇒ Hash
- #get_sets(only_sets = [], except_sets = []) ⇒ Object private
-
#has_self_soft_validations? ⇒ Boolean
True if at least on soft_validate() exists in this class.
- #reset_soft_validation! ⇒ Object private
-
#soft_validate(method, options = {}) ⇒ Boolean
self.name is the class name, e.g.
-
#soft_validates? ⇒ Boolean
Always true indicates that this class has included SoftValidation.
- #soft_validation_descriptions ⇒ Hash
-
#soft_validation_methods_on_self ⇒ Array
All methods from all sets from self (not superclasses).
-
#soft_validators(only_sets: [], except_sets: [], only_methods: [], except_methods: [], include_flagged: false, fixable: nil, include_superclass: true) ⇒ Array
An internal accessor for self.soft_validation_methods.
Instance Method Details
#add_method(method, options) ⇒ SoftValidationMethod
153 154 155 156 157 158 159 160 |
# File 'lib/soft_validation.rb', line 153 def add_method(method, ) # Yes, this has to be self. # # The critical insight is to use the `=` to access the setter method. This allows the subclasses to have their own copy of `soft_validation_methods` # See https://api.rubyonrails.org/classes/Class.html # b self.soft_validation_methods = self.soft_validation_methods.merge(method => SoftValidationMethod.new()) end |
#add_to_set(method, options) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/soft_validation.rb', line 164 def add_to_set(method, ) # TODO: update this to use setters? Might not # be required because we are subgrouping by set. n = self.name set = [:set] soft_validation_sets[n] ||= {} if set soft_validation_sets[n][set] ||= [] soft_validation_sets[n][set] << method else soft_validation_sets[n][:default] ||= [] soft_validation_sets[n][:default] << method end end |
#ancestor_klasses_with_validation ⇒ Hash
211 212 213 |
# File 'lib/soft_validation.rb', line 211 def ancestor_klasses_with_validation ANCESTORS_WITH_SOFT_VALIDATIONS[self] end |
#get_sets(only_sets = [], except_sets = []) ⇒ Object (private)
291 292 293 294 295 296 |
# File 'lib/soft_validation.rb', line 291 def get_sets(only_sets = [], except_sets = []) all_sets = soft_validation_sets[name]&.keys return [] if all_sets.nil? a = (all_sets - except_sets) only_sets.empty? ? a : a & only_sets end |
#has_self_soft_validations? ⇒ Boolean
Returns true if at least on soft_validate() exists in this class.
189 190 191 |
# File 'lib/soft_validation.rb', line 189 def has_self_soft_validations? soft_validation_methods_on_self.any? end |
#reset_soft_validation! ⇒ Object (private)
286 287 288 289 |
# File 'lib/soft_validation.rb', line 286 def reset_soft_validation! self.soft_validation_methods = { } self.soft_validation_sets = { self.name => { default: []}} end |
#soft_validate(method, options = {}) ⇒ Boolean
self.name is the class name, e.g. Otu
141 142 143 144 145 146 147 |
# File 'lib/soft_validation.rb', line 141 def soft_validate(method, = {}) [:klass] = self [:method] = method add_method(method, ) add_to_set(method, ) true end |
#soft_validates? ⇒ Boolean
Returns always true indicates that this class has included SoftValidation.
183 184 185 |
# File 'lib/soft_validation.rb', line 183 def soft_validates? true end |
#soft_validation_descriptions ⇒ Hash
202 203 204 205 206 207 208 |
# File 'lib/soft_validation.rb', line 202 def soft_validation_descriptions result = {} soft_validators.each do |v| result[v] end result end |
#soft_validation_methods_on_self ⇒ Array
Returns all methods from all sets from self (not superclasses).
195 196 197 198 199 |
# File 'lib/soft_validation.rb', line 195 def soft_validation_methods_on_self a = soft_validation_sets[name]&.keys return [] if a.nil? a.collect{|s| soft_validation_sets[name][s] }.flatten end |
#soft_validators(only_sets: [], except_sets: [], only_methods: [], except_methods: [], include_flagged: false, fixable: nil, include_superclass: true) ⇒ Array
An internal accessor for self.soft_validation_methods. If nothing is provided all possible specs, excluding those flagged are returned.
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/soft_validation.rb', line 240 def soft_validators(only_sets: [], except_sets: [], only_methods: [], except_methods: [], include_flagged: false, fixable: nil, include_superclass: true) only_methods = Utilities::Params.arrayify(only_methods) return only_methods if !only_methods.empty? except_methods = Utilities::Params.arrayify(except_methods) # Get sets sets = get_sets( Utilities::Params.arrayify(only_sets), Utilities::Params.arrayify(except_sets) ) methods = [] klass_validators = [] # Return "Local" (this class only) validators if has_self_soft_validations? a = [] if sets.empty? && only_sets.empty? && except_sets.empty? # no sets provided, default to all methods a = self.soft_validation_methods.keys # self.soft_validation_method_names else sets.each do |s| a += self.soft_validation_sets[self.name][s] end end a.delete_if{|n| !self.soft_validation_methods[n].send(:matches?, fixable, include_flagged) } methods += a end # Add the rest of the validators, from Superclasses if include_superclass ancestor_klasses_with_validation.each do |klass| methods += klass.soft_validators(include_superclass: false, only_sets: only_sets, except_sets: except_sets, except_methods: except_methods, include_flagged: include_flagged, fixable: fixable) end end # Get rid of explicitly excluded methods.delete_if{|m| except_methods.include?(m) } methods end |