Module: SoftValidation::ClassMethods

Defined in:
lib/soft_validation.rb

Instance Method Summary collapse

Instance Method Details

#add_method(method, options) ⇒ SoftValidationMethod

Parameters:

  • method (Symbol)

    the name of the method with the soft validation logic, in TW like ‘sv_foo`

  • options (Hash)

Returns:



150
151
152
153
154
155
156
157
# File 'lib/soft_validation.rb', line 150

def add_method(method, options)
  # 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(options))
end

#add_to_set(method, options) ⇒ Object

Parameters:

  • method (Hash)
  • options (Hash)


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, options)
  # TODO: update this to use setters?  Might not
  # be required because we are subgrouping by set.
  n = self.name
  set = options[: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_validationHash

Returns:

  • (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.

Returns:

  • (Boolean)

    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

Parameters:

  • method (Symbol)
  • options (Hash) (defaults to: {})

Returns:

  • (Boolean)


138
139
140
141
142
143
144
# File 'lib/soft_validation.rb', line 138

def soft_validate(method, options = {})
  options[:klass] = self
  options[:method] = method
  add_method(method, options)
  add_to_set(method, options)
  true
end

#soft_validates?Boolean

Returns always true indicates that this class has included SoftValidation.

Returns:

  • (Boolean)

    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_descriptionsHash

Returns:

  • (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_selfArray

Returns all methods from all sets from self (not superclasses).

Returns:

  • (Array)

    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.

Parameters:

  • only_sets (Array) (defaults to: [])

    names (symbols) of sets to run

  • except_sets (Array) (defaults to: [])

    names (symbols]

  • only_methods (Array) (defaults to: [])

    Names (symbols) of soft validation methods (not fix methods) to run. _If provided all other params are ignored._

  • except_methods (Array) (defaults to: [])

    Names (symbols) of soft validation methods to exclude. Ignored if only_methods is provided.

  • include_superclass (Boolean) (defaults to: true)

    include validations on superclasses, default is ‘true`

  • include_flagged (Boolean) (defaults to: false)

    some soft validations have more consequences, these are flagged, default ‘false`

  • fixable (Boolean) (defaults to: nil)

    run soft validations only on fixable records, default is ‘false`

Returns:

  • (Array)

    of Symbol the names of the soft validation methods



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