Module: SoftValidation::ClassMethods

Defined in:
lib/soft_validation.rb

Instance Method Summary (collapse)

Instance Method Details

- (SoftValidationMethod) add_method(method, options)

Parameters:

  • method (Symbol)
  • options (Hash)

Returns:



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

- (Object) add_to_set(method, options)

Parameters:

  • method (Hash)
  • options (Hash)


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

- (Hash) ancestor_klasses_with_validation

Returns:

  • (Hash)


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

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



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

- (Object) reset_soft_validation!



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

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

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

Parameters:

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

Returns:

  • (Boolean)


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

- (Boolean) soft_validates?

Returns always true indicates that this class has included SoftValidation

Returns:

  • (Boolean)

    always true indicates that this class has included SoftValidation



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

def soft_validates?
  true
end

- (Hash) soft_validation_descriptions

Returns:

  • (Hash)


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

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

an internal accessor for self.soft_validation_methods

Parameters:

  • set (Symbol)

    the set to return

  • ancestors (Boolean)

    whether to also return the ancestors validation methods

Returns:

  • (Array)

    of Symbol the names of the 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