Skip to content

Commit

Permalink
Check in new loop implementation before regenerating transaction sets
Browse files Browse the repository at this point in the history
  • Loading branch information
tcd committed Jan 7, 2020
1 parent c4f6527 commit cc5f820
Show file tree
Hide file tree
Showing 23 changed files with 988 additions and 707 deletions.
4 changes: 3 additions & 1 deletion lib/eddy/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

require "eddy/build/elements/elements"
require "eddy/build/segment_builder"
require "eddy/build/loop_builder"
require "eddy/build/loop/base"
require "eddy/build/loop/repeat"
require "eddy/build/loop/render"
require "eddy/build/transaction_set_builder"

module Eddy
Expand Down
61 changes: 61 additions & 0 deletions lib/eddy/build/loop/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require "ginny"

module Eddy
module Build
module Loop
# Generate Ruby code from JSON/YAML EDI definitions.
class Base

# @return [Eddy::Summary::Loop]
attr_accessor :summary
# @return [String] (nil)
attr_accessor :folder
# Namespace the Loop is within.
# @return [String]
attr_accessor :t_set_id

# @param summary [Eddy::Summary::Loop]
# @param t_set_id [String]
# @param folder [String] (nil)
# @return [void]
def initialize(summary, t_set_id, folder: nil)
self.summary = summary
self.t_set_id = t_set_id
self.folder = folder
end

# @return [String]
def render()
return self.ginny_class.render()
end

# @return [Ginny::Class]
def ginny_class()
return Ginny::Class.create({
classify_name: false,
parent: "Eddy::Models::Loop::Base",
name: "Base",
description: summary.doc_comment(header: true),
body: <<~STR.strip,
# @param store [Eddy::Data::Store]
# @return [void]
def initialize(store)
@repeat_limit = #{self.summary.repeat_limit}
super(store, Repeat)
end
# Add a repeat of loop #{self.summary.id}.
#
# @yieldparam [Eddy::TransactionSets::#{self.t_set_id}::Loops::#{self.summary.id}::Repeat] rep
# @return [void]
def repeat(&block)
super(&block)
end
STR
})
end

end
end
end
end
18 changes: 18 additions & 0 deletions lib/eddy/build/loop/render.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require "ginny"

module Eddy
module Build
module Loop
# Generate Ruby code for an EDI loop.
#
# @param summary [Eddy::Summary::Loop]
# @param t_set_id [String]
# @return [String]
def self.render(summary, t_set_id)
base = Eddy::Build::Loop::Base.new(summary, t_set_id).render()
repeat = Eddy::Build::Loop::Repeat.new(summary, t_set_id).render()
return Ginny.mod(("\n" + base + "\n\n" + repeat + "\n"), "Eddy", "TransactionSets", t_set_id, "Loops", summary.id)
end
end
end
end
142 changes: 142 additions & 0 deletions lib/eddy/build/loop/repeat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
require "ginny"

module Eddy
module Build
module Loop
# Generate Ruby code from JSON/YAML EDI definitions.
class Repeat

# @return [Eddy::Summary::Loop]
attr_accessor :summary
# Namespace the Loop is within.
# @return [String]
attr_accessor :t_set_id

# @param summary [Eddy::Summary::Loop]
# @param t_set_id [String]
# @return [void]
def initialize(summary, t_set_id)
self.summary = summary
self.t_set_id = t_set_id
end

# @return [String]
def render()
return self.ginny_class.render()
end

# @return [Ginny::Class]
def ginny_class()
return Ginny::Class.create({
classify_name: false,
parent: "Eddy::Models::Loop::Repeat",
name: "Repeat",
description: "(see Eddy::TransactionSets::#{t_set_id}::Loops::#{self.summary.id}::Base)",
body: <<~STR.strip,
# @param store [Eddy::Data::Store]
# @return [void]
def initialize(store)
#{self.declarations()}
super(
store,
#{self.super_call()}
)
end
#{self.accessors()}
STR
})
end

# @return [String]
def repeat_method()
params = self.summary.components.map do |comp|
case comp
when Eddy::Summary::Segment then "# @yieldparam [Eddy::Segments::#{comp.id.upcase}] #{comp.id.downcase}"
when Eddy::Summary::Loop then "# @yieldparam [Eddy::TransactionSets::#{t_set_id}::Loops::#{comp.id.upcase}::Base] #{comp.var_name}"
end
end.compact.join("\n")
return <<~RB
# Add a repeat of loop #{self.summary.id}.
#
#{params}
# @return [void]
def repeat(&block)
super(&block)
end
RB
end

# @return [String]
def super_call()
return self.summary.components.map do |comp|
case comp
when Eddy::Summary::Segment then " @#{comp.id.downcase},"
when Eddy::Summary::Loop then " @#{comp.var_name},"
end
end.compact.join("\n ")
end

# @return [String]
def declarations()
self.summary.components.map do |comp|
case comp
when Eddy::Summary::Segment then " @#{comp.id.downcase} = Eddy::Segments::#{comp.id.upcase}.new(store)"
when Eddy::Summary::Loop then " @#{comp.var_name} = Eddy::TransactionSets::#{t_set_id}::Loops::#{comp.id.upcase}::Base.new(store)"
end
end.compact.join("\n")
end

# @return [String]
def accessors()
defs = self.summary.components.map do |comp|
if comp.is_a?(Eddy::Summary::Loop) && comp.repeat_limit > 1
self.class.loop_accessor(comp, self.t_set_id)
else
self.class.segment_accessor(comp.id)
end
end
return defs.join("\n\n")
end

# @param segment_id [String]
# @return [String]
def self.segment_accessor(segment_id)
upper = segment_id.upcase
lower = segment_id.downcase
return <<~RB.strip
# (see Eddy::Segments::#{upper})
#
# @yieldparam [Eddy::Segments::#{upper}] #{lower}
# @return [Eddy::Segments::#{upper}]
def #{upper}()
yield(@#{lower}) if block_given?
return @#{lower}
end
RB
end

# @param summary [Eddy::Summary::Loop]
# @param t_set_id [String]
# @return [String]
def self.loop_accessor(summary, t_set_id)
return <<~RB.strip
# (see Eddy::TransactionSets::#{t_set_id}::Loops::#{summary.id.upcase}::Base)
#
# @yieldparam [Eddy::TransactionSets::#{t_set_id}::Loops::#{summary.id.upcase}::Repeat]
# @return [void]
def #{summary.var_name.upcase}()
if block_given?
@#{summary.var_name}.repeat()
else
raise Eddy::Errors::Error, "No block given in loop iteration"
end
return nil
end
RB
end

end
end
end
end
93 changes: 0 additions & 93 deletions lib/eddy/build/loop_builder.rb

This file was deleted.

7 changes: 6 additions & 1 deletion lib/eddy/build/segment_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def self.from_file(path, **kwargs)
# @param (see #initialize)
# @return [Eddy::Build::SegmentBuilder]
def self.from_summary(summary, **kwargs)
sb = Eddy::Build::SegmentBuilder.new(**kwargs)
sb = new(**kwargs)
sb.summary = summary
return sb
end
Expand All @@ -54,6 +54,11 @@ def build(build_elements: false)
return result
end

# @return [String]
def render()
return self.ginny_class.render()
end

# @return [Ginny::Class]
def ginny_class()
return Ginny::Class.create({
Expand Down
Loading

0 comments on commit cc5f820

Please sign in to comment.