class ErpRules::RulesEngine::ContextBuilder

Public Class Methods

new(facts) click to toggle source
# File lib/erp_rules/rules_engine/context_builder.rb, line 8
def initialize(facts)
  @facts = facts
end

Public Instance Methods

build_and_save_context(context_klass, json, biz_txn_event) click to toggle source
# File lib/erp_rules/rules_engine/context_builder.rb, line 59
def build_and_save_context(context_klass, json, biz_txn_event)
  txn_context = context_klass.new
  txn_context.base_txn_context = BaseTxnContext.new
  txn_context.base_txn_context.biz_txn_event = biz_txn_event
  txn_context.context = json
  txn_context.save
end
build_execution_context(options={}) click to toggle source
# File lib/erp_rules/rules_engine/context_builder.rb, line 12
def build_execution_context(options={})
  if options[:only].blank?
    execution_context = {
      :environment_context => self.environment_context(),
      :search_context => self.search_context(),
      :customer_context => self.customer_context()
    }
  else
    execution_context = {}
    options[:only].each do |context|
      execution_context[context] = self.send(context)
    end
  end

  ErpRules::RulesEngine::Context.new execution_context
end
customer_context() click to toggle source
# File lib/erp_rules/rules_engine/context_builder.rb, line 46
def customer_context()
end
environment_context() click to toggle source
# File lib/erp_rules/rules_engine/context_builder.rb, line 49
def environment_context()
  env_txn_context = {
    :time       => @facts[:time],
    :user_agent => @facts[:user_agent],
    :url        => @facts[:url],
    :remote_ip  => @facts[:remote_ip]
  }
  env_txn_context
end
persist_context(txn_type, txn_sub_type) click to toggle source
# File lib/erp_rules/rules_engine/context_builder.rb, line 29
def persist_context(txn_type, txn_sub_type)
  unless @execution_context.nil?
    search_txn = SearchTxn.new
    search_txn.biz_txn_event = BizTxnEvent.new
    search_txn.biz_txn_event.biz_txn_type = BizTxnType.find_by_type_and_subtype(txn_type,txn_sub_type)
    search_txn.biz_txn_event.description = "#{txn_type} , #{txn_sub_type}"

    build_and_save_context(SearchTxnContext, @execution_context[:search].to_json, search_txn.biz_txn_event) unless @execution_context[:search].nil?
    build_and_save_context(CustomerTxnContext, @execution_context[:customer].to_json, search_txn.biz_txn_event) unless @execution_context[:customer].nil?
    build_and_save_context(EnvironmentTxnContext, @execution_context[:environment].to_json, search_txn.biz_txn_event) unless @execution_context[:environment].nil?
  end
end
search_context() click to toggle source
# File lib/erp_rules/rules_engine/context_builder.rb, line 42
def search_context()
  logger.warn "ContextBuilder.search_context() is an abstract method"
end