Class: Servus::Guards::FalseyGuard

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

Overview

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

Examples:

Single attribute

enforce_falsey!(on: user, check: :banned)

Multiple attributes (all must be falsey)

enforce_falsey!(on: post, check: [:deleted, :hidden, :flagged])

Conditional check

if check_falsey?(on: user, check: :suspended)
  # user is not suspended
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 falsey.

Parameters:

  • on (Object)

    the object to check

  • check (Symbol, Array<Symbol>)

    attribute(s) to verify

Returns:

  • (Boolean)

    true if all attributes are falsey



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

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