Class: MVCLI::Validatable::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/mvcli/validatable.rb

Instance Method Summary collapse

Constructor Details

#initialize(field, message, options, predicate) ⇒ Rule

Returns a new instance of Rule.



168
169
170
# File 'lib/mvcli/validatable.rb', line 168

def initialize(field, message, options, predicate)
  @field, @message, @options, @predicate = field, message, options, predicate
end

Instance Method Details

#call(validatable, violations, errors) ⇒ Object



171
172
173
174
175
176
177
178
179
# File 'lib/mvcli/validatable.rb', line 171

def call(validatable, violations, errors)
  value, error = read validatable
  if error
    errors[@field] << error
  else
    return if value.nil? && !@options[:nil]
    violations[@field] << @message unless @predicate.call value
  end
end

#read(validatable) ⇒ Object



181
182
183
184
185
# File 'lib/mvcli/validatable.rb', line 181

def read(validatable)
  return validatable.send(@field), nil
rescue StandardError => e
  return nil, e
end