Class: Ronflex::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/ronflex/rule.rb

Overview

Class Rule

Represents a rule that associates a specific type (e.g., ‘:admin`, `:guest`) with a custom condition defined by a block of logic. The rule can then evaluate whether a model matches the type and satisfies the condition for a particular request.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) {|model, request| ... } ⇒ Rule

Initializes a new rule.

Parameters:

  • type (Symbol, String)

    The type of model this rule applies to. Typically a symbol representing a role (e.g., ‘:admin` or `:guest`).

Yields:

  • (model, request)

    The block defining the rule’s logic. It is executed to determine if the rule matches for a given model and request.

Yield Parameters:

  • model (Object)

    The model being evaluated (e.g., a user or role).

  • request (Object)

    The request being evaluated (e.g., an HTTP request object).



29
30
31
32
# File 'lib/ronflex/rule.rb', line 29

def initialize(type, &block)
  @type = type
  @rule = block
end

Instance Attribute Details

#ruleProc (readonly)

Returns the block of logic that determines if the rule matches a given model and request.

Returns:

  • (Proc)

    the block of logic that determines if the rule matches a given model and request.



19
20
21
# File 'lib/ronflex/rule.rb', line 19

def rule
  @rule
end

#typeSymbol, String (readonly)

Returns the type of the model this rule applies to (e.g., ‘:admin` or `:guest`).

Returns:

  • (Symbol, String)

    the type of the model this rule applies to (e.g., ‘:admin` or `:guest`).



16
17
18
# File 'lib/ronflex/rule.rb', line 16

def type
  @type
end

Instance Method Details

#matches?(model, request) ⇒ Boolean

Checks if the rule matches a given model and request.

This method evaluates whether the provided model matches the rule’s type and if the rule’s block returns ‘true` for the given model and request.

Parameters:

  • model (Object)

    The model to check (e.g., a user or role).

  • request (Object)

    The request to check (e.g., an HTTP request object).

Returns:

  • (Boolean)

    ‘true` if the model matches the rule’s type and satisfies the block’s logic, ‘false` otherwise.



42
43
44
# File 'lib/ronflex/rule.rb', line 42

def matches?(model, request)
  model == type && rule.call(model, request)
end