From aae040cf4d4e2e98641e2ccb993d908469c8e916 Mon Sep 17 00:00:00 2001 From: pjaspers Date: Mon, 8 Jun 2020 11:19:38 +0200 Subject: [PATCH] Move after_init to a plugin based system --- lib/orbf/rules_engine/data/activity_state.rb | 1 + lib/orbf/rules_engine/data/invoice.rb | 1 + lib/orbf/rules_engine/data/variable.rb | 12 ++++----- lib/orbf/rules_engine/value_object.rb | 26 +++++++++++++++++++- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/lib/orbf/rules_engine/data/activity_state.rb b/lib/orbf/rules_engine/data/activity_state.rb index ebf2c84..a4340c6 100644 --- a/lib/orbf/rules_engine/data/activity_state.rb +++ b/lib/orbf/rules_engine/data/activity_state.rb @@ -1,6 +1,7 @@ module Orbf module RulesEngine class ActivityState < Orbf::RulesEngine::ValueObject::Model(:state, :ext_id, :name, :kind, :formula, :origin, :category_combo_ext_id) + plugin Plugins::AfterInitialize module Kinds KIND_CONSTANT = "constant".freeze diff --git a/lib/orbf/rules_engine/data/invoice.rb b/lib/orbf/rules_engine/data/invoice.rb index cc3ee02..ccd04c4 100644 --- a/lib/orbf/rules_engine/data/invoice.rb +++ b/lib/orbf/rules_engine/data/invoice.rb @@ -1,6 +1,7 @@ module Orbf module RulesEngine class ActivityItem < Orbf::RulesEngine::ValueObject::Model(:activity, :solution, :problem, :substitued, :variables) + plugin Plugins::AfterInitialize def after_init @indexed_variables = variables.index_by { |v| [v.state, v.activity_code] } diff --git a/lib/orbf/rules_engine/data/variable.rb b/lib/orbf/rules_engine/data/variable.rb index 84d4238..75efe96 100644 --- a/lib/orbf/rules_engine/data/variable.rb +++ b/lib/orbf/rules_engine/data/variable.rb @@ -7,7 +7,7 @@ class Variable < Orbf::RulesEngine::ValueObject::Model( :activity_code, :orgunit_ext_id, :formula, :package, :payment_rule, :exportable_variable_key, :category_option_combo_ext_id, :formula, :payment_rule) - + plugin Plugins::AfterInitialize class << self def new_activity_decision_table(params) Variable.with( @@ -195,17 +195,15 @@ def dhis2_period end end + def after_init + raise "Variable type '#{type}' must be one of #{Types::TYPES}" unless Types::TYPES.include?(type.to_s) + end + protected def values @values.slice(:key, :period, :expression, :type, :state, :activity_code, :orgunit_ext_id, :formula, :package, :payment_rule) end - - private - - def after_init - raise "Variable type '#{type}' must be one of #{Types::TYPES}" unless Types::TYPES.include?(type.to_s) - end end end end diff --git a/lib/orbf/rules_engine/value_object.rb b/lib/orbf/rules_engine/value_object.rb index 53ce276..a6882e3 100644 --- a/lib/orbf/rules_engine/value_object.rb +++ b/lib/orbf/rules_engine/value_object.rb @@ -1,4 +1,24 @@ # frozen_string_literal: true +module Orbf + module Plugins + module AfterInitialize + module ClassMethods + def call(_) + v = super + v.after_init + v + end + end + + module InstanceMethods + # An empty after_initialize hook, so that plugins that use this + # can always call super to get the default behavior. + def after_init + end + end + end + end +end module Orbf module RulesEngine @@ -59,9 +79,13 @@ def after_init end class << self + def plugin(a_module) + extend(a_module::ClassMethods) + include(a_module::InstanceMethods) + end + def with(hash) i = call(hash) - i.send(:after_init) i end end