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 chainwhen()- Conditionally execute logic based on a boolean expressionunless()- Execute logic when a condition is false (opposite ofwhen())throwIf()- Throw an exception if a condition is metthrowUnless()- 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
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
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
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
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
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 performancePrefer early validation: Use
throwIf()andthrowUnless()early in the chain to fail fast
Readability Tips
One condition per helper: Don't nest multiple conditions in a single
when()orunless()callDescriptive exception messages: Use clear, actionable messages in
throwIf()andthrowUnless()Consistent formatting: Format chains consistently for better readability
These functional helpers enable cleaner, more expressive entity building without breaking the fluent chain or requiring intermediate variables. They promote functional programming patterns and make entity logic more declarative and easier to test.
Related Topics
Last updated
Was this helpful?