Module: OpenLoam::BusinessRules

Defined in:
lib/open_loam/business_rules.rb,
lib/open_loam/business_rules/actions.rb,
lib/open_loam/business_rules/condition.rb

Overview

The engine that evaluates OpenLoam::BusinessRule rows: WHEN a trigger fires and the condition holds, THEN run the actions — in the event's tenant context, in priority order, each rule isolated so one failure never breaks dispatch.

It listens to every OpenLoam event (like OpenLoam::Webhooks), so a tenant can add a rule from the admin without a reboot. Evaluation runs INLINE in whatever published the event — cheap for the prototype; a job-based dispatcher is the scaling path.

Defined Under Namespace

Modules: Actions, Condition

Constant Summary collapse

MAX_DEPTH =
3

Class Method Summary collapse

Class Method Details

.evaluate(record, trigger: nil) ⇒ Object

Manual evaluation for a record — for programmatic/test use. Runs the active rules for the record's entity type (optionally filtered to a trigger) in priority order.



42
43
44
45
46
47
48
# File 'lib/open_loam/business_rules.rb', line 42

def evaluate(record, trigger: nil)
  OpenLoam::BusinessRule.active.for_entity(record.class.base_class.name).by_priority.each do |rule|
    next if trigger && !rule.matches_trigger?(trigger)

    run_rule(rule, record, trigger)
  end
end

.on_event(event_name, payload) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/open_loam/business_rules.rb', line 24

def on_event(event_name, payload)
  tenant = OpenLoam::Tenant.find_by(id: payload[:tenant_id])
  return if tenant.nil?

  OpenLoam.as_tenant(tenant) do
    within_depth_guard do
      OpenLoam::BusinessRule.active.by_priority.each do |rule|
        next unless rule.matches_trigger?(event_name)

        run_rule(rule, subject_for(rule, payload), event_name)
      end
    end
  end
end

.subscribe! ⇒ Object

Wired once from OpenLoam::Engine. Idempotent — subscribing twice would run every rule twice (see OpenLoam::Webhooks.subscribe!).



20
21
22
# File 'lib/open_loam/business_rules.rb', line 20

def subscribe!
  @subscription ||= OpenLoam::Events.subscribe_all { |event_name, payload| on_event(event_name, payload) }
end

.veto?(record, trigger) ⇒ Boolean

Do the active rules with a block_transition action, whose condition holds, veto this transition? Consulted by a workflow BEFORE it moves (see the demo's DamageReport). Evaluate-only — no actions run, nothing logged.

Returns:

  • (Boolean)


53
54
55
56
57
58
59
# File 'lib/open_loam/business_rules.rb', line 53

def veto?(record, trigger)
  OpenLoam::BusinessRule.active.for_entity(record.class.base_class.name).by_priority.any? do |rule|
    rule.matches_trigger?(trigger) &&
      rule.action_list.any? { |action| action["type"].to_s == "block_transition" } &&
      Condition.matches?(rule.condition_tree, record)
  end
end