Class: RulesEngine::Publish::DbPublisher

Inherits:
Publisher
  • Object
show all
Defined in:
lib/rules_engine/publish/publisher/db_publisher.rb

Instance Method Summary collapse

Constructor Details

#initialize(*options) ⇒ DbPublisher

Returns a new instance of DbPublisher.



28
29
# File 'lib/rules_engine/publish/publisher/db_publisher.rb', line 28

def initialize(*options)        
end

Instance Method Details

#get(plan_code, plan_version = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rules_engine/publish/publisher/db_publisher.rb', line 46

def get(plan_code, plan_version = nil)
  return get_plan_without_caching(plan_code, plan_version) unless RulesEngine::Cache.perform_caching?
  
  plan = RulesEngine::Cache.cache_store.read(cache_code(plan_code, plan_version))
  if (plan.nil?)
    plan = get_plan_without_caching(plan_code, plan_version)
    RulesEngine::Cache.cache_store.write(cache_code(plan_code, plan_version), plan)
  end            
  
  plan
end

#history(plan_code, options = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rules_engine/publish/publisher/db_publisher.rb', line 58

def history(plan_code, options = {})
  re_published_plans = RePublishedPlan.plans(plan_code, options)
  
  {
    "publications" => re_published_plans.map do |re_published_plan|
      { 
        "plan_version" => re_published_plan.plan_version, 
        "version_tag" => re_published_plan.version_tag, 
        "published_at" => re_published_plan.published_at.utc.to_s
      }
    end  
  }
end

#publish(plan_code, version_tag, plan) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rules_engine/publish/publisher/db_publisher.rb', line 31

def publish(plan_code, version_tag, plan)
  
  re_plan = RePublishedPlan.plan(plan_code, {})
  
  plan_version = re_plan.nil? ? 1 : re_plan.plan_version + 1
  plan.merge!("code" => plan_code, "version" => plan_version, "tag" => version_tag)
  RePublishedPlan.create(:plan_code => plan_code, :plan_version => plan_version, :version_tag => version_tag, :data => plan.to_json, :published_at => Time.now.utc)
  
  if RulesEngine::Cache.perform_caching?
    RulesEngine::Cache.cache_store.write(cache_code(plan_code), plan)
  end
  
  plan_version
end

#remove(plan_code, plan_version = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/rules_engine/publish/publisher/db_publisher.rb', line 72

def remove(plan_code, plan_version = nil)
  plans = plan_version.nil? ? RePublishedPlan.plans(plan_code) : [RePublishedPlan.plan(plan_code, :version => plan_version)].compact
  plans.each do |re_published_plan|
    re_published_plan.destroy
    RulesEngine::Cache.cache_store.delete(cache_code(plan_code, re_published_plan.plan_version)) if RulesEngine::Cache.perform_caching?
  end
  
  RulesEngine::Cache.cache_store.delete(cache_code(plan_code)) if RulesEngine::Cache.perform_caching?
end