Module: SoftValidation::ClassMethods

Defined in:
lib/soft_validation.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) add_method(method, options)



114
115
116
117
118
# File 'lib/soft_validation.rb', line 114

def add_method(method, options)
  n                                       = self.name
  self.soft_validation_methods[n]         ||= {}
  self.soft_validation_methods[n][method] = SoftValidationMethod.new(options)
end

- (Object) add_to_set(method, options)



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/soft_validation.rb', line 120

def add_to_set(method, options)
  n   = self.name
  set = options[:set]

  self.soft_validation_sets[n] ||= {}

  self.soft_validation_sets[n][:all] ||= []
  self.soft_validation_sets[n][:all] << method

  if set
    self.soft_validation_sets[n][set] ||= []
    self.soft_validation_sets[n][set] << method
  end
end

- (Object) ancestor_klasses_with_validation



176
177
178
# File 'lib/soft_validation.rb', line 176

def ancestor_klasses_with_validation
  ANCESTORS_WITH_SOFT_VALIDATIONS[self]
end

- (Boolean) has_self_soft_validations?

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



143
144
145
# File 'lib/soft_validation.rb', line 143

def has_self_soft_validations?
  self.soft_validation_sets[self.name] && self.soft_validation_sets[self.name][:all].count > 0
end

- (Object) reset_soft_validation!



180
181
182
183
# File 'lib/soft_validation.rb', line 180

def reset_soft_validation!
  self.soft_validation_methods = {self.name => {}}
  self.soft_validation_sets = { self.name =>  {all:  [] } }
end

- (Object) soft_validate(method, options = {})

self.name is the class name, e.g. Otu



106
107
108
109
110
111
112
# File 'lib/soft_validation.rb', line 106

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

- (True) soft_validates?

Returns indicates that this class has included SoftValidation

Returns:

  • (True)

    indicates that this class has included SoftValidation



137
138
139
# File 'lib/soft_validation.rb', line 137

def soft_validates?
  true
end

- (Object) soft_validation_descriptions



168
169
170
171
172
173
174
# File 'lib/soft_validation.rb', line 168

def soft_validation_descriptions
  result = {}
  soft_validators.each do |v|
    result[v]
  end
  result
end

- (Array of Symbol) soft_validators(set: :all, include_ancestors: true)

an internal accessor for self.soft_validation_methods

Parameters:

  • the (:set, Symbol)

    set to return

  • whether (:ancestors, Boolean)

    to also return the ancestors validation methods

Returns:

  • (Array of Symbol)

    the names of the soft validation methods



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/soft_validation.rb', line 154

def soft_validators(set: :all, include_ancestors: true)
  methods = []
  # klass_validators = self.soft_validation_methods[self.name][set] if has_self_soft_validations?
  klass_validators = self.soft_validation_sets[self.name][set] if has_self_soft_validations?

  methods += klass_validators if !klass_validators.nil?
  if include_ancestors
    ancestor_klasses_with_validation.each do |klass|
      methods += klass.soft_validators(set: set, include_ancestors: false)
    end
  end
  methods
end