Composable rules
Build larger decisions from small named checks, with boolean combinators and section grouping.
Move scattered boolean checks into named, composable predicates. Keep your controllers, models, jobs and interfaces aligned around the same business logic.
Get startedView source ↗Predicate.define(:product) do
has_name { |s| present?(s[:name]) }
in_stock { |s| s[:quantity] > 0 }
ready_to_sell do |s|
has_name(s) && in_stock(s)
end
end
Predicate.for(:product).call(
:ready_to_sell,
{ name: 'Coffee', quantity: 4 }
)
# => trueBuild larger decisions from small named checks, with boolean combinators and section grouping.
Expose predicate methods on models and reuse the same decisions in controllers, views and jobs.
State-based caching, optional TTL, thread-safe cache operations and evaluation statistics.
Use it for onboarding readiness, multi-step forms, checkout requirements and feature eligibility. Keep authorization enforcement in your application's policy boundary.
Add the gem to your Gemfile:
gem 'predicate-rb'
Run bundle install. The library is required as predicate.
By default, missing or failing predicates evaluate to false. Negating that result can produce true. For decisions where evaluation errors must not be silently accepted, use strict mode and handle exceptions explicitly. Include all decision-relevant inputs in cached state.