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
150 151 152 153 154 155 156 157 |
# File 'lib/soft_validation.rb', line 150 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
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/soft_validation.rb', line 161 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
208 209 210 |
# File 'lib/soft_validation.rb', line 208 def ancestor_klasses_with_validation ANCESTORS_WITH_SOFT_VALIDATIONS[self] end |
#get_sets(only_sets = [], except_sets = []) ⇒ Object (private)
288 289 290 291 292 293 |
# File 'lib/soft_validation.rb', line 288 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.
186 187 188 |
# File 'lib/soft_validation.rb', line 186 def has_self_soft_validations? soft_validation_methods_on_self.any? end |
#reset_soft_validation! ⇒ Object (private)
283 284 285 286 |
# File 'lib/soft_validation.rb', line 283 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
138 139 140 141 142 143 144 |
# File 'lib/soft_validation.rb', line 138 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.
180 181 182 |
# File 'lib/soft_validation.rb', line 180 def soft_validates? true end |
#soft_validation_descriptions ⇒ Hash
199 200 201 202 203 204 205 |
# File 'lib/soft_validation.rb', line 199 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).
192 193 194 195 196 |
# File 'lib/soft_validation.rb', line 192 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.
237 238 239 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 |
# File 'lib/soft_validation.rb', line 237 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 |