Last active
February 13, 2024 16:11
-
-
Save alexdean/1869b6e5584179f295008612689abcfb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# in this test, i want to define a custom N1 segment which adds an extra element to the standard N1. | |
# | |
# it seems that the code somehow continues to reference the built-in N1 definition. | |
# i see errors saying that "N1 segment has only 6 elements" (the original number of elements in N1) | |
# even when my custom N1 defines 7 elements. | |
# | |
# see lines 103-111 below for more details on the error i see. | |
require "bundler/inline" | |
gemfile(true) do | |
source "https://rubygems.org" | |
gem "stupidedi", "1.4.0" | |
end | |
require "stupidedi" | |
module MyCompany | |
module Partner1 | |
module SegmentDefs | |
d = Stupidedi::Schema | |
e = Stupidedi::Versions::FortyTen::ElementDefs | |
r = Stupidedi::Versions::FortyTen::ElementReqs | |
n = Stupidedi::Versions::FortyTen::SyntaxNotes | |
N1 = d::SegmentDef.build(:N1, "Name", | |
"To identify a party by type of organization, name, and code", | |
e::E98 .simple_use(r::Mandatory, d::RepeatCount.bounded(1)), | |
e::E93 .simple_use(r::Optional, d::RepeatCount.bounded(1)), | |
e::E66 .simple_use(r::Mandatory, d::RepeatCount.bounded(1)), | |
e::E67 .simple_use(r::Optional, d::RepeatCount.bounded(1)), | |
e::E706 .simple_use(r::Optional, d::RepeatCount.bounded(1)), | |
e::E98 .simple_use(r::Optional, d::RepeatCount.bounded(1)), | |
e::E98 .simple_use(r::Optional, d::RepeatCount.bounded(1)) | |
# standard N1 has 6 elements, but mine has 7. | |
) | |
end | |
module Implementations | |
b = Stupidedi::TransactionSets::Builder | |
d = Stupidedi::Schema | |
r = Stupidedi::TransactionSets::FortyTen::Implementations::SegmentReqs | |
e = Stupidedi::TransactionSets::FortyTen::Implementations::ElementReqs | |
s = Stupidedi::Versions::FortyTen::SegmentDefs | |
sc = MyCompany::Partner1::SegmentDefs | |
# based on Stupidedi::TransactionSets::FortyTen::Implementations::IM210 | |
IM210 = Stupidedi::TransactionSets::Builder.build("IM", "210", "Motor Carrier Freight Details and Invoice", | |
d::TableDef.header("Heading", | |
b::Segment(10, s::ST, "Transaction Set Header", r::Required, d::RepeatCount.bounded(1), | |
b::Element(e::Required, "Transaction Set Identifier Code", b::Values("210")), | |
b::Element(e::Required, "Transaction Set Control Number")), | |
d::LoopDef.build("0100", d::RepeatCount.bounded(10), | |
# using our custom N1 here rather than the one provided by stupidedi. | |
b::Segment(110, sc::N1, "Name", r::Situational, d::RepeatCount.bounded(1), | |
b::Element(e::Required, "Entity Identifier Code"), | |
b::Element(e::Required, "Name"), | |
b::Element(e::Situational, "Identification Code Qualifier"), | |
b::Element(e::Situational, "Identification Code"), | |
b::Element(e::Situational, "Entity Relation Code"), | |
b::Element(e::Situational, "Entity Identifier Code"), | |
b::Element(e::Situational, "Additional Test Identifier Code") | |
) | |
) | |
), | |
d::TableDef.summary("Summary", | |
b::Segment(20, s::SE, "Transaction Set Trailer", r::Required, d::RepeatCount.bounded(1), | |
b::Element(e::Situational, "Number of Included Segments"), | |
b::Element(e::Situational, "Transaction Set Control Number") | |
) | |
) | |
) | |
end | |
end | |
end | |
config = Stupidedi::Config.new | |
config.interchange.register('00401') do | |
Stupidedi::Interchanges::FourOhOne::InterchangeDef | |
end | |
config.functional_group.register("004010") do | |
Stupidedi::Versions::FortyTen::FunctionalGroupDef | |
end | |
config.transaction_set.register("004010", "IM", "210") do | |
MyCompany::Partner1::Implementations::IM210 | |
end | |
interchange_control_number = rand(10_000) | |
group_control_number = rand(10_000) | |
b = Stupidedi::Parser::BuilderDsl.build(config) | |
b.ISA('00', '', '00', '', '02', 'SENDER_ID', 'ZZ', 'RECEIVER_ID', '201224', '1200', 'U', '00401', '1234', '0', 'T', '>') | |
b.GS('IM', 'SENDER_ID', 'RECEIVER_ID', '20201224', '1200', '5678', 'X', '004010') | |
b.ST('210', '0001') | |
# my custom N1 definition has 7 elements | |
# but I get an error when trying to specify the 7th one. | |
# | |
# Traceback (most recent call last): | |
# 3: from edi_builder_errors_when_adding_elements.rb:111:in `<main>' | |
# 2: from /Library/Ruby/Gems/2.6.0/gems/stupidedi-1.4.0/lib/stupidedi/parser/builder_dsl.rb:103:in `method_missing' | |
# 1: from /Library/Ruby/Gems/2.6.0/gems/stupidedi-1.4.0/lib/stupidedi/parser/builder_dsl.rb:41:in `segment!' | |
# /Library/Ruby/Gems/2.6.0/gems/stupidedi-1.4.0/lib/stupidedi/parser/tokenization.rb:91:in `mksegment_tok': N1 segment has only 6 elements (ArgumentError) | |
b.N1('BT', 'BILL TO', '1', 'DUNS', '5', '6', '7') | |
b.SE('10', '0001') | |
b.GE('1', '5678') | |
b.IEA('1', '1234') | |
separators = { | |
segment: "~\n", | |
element: "|", | |
component: ">", | |
repetition: "^", | |
} | |
b.machine.zipper.tap do |z| | |
w = Stupidedi::Writer::Default.new(z.root, Stupidedi::Reader::Separators.build(separators)) | |
puts w.write | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Were you able to resolve this issue?