Module: ActsAsCalculator::Calculable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/acts_as_calculator/calculable.rb
Instance Method Summary collapse
-
#calculate(key, as_of: nil, scope: nil, owner: nil, dry_run: false, context: {}, **extra) ⇒ Object
Leftover keywords splat into the calculation context, which makes
as_of,scope,owneranddry_rununusable as Dentaku variable names;context:is the escape hatch for a formula that genuinely declares one, and wins over the splat. - #calculate_as_of(key, as_of, **context) ⇒ Object
- #calculation_history(key = nil, limit: nil) ⇒ Object
- #calculator_owner ⇒ Object
-
#render(key, calculate: nil, result: nil, results: {}, as_of: nil, scope: nil, owner: nil, version_number: nil, dry_run: false, context: {}, **extra) ⇒ Object
Chains a calculation and a render in one call —
calculate:names one formula key (assigned asresult) or several (assigned asresults) — or renders a Result the caller already has.
Instance Method Details
#calculate(key, as_of: nil, scope: nil, owner: nil, dry_run: false, context: {}, **extra) ⇒ Object
Leftover keywords splat into the calculation context, which makes as_of, scope,
owner and dry_run unusable as Dentaku variable names; context: is the escape
hatch for a formula that genuinely declares one, and wins over the splat.
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/acts_as_calculator/calculable.rb', line 30 def calculate(key, as_of: nil, scope: nil, owner: nil, dry_run: false, context: {}, **extra) EvaluateFormula.( calculable: self, key:, scope: scope || calculator_scope_name, owner: owner || calculator_owner, as_of:, context: extra.merge(context), dry_run: ) end |
#calculate_as_of(key, as_of, **context) ⇒ Object
42 43 44 |
# File 'lib/acts_as_calculator/calculable.rb', line 42 def calculate_as_of(key, as_of, **context) calculate(key, as_of:, **context) end |
#calculation_history(key = nil, limit: nil) ⇒ Object
46 47 48 49 50 |
# File 'lib/acts_as_calculator/calculable.rb', line 46 def calculation_history(key = nil, limit: nil) runs = calculator_runs.recent_first runs = runs.for_formula_key(key) unless key.nil? limit.nil? ? runs : runs.limit(limit) end |
#calculator_owner ⇒ Object
52 53 54 |
# File 'lib/acts_as_calculator/calculable.rb', line 52 def calculator_owner nil end |
#render(key, calculate: nil, result: nil, results: {}, as_of: nil, scope: nil, owner: nil, version_number: nil, dry_run: false, context: {}, **extra) ⇒ Object
Chains a calculation and a render in one call — calculate: names one formula key
(assigned as result) or several (assigned as results) — or renders a Result the
caller already has. Leftover keywords splat into the template's assigns and into
the calculation's context, so one call can feed both.
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/acts_as_calculator/calculable.rb', line 60 def render(key, calculate: nil, result: nil, results: {}, as_of: nil, scope: nil, owner: nil, version_number: nil, dry_run: false, context: {}, **extra) assigns = extra.merge(context) computed = calculator_render_results(calculate, as_of:, scope:, owner:, dry_run:, context: assigns) RenderTemplate.( key:, version_number:, context: assigns, scope: scope || calculator_scope_name, owner: owner || calculator_owner, result: result || (computed.values.first unless calculate.is_a?(Array)), results: results.merge(computed) ) end |