Class: Servus::Guards::TruthyGuard

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

Overview

Guard that ensures all specified attributes on an object are truthy.

Examples:

Single attribute

enforce_truthy!(on: user, check: :active)

Multiple attributes (all must be truthy)

enforce_truthy!(on: user, check: [:active, :verified, :confirmed])

Conditional check

if check_truthy?(on: subscription, check: :valid?)
  # subscription is valid
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(on:, check:) ⇒ Boolean

Tests whether all specified attributes are truthy.

Parameters:

  • on (Object)

    the object to check

  • check (Symbol, Array<Symbol>)

    attribute(s) to verify

Returns:

  • (Boolean)

    true if all attributes are truthy



30
31
32
# File 'lib/servus/guards/truthy_guard.rb', line 30

def test(on:, check:)
  Array(check).all? { |attr| !!on.public_send(attr) }
end