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

#testBoolean

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

Reads ‘on`, `check`, and `is` from the kwargs stored at initialization.

Returns:

  • (Boolean)

    true if attribute matches expected value(s)



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

def test
  Array(kwargs[:is]).include?(on.public_send(check))
end