Functional Helpers

Functional helper methods for building expressive, chainable ActiveEntity operations

ActiveEntity provides several functional helper methods that enable cleaner, more expressive entity building without breaking the fluent chain or requiring intermediate variables. These methods were introduced in CBORM 4.6.0 and allow you to build entities using functional programming patterns.

Overview

The functional helpers provide flow control capabilities directly within your entity chains:

  • peek() - Inspect or log the entity during building without breaking the chain

  • when() - Conditionally execute logic based on a boolean expression

  • unless() - Execute logic when a condition is false (opposite of when())

  • throwIf() - Throw an exception if a condition is met

  • throwUnless() - Throw an exception unless a condition is met

All functional helpers return the entity itself, enabling continuous method chaining.

peek()

The peek() method allows you to inspect, log, or interact with the entity during the building process without breaking the chain. This is particularly useful for debugging or auditing entity state at specific points.

Signature

any function peek( required target )

Arguments

Argument
Type
Required
Description

target

closure

Yes

A closure that receives the entity as its argument

Examples

when()

The when() method provides conditional execution within your entity chain. If the boolean expression evaluates to true, the success closure executes; otherwise, the optional failure closure executes.

Signature

Arguments

Argument
Type
Required
Description

target

boolean

Yes

The boolean expression to evaluate

success

closure

Yes

Closure to execute if target is true

failure

closure

No

Closure to execute if target is false

Examples

unless()

The unless() method is the logical opposite of when(). It executes the success closure when the condition is false, and optionally executes the failure closure when the condition is true.

Signature

Arguments

Argument
Type
Required
Description

target

boolean

Yes

The boolean expression to evaluate

success

closure

Yes

Closure to execute if target is false

failure

closure

No

Closure to execute if target is true

Examples

throwIf()

The throwIf() method throws a controlled exception if the boolean condition evaluates to true. This is useful for inline validation and early failure detection in your entity chains.

Signature

Arguments

Argument
Type
Required
Default
Description

target

boolean

Yes

The boolean expression to evaluate

type

string

Yes

The exception type to throw

message

string

No

""

The exception message

detail

string

No

""

Additional exception details

Examples

throwUnless()

The throwUnless() method is the logical opposite of throwIf(). It throws a controlled exception if the boolean condition evaluates to false.

Signature

Arguments

Argument
Type
Required
Default
Description

target

boolean

Yes

The boolean expression to evaluate

type

string

Yes

The exception type to throw

message

string

No

""

The exception message

detail

string

No

""

Additional exception details

Examples

Best Practices

Chaining Multiple Helpers

You can chain multiple functional helpers together to build complex entity logic:

Use with CBValidation

Functional helpers work well alongside cbvalidation for comprehensive entity validation:

Performance Considerations

  • Avoid complex logic: Keep closures simple and focused on single responsibilities

  • Use peek() for debugging: Remove or comment out peek() calls in production for better performance

  • Prefer early validation: Use throwIf() and throwUnless() early in the chain to fail fast

Readability Tips

  • One condition per helper: Don't nest multiple conditions in a single when() or unless() call

  • Descriptive exception messages: Use clear, actionable messages in throwIf() and throwUnless()

  • Consistent formatting: Format chains consistently for better readability

Last updated

Was this helpful?