Class: Servus::Guards::StateGuard

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

Overview

Guard that ensures an attribute matches an expected value or one of several allowed values.

Examples:

Single expected value

enforce_state!(on: order, check: :status, is: :pending)

Multiple allowed values (any match passes)

enforce_state!(on: , check: :status, is: [:active, :trial])

Conditional check

if check_state?(on: order, check: :status, is: :shipped)
  # order is shipped
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:, is:) ⇒ Boolean

Tests whether the attribute matches the expected value(s).

Parameters:

  • on (Object)

    the object to check

  • check (Symbol)

    the attribute to verify

  • is (Object, Array)

    expected value(s) - passes if attribute matches any

Returns:

  • (Boolean)

    true if attribute matches expected value(s)



31
32
33
# File 'lib/servus/guards/state_guard.rb', line 31

def test(on:, check:, is:) # rubocop:disable Naming/MethodParameterName
  Array(is).include?(on.public_send(check))
end