Class: Servactory::Maintenance::Attributes::Validations::Must

Inherits:
Base
  • Object
show all
Defined in:
lib/servactory/maintenance/attributes/validations/must.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context:, attribute:, value:, check_options:) ⇒ Must

Returns a new instance of Must.



20
21
22
23
24
25
26
27
# File 'lib/servactory/maintenance/attributes/validations/must.rb', line 20

def initialize(context:, attribute:, value:, check_options:)
  super()

  @context = context
  @attribute = attribute
  @value = value
  @check_options = check_options
end

Class Method Details

.check(context:, attribute:, value:, check_key:, check_options:) ⇒ Object



8
9
10
11
12
# File 'lib/servactory/maintenance/attributes/validations/must.rb', line 8

def self.check(context:, attribute:, value:, check_key:, check_options:)
  return unless should_be_checked_for?(attribute, check_key)

  new(context:, attribute:, value:, check_options:).check
end

.should_be_checked_for?(attribute, check_key) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/servactory/maintenance/attributes/validations/must.rb', line 14

def self.should_be_checked_for?(attribute, check_key)
  check_key == :must && attribute.must_present?
end

Instance Method Details

#checkObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/servactory/maintenance/attributes/validations/must.rb', line 29

def check
  @check_options.each do |code, options|
    message, reason, meta = call_or_fetch_message_from(code, options)

    next if message.blank?

    add_error_with(message, code, reason, meta)
  end

  errors
end