Module: PricingPlans::JobGuards

Defined in:
lib/pricing_plans/job_guards.rb

Overview

Lightweight ergonomics for background jobs and services

Class Method Summary collapse

Class Method Details

.with_plan_limit(limit_key, plan_owner:, by: 1, allow_system_override: false) {|result| ... } ⇒ Object

Runs the given block only if within limit or when system override is allowed. Returns the Result in all cases so callers can inspect state. Usage:

PricingPlans::JobGuards.with_plan_limit(:licenses, plan_owner: org, by: 1, allow_system_override: true) do |result|
# perform work; result.warning?/grace? can be surfaced
end

Yields:

  • (result)


14
15
16
17
18
19
20
21
22
# File 'lib/pricing_plans/job_guards.rb', line 14

def with_plan_limit(limit_key, plan_owner:, by: 1, allow_system_override: false)
  result = ControllerGuards.require_plan_limit!(limit_key, plan_owner: plan_owner, by: by, allow_system_override: allow_system_override)

  blocked_without_override = result.blocked? && !(allow_system_override && result. && result.[:system_override])
  return result if blocked_without_override

  yield(result) if block_given?
  result
end