Class: Formulary::DrugListGenerator
- Inherits:
-
Object
- Object
- Formulary::DrugListGenerator
- Defined in:
- lib/formulary/drug_list_generator.rb
Overview
This class generates FormularyDrug resources for a particular plan
Instance Attribute Summary collapse
-
#plan ⇒ Object
readonly
Returns the value of attribute plan.
Instance Method Summary collapse
- #add_to_list(id) ⇒ Object private
- #base_output_directory ⇒ Object private
- #count ⇒ Object private
- #create_drug(drug) ⇒ Object private
- #generate ⇒ Object
- #id_prefix ⇒ Object
- #increment_id ⇒ Object private
-
#initialize(plan) ⇒ DrugListGenerator
constructor
A new instance of DrugListGenerator.
- #list ⇒ Object private
- #plan_drugs ⇒ Object private
- #write_formulary ⇒ Object private
- #write_payer_insurance_plan ⇒ Object private
- #write_resource(resource) ⇒ Object private
Constructor Details
#initialize(plan) ⇒ DrugListGenerator
Returns a new instance of DrugListGenerator.
18 19 20 |
# File 'lib/formulary/drug_list_generator.rb', line 18 def initialize(plan) @plan = plan end |
Instance Attribute Details
#plan ⇒ Object (readonly)
Returns the value of attribute plan.
16 17 18 |
# File 'lib/formulary/drug_list_generator.rb', line 16 def plan @plan end |
Instance Method Details
#add_to_list(id) ⇒ Object (private)
93 94 95 96 97 98 99 |
# File 'lib/formulary/drug_list_generator.rb', line 93 def add_to_list(id) list << { item: { reference: "MedicationKnowledge/#{id}", }, } end |
#base_output_directory ⇒ Object (private)
39 40 41 |
# File 'lib/formulary/drug_list_generator.rb', line 39 def base_output_directory "output" end |
#count ⇒ Object (private)
43 44 45 |
# File 'lib/formulary/drug_list_generator.rb', line 43 def count @count ||= 1 end |
#create_drug(drug) ⇒ Object (private)
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/formulary/drug_list_generator.rb', line 63 def create_drug(drug) plan_drug = QHPPlanDrug.new(drug, plan) id = "#{id_prefix}-#{count.to_s.rjust(5, "0")}" # if the right of the = is not called, it causes the program to crash id = plan_drug.rxnorm_code write_resource(FormularyDrugFactory.new(plan_drug).build(plan_drug.rxnorm_code)) add_to_list(id) write_resource(FormularyItemFactory.new(plan, plan_drug).build) end |
#generate ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/formulary/drug_list_generator.rb', line 22 def generate plan_drugs.each do |drug| create_drug(drug) increment_id end return if list.empty? write_payer_insurance_plan write_formulary end |
#id_prefix ⇒ Object
33 34 35 |
# File 'lib/formulary/drug_list_generator.rb', line 33 def id_prefix @id_prefix ||= ::Digest::SHA1.hexdigest(plan.id)[0..5] end |
#increment_id ⇒ Object (private)
47 48 49 |
# File 'lib/formulary/drug_list_generator.rb', line 47 def increment_id @count += 1 end |
#list ⇒ Object (private)
51 52 53 |
# File 'lib/formulary/drug_list_generator.rb', line 51 def list @list ||= [] end |
#plan_drugs ⇒ Object (private)
55 56 57 58 59 60 61 |
# File 'lib/formulary/drug_list_generator.rb', line 55 def plan_drugs @plan_drugs ||= QHPDrugRepo .drugs_for_plan(plan) .uniq(&:rxnorm_code) .take(Config.max_drugs_per_plan) end |
#write_formulary ⇒ Object (private)
78 79 80 |
# File 'lib/formulary/drug_list_generator.rb', line 78 def write_formulary write_resource(FormularyFactory.new(plan).build) end |
#write_payer_insurance_plan ⇒ Object (private)
74 75 76 |
# File 'lib/formulary/drug_list_generator.rb', line 74 def write_payer_insurance_plan write_resource(PayerInsurancePlanFactory.new(plan).build) end |
#write_resource(resource) ⇒ Object (private)
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/formulary/drug_list_generator.rb', line 82 def write_resource(resource) output_directory = File.join(base_output_directory, resource.resourceType) FileUtils.mkdir_p output_directory file_path = File.join(output_directory, "#{resource.id}.#{resource.resourceType}.json") File.open(file_path, "w") do |file| file.write(resource.to_json) end end |