Class: Servus::Guards::PresenceGuard

Inherits:
Servus::Guard show all
Defined in:
lib/servus/guards/presence_guard.rb

Overview

Guard that ensures all provided values are present (not nil or empty).

This is a flexible guard that accepts any number of keyword arguments and validates that all values are present.

Examples:

Basic usage

class MyService < Servus::Base
  def call
    enforce_presence!(user: user, account: )
    # ...
  end
end

Single value

enforce_presence!(email: email)

Multiple values

enforce_presence!(user: user, account: , device: device)

Conditional check

if check_presence?(user: user)
  # user is present
end

Instance Attribute Summary

Attributes inherited from Servus::Guard

#kwargs

Instance Method Summary collapse

Methods inherited from Servus::Guard

derive_method_name, #error, error_code, execute!, execute?, http_status, inherited, #initialize, message, #message, #method_missing, register_guard_methods, #respond_to_missing?

Constructor Details

This class inherits a constructor from Servus::Guard

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Servus::Guard

Instance Method Details

#test(**values) ⇒ Boolean

Tests whether all provided values are present.

A value is considered present if it is not nil and not empty (for values that respond to empty?).

Parameters:

  • values (Hash)

    keyword arguments to validate

Returns:

  • (Boolean)

    true if all values are present



43
44
45
# File 'lib/servus/guards/presence_guard.rb', line 43

def test(**values)
  values.all? { |_, value| present?(value) }
end