Class: PricingPlans::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/pricing_plans/registry.rb

Class Method Summary collapse

Class Method Details

.build_from_configuration(configuration) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/pricing_plans/registry.rb', line 6

def build_from_configuration(configuration)
  @plans = configuration.plans.dup
  @configuration = configuration
  @event_handlers = configuration.event_handlers.dup

  validate_registry!
  lint_usage_credits_integration!
  attach_plan_owner_helpers!
  attach_pending_association_limits!

  self
end

.clear!Object



19
20
21
22
23
# File 'lib/pricing_plans/registry.rb', line 19

def clear!
  @plans = nil
  @configuration = nil
  @event_handlers = nil
end

.configurationObject



39
40
41
# File 'lib/pricing_plans/registry.rb', line 39

def configuration
  @configuration
end

.default_planObject



63
64
65
66
# File 'lib/pricing_plans/registry.rb', line 63

def default_plan
  return nil unless @configuration
  plan(@configuration.default_plan)
end

.emit_event(event_type, limit_key, *args) ⇒ Object



77
78
79
80
# File 'lib/pricing_plans/registry.rb', line 77

def emit_event(event_type, limit_key, *args)
  # Delegate to Callbacks module for error-isolated execution
  Callbacks.dispatch(event_type, limit_key, *args)
end

.event_handlersObject



43
44
45
# File 'lib/pricing_plans/registry.rb', line 43

def event_handlers
  @event_handlers || { warning: {}, grace_start: {}, block: {} }
end

.highlighted_planObject



68
69
70
71
72
73
74
75
# File 'lib/pricing_plans/registry.rb', line 68

def highlighted_plan
  return nil unless @configuration
  if @configuration.highlighted_plan
    return plan(@configuration.highlighted_plan)
  end
  # Fallback to plan flagged highlighted in DSL
  plans.values.find(&:highlighted?)
end

.plan(key) ⇒ Object

Raises:



29
30
31
32
33
# File 'lib/pricing_plans/registry.rb', line 29

def plan(key)
  plan_obj = plans[key.to_sym]
  raise PlanNotFoundError, "Plan #{key} not found" unless plan_obj
  plan_obj
end

.plan_exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/pricing_plans/registry.rb', line 35

def plan_exists?(key)
  plans.key?(key.to_sym)
end

.plan_owner_classObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pricing_plans/registry.rb', line 47

def plan_owner_class
  return nil unless @configuration

  value = @configuration.plan_owner_class
  return nil unless value

  case value
  when String
    value.constantize
  when Class
    value
  else
    raise ConfigurationError, "plan_owner_class must be a string or class"
  end
end

.plansObject



25
26
27
# File 'lib/pricing_plans/registry.rb', line 25

def plans
  @plans || {}
end