Class: ErpRules::RulesEngine::ContextBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/erp_rules/rules_engine/context_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(facts) ⇒ ContextBuilder

Returns a new instance of ContextBuilder.



8
9
10
# File 'lib/erp_rules/rules_engine/context_builder.rb', line 8

def initialize(facts)
  @facts = facts
end

Instance Method Details

#build_and_save_context(context_klass, json, biz_txn_event) ⇒ Object



59
60
61
62
63
64
65
# 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 = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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_contextObject



46
47
# File 'lib/erp_rules/rules_engine/context_builder.rb', line 46

def customer_context()
end

#environment_contextObject



49
50
51
52
53
54
55
56
57
# 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) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# 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_contextObject



42
43
44
# File 'lib/erp_rules/rules_engine/context_builder.rb', line 42

def search_context()
  logger.warn "ContextBuilder.search_context() is an abstract method"
end