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:



120
121
122
123
124
# File 'lib/soft_validation.rb', line 120

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)


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

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)


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

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



151
152
153
# File 'lib/soft_validation.rb', line 151

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!



188
189
190
191
# File 'lib/soft_validation.rb', line 188

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)


109
110
111
112
113
114
115
# File 'lib/soft_validation.rb', line 109

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



145
146
147
# File 'lib/soft_validation.rb', line 145

def soft_validates?
  true
end

- (Hash) soft_validation_descriptions

Returns:

  • (Hash)


175
176
177
178
179
180
181
# File 'lib/soft_validation.rb', line 175

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

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

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



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/soft_validation.rb', line 160

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