diff --git a/lib/simple_hl7/message.rb b/lib/simple_hl7/message.rb index fdcce00..62a3cda 100644 --- a/lib/simple_hl7/message.rb +++ b/lib/simple_hl7/message.rb @@ -19,7 +19,7 @@ def method_missing(meth, *args, &block) if meth.to_s =~ /^[a-zA-Z][a-zA-Z0-9]{2}$/ get_named_segment(meth) elsif meth.to_s =~ /^[a-zA-Z][a-zA-Z0-9]{2}_all$/ - seg_name = meth[0..3] + seg_name = meth[0...3] all_segments(seg_name) else super @@ -83,10 +83,14 @@ def append_segment(segment) segment end + def has_segment?(name) + @segments.any?{|segment| segment.name == name.upcase} + end + private def all_segments(name) - @segments.select {|seg| seg.name == name} + @segments.select {|seg| seg.name == name.upcase} end diff --git a/lib/simple_hl7/segment.rb b/lib/simple_hl7/segment.rb index 30664f1..50b7233 100644 --- a/lib/simple_hl7/segment.rb +++ b/lib/simple_hl7/segment.rb @@ -27,5 +27,9 @@ def name def to_a super.insert(0, name) end + + def empty? + @subcomposites.keys.size == 1 + end end end diff --git a/spec/simple_hl7/message_spec.rb b/spec/simple_hl7/message_spec.rb index 3a740c6..888c0c8 100644 --- a/spec/simple_hl7/message_spec.rb +++ b/spec/simple_hl7/message_spec.rb @@ -74,9 +74,22 @@ module SimpleHL7 msg.msh[6].to_s.should == "accountid" msg.pid[5].to_s.should == "User" msg.pid[5][2].to_s.should == "Test" + msg.has_segment?("al1").should be_falsey end + end + describe "repeating segments accessed by calling method with segment name followed by '_all' e.g. al1_all for allergy segments" do + it "provides access to repeating segments" do + hl7_msg = File.read("#{File.expand_path File.dirname(__FILE__)}/sample_message_with_repeating_segments.hl7") + msg = Message.parse(hl7_msg, segment_separator: "\n") + msg.al1_all.size.should == 3 + msg.al1_all[1][3][2].to_s.match(/Nitrostat/).should be_truthy + msg.al1_all[2][3][2].to_s.match(/iodine/).should be_truthy + msg.al1[3][2].to_s.match(/nitroglycerin/).should be_truthy + end + end + describe "#parse_llp" do it "properly parses a llp string" do msg = Message.parse_llp("\x0bMSH|^~\\&||||accountid\rPID|||||User^Test~Repeat\x1c\r") diff --git a/spec/simple_hl7/sample_message_with_repeating_segments.hl7 b/spec/simple_hl7/sample_message_with_repeating_segments.hl7 new file mode 100644 index 0000000..4416709 --- /dev/null +++ b/spec/simple_hl7/sample_message_with_repeating_segments.hl7 @@ -0,0 +1,16 @@ +MSH|^~\&|HNAM|MCH|CARINGLY|CARINGLY|20211201142018||ADT^A08|Q246327519T316598917X0|P|2.3||||||8859/1 +EVN|A08|20211201142017|||1^SYSTEM^SYSTEM^^^^^^PERSONNEL PRIMARY IDENTIFIER^Personnel^^^Personnel Primary Identifier~CERNSYSTEM^SYSTEM^SYSTEM^^^^^^External Id^Personnel^^^External Identifier +PID|1|306845^^^MLKCH MRN^MRN|306845^^^MLKCH MRN^MRN~X0000100275^^^Community MRN^Community MRN|^0^^CONSUMER_MESSAGING^Messaging|ZZZTEST^EDUSACS^^^^^Current||19700319|Male||American Indian or Alaska Native|48 none^^New Baltimore^MI^48047^US^Home^^Macomb~cd@none.com^^^^^^E-mail||(999)988-8888^Home^Tel||English|Unknown||907735^^^MLKCH FIN^FIN NBR|||||||0 +PD1|||||||No, info not provided +NK1|1|TESTING^AUUNT^^^^^Current|||(586)544-4444^Home^Tel||Emergency Contact +NK1|2|TESTING^AUUNT^^^^^Current|||(586)544-4444^Home^Tel||Support Person +PV1|1|Emergency|ED^ED16^A^MLK^^Bed(s)^MLK|Emergency|||1619954633^Brown^Eric^^MD^Physician^^^NPI^Personnel^^^National Provider Identifier~FB6005450^Brown^Eric^^MD^Physician^^^DEA^Personnel^^^DOCDEA~A72839^Brown^Eric^^MD^Physician^^^Provider State License Number^Personnel^^^LICENSENBR~EGO^Brown^Eric^^MD^Physician^^^Billing Group^Personnel^^^OTHER~6050081358012^Brown^Eric^^MD^Physician^^^SureScripts Prescriber Index^Personnel^^^SureScripts Prescriber Index||2754578955^MLKCHTest^Physician Primary Care^^^^^^NPI^Personnel^^^National Provider Identifier~PHYSPRIMCARE01^MLKCHTest^Physician Primary Care^^^^^^PROVIDER_MESSAGING^Personnel^^^Messaging|Emergency Medicine||||Clinic Referral|||1619954633^Brown^Eric^^MD^Physician^^^NPI^Personnel^^^National Provider Identifier~FB6005450^Brown^Eric^^MD^Physician^^^DEA^Personnel^^^DOCDEA~A72839^Brown^Eric^^MD^Physician^^^Provider State License Number^Personnel^^^LICENSENBR~EGO^Brown^Eric^^MD^Physician^^^Billing Group^Personnel^^^OTHER~6050081358012^Brown^Eric^^MD^Physician^^^SureScripts Prescriber Index^Personnel^^^SureScripts Prescriber Index|Emergency||Self Pay|||||||||||||||||||MLK||Active|||20210706065235||||||||6914424^Pierce^Troy^^^^^Other~DCHAVIRA^Chavira^Daniel^^^^^PRIMARY_CONTACT +PV2|||^pain|||||||0|||||||||||Yes||^^589723|||||||Not Started +DG1|1|ICD10-CM|I10|Hypertension|20211201122500|Discharge|||||||||1 +GT1|1|306845^^^MLKCH MRN^MRN~100275^^^Community MRN^Community MRN~100275^^^Community MRN^Community MRN~306845^^^MLKCH MRN^MRN|ZZZTEST^EDUSACS^^^^^Current||48 none^^New Baltimore^MI^48047^US^Home^^Macomb~cd@none.com^^^^^^E-mail|(999)988-8888^Home^Tel||19700319|Male|Default Guarantor|SELF|||||Unknown||||Unknown|||||||||605846 +IN1|1|1607851^SELF PAY^^^SELF PAY|1455518|Self Pay|TBD^^^^^US^Business||(000)000-0000^Business^^^^^^0|||605846|Unknown|20210706000000|||Self|ZZZTEST^EDUSACS^^^^^Current|SELF|19700319|48 none^^New Baltimore^MI^48047^US^Home^^Macomb~cd@none.com^^^^^^E-mail|Yes||1|||||||||||||Self Pay|||||||Unknown|Male +IN2|||^Unknown||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||(999)988-8888^Home^Tel|(000)000-0000||||||||SELF +AL1|1|Drug|d00321^nitroglycerin^Multum Drug|Severe +AL1|2|Drug|d00321^Nitrostat^Multum Drug|Severe +NTE|1||pt states experiences difficulty breathing with nitrostat (doesn't tolerate NGT ointment, PO spray either) +AL1|3|Drug|402^contrast media (iodine-based)^Multum Allergy Categgit@github.com:daya/simple_hl7.gitory|Moderate|Itching