Skip to content

Commit

Permalink
Move after_init to a plugin based system
Browse files Browse the repository at this point in the history
  • Loading branch information
pjaspers authored and pjaspers committed Mar 3, 2022
1 parent 1c5c485 commit aae040c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/orbf/rules_engine/data/activity_state.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/orbf/rules_engine/data/invoice.rb
Original file line number Diff line number Diff line change
@@ -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] }
Expand Down
12 changes: 5 additions & 7 deletions lib/orbf/rules_engine/data/variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
26 changes: 25 additions & 1 deletion lib/orbf/rules_engine/value_object.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit aae040c

Please sign in to comment.