Module: SoftValidation::ClassMethods

Defined in:
lib/soft_validation.rb

Instance Method Summary collapse

Instance Method Details

#add_method(method, options) ⇒ SoftValidationMethod


122
123
124
125
126
# File 'lib/soft_validation.rb', line 122

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

#add_to_set(method, options) ⇒ Object


130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/soft_validation.rb', line 130

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

#ancestor_klasses_with_validationHash


201
202
203
# File 'lib/soft_validation.rb', line 201

def ancestor_klasses_with_validation
  ANCESTORS_WITH_SOFT_VALIDATIONS[self]
end

#has_self_soft_validations?Boolean


153
154
155
# File 'lib/soft_validation.rb', line 153

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

#reset_soft_validation!Object


205
206
207
208
# File 'lib/soft_validation.rb', line 205

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

#soft_validate(method, options = {}) ⇒ Boolean

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


111
112
113
114
115
116
117
# File 'lib/soft_validation.rb', line 111

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


147
148
149
# File 'lib/soft_validation.rb', line 147

def soft_validates?
  true
end

#soft_validation_descriptionsHash


192
193
194
195
196
197
198
# File 'lib/soft_validation.rb', line 192

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

#soft_validators(set: :all, include_ancestors: true, fixable_only: false) ⇒ Array

an internal accessor for self.soft_validation_methods


162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/soft_validation.rb', line 162

def soft_validators(set: :all, include_ancestors: true, fixable_only: false)
  methods = []
 
  klass_validators = []

  
  
  if has_self_soft_validations?
    a = self.soft_validation_sets[self.name][set]
  
    if fixable_only
      a.each do |m|
        klass_validators.unshift(m) if self.soft_validation_methods[self.name][m].fixable? 
      end
    else
      klass_validators = a if a # self.soft_validation_sets[self.name][set] if has_self_soft_validations?
    end
  end

  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, fixable_only: fixable_only)
    end
  end
  methods
end