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:



153
154
155
156
157
158
159
160
# File 'lib/soft_validation.rb', line 153

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)


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, 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)


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.

Returns:

  • (Boolean)

    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

Parameters:

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

Returns:

  • (Boolean)


141
142
143
144
145
146
147
# File 'lib/soft_validation.rb', line 141

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



183
184
185
# File 'lib/soft_validation.rb', line 183

def soft_validates?
  true
end

#soft_validation_descriptionsHash

Returns:

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

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

Returns:

  • (Array)

    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.

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



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