Class: ActsAsCalculator::PublishFormulaVersion

Inherits:
Object
  • Object
show all
Defined in:
lib/acts_as_calculator/publish_formula_version.rb

Overview

Adding a version is never just an insert. FormulaVersion refuses two active versions whose effective ranges overlap, so publishing a rule that replaces one already in force has to supersede the incumbent first — and SupersedeFormulaVersions refuses outright where that would leave part of the incumbent's range uncovered.

Every authoring path goes through here (JSON import, the API), so none of them can grow its own idea of what replacing a rule means or of how version numbers are handed out.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(formula:, expression:, effective_from:, effective_to: nil, status: FormulaVersion::ACTIVE, change_note: nil, variables: []) ⇒ PublishFormulaVersion

Returns a new instance of PublishFormulaVersion.



16
17
18
19
20
21
22
23
24
25
# File 'lib/acts_as_calculator/publish_formula_version.rb', line 16

def initialize(formula:, expression:, effective_from:, effective_to: nil,
               status: FormulaVersion::ACTIVE, change_note: nil, variables: [])
  @formula = formula
  @expression = expression.to_s
  @effective_from = CastDate.(effective_from)
  @effective_to = effective_to && CastDate.(effective_to)
  @status = status.to_s
  @change_note = change_note
  @variables = Array(variables).map { |variable| CastVariableAttributes.(variable) }
end

Class Method Details

.callObject



12
13
14
# File 'lib/acts_as_calculator/publish_formula_version.rb', line 12

def self.call(...)
  new(...).call
end

Instance Method Details

#callObject

requires_new so this is one unit of work even when the caller already has a transaction open: a rejected variable must not leave behind a version claiming to declare it, and a supersede must not outlive the version it made room for. A caller that rescues and carries on is exactly the case a joined transaction would not cover.



31
32
33
34
35
36
37
# File 'lib/acts_as_calculator/publish_formula_version.rb', line 31

def call
  Record.transaction(requires_new: true) do
    SupersedeFormulaVersions.(formula:, effective_from:, effective_to:) if status == FormulaVersion::ACTIVE

    create_version
  end
end