lib/eddy/build/transaction_set_builder.rb in eddy-0.5.1 vs lib/eddy/build/transaction_set_builder.rb in eddy-0.6.0
- old
+ new
@@ -1,84 +1,84 @@
require "ginny"
+require "forwardable"
module Eddy
module Build
# Generate Ruby code from JSON/YAML EDI definitions.
- #
- # - Validate definition file
- # - Create Summary
- # - For Segments, check that we have a default definition for each.
- # - Generate segments?
- # - For Elements, check that we have a default definition for each.
- # - Apply any input over default element attributes.
class TransactionSetBuilder
+ extend Forwardable
- # @return [Eddy::Schema::TransactionSetSummary]
+ # @return [Eddy::Summary::TransactionSet]
attr_accessor :summary
- # @return [String] (nil)
- attr_accessor :folder
- # @return [Boolean] (false)
- attr_accessor :headers
+ # @param summary [Eddy::Summary::TransactionSet]
# @param folder [String] (nil)
- # @param headers [Boolean] (false)
# @return [void]
- def initialize(folder: nil, headers: false)
- self.folder = folder
- self.headers = headers
+ def initialize(summary, folder: nil)
+ self.summary = summary
+ @folder = folder
end
+ def_delegators(
+ :@summary,
+ :id,
+ :name,
+ :normalized_name,
+ :functional_group,
+ )
+
# @param path [String] Path to a JSON or YAML file containing a valid Transaction Set definition.
# @param (see #initialize)
# @return [Eddy::Build::TransactionSetBuilder]
def self.from_file(path, **kwargs)
- raise Eddy::Errors::Error, "Invalid transaction set definition" unless Eddy::Schema.valid_transaction_set_data?(path)
+ raise Eddy::Errors::Error, "Invalid transaction set definition" unless Eddy::Summary.valid_transaction_set_data?(path)
data = Eddy::Util.read_json_or_yaml(path)
- builder = Eddy::Build::TransactionSetBuilder.new(**kwargs)
- builder.summary = Eddy::Schema::TransactionSetSummary.create(data)
- return builder
+ return new(Eddy::Summary::TransactionSet.create(data), **kwargs)
end
- # @param summary [Eddy::Schema::TransactionSetSummary]
- # @param (see #initialize)
- # @return [Eddy::Build::TransactionSetBuilder]
- def self.from_summary(summary, **kwargs)
- builder = Eddy::Build::TransactionSetBuilder.new(**kwargs)
- builder.summary = summary
- return builder
+ # @return [String]
+ def folder()
+ root_path = @folder || File.join(Eddy::Util.root_dir, "build", "transaction_sets")
+ path = File.join(root_path, self.id.to_s)
+ FileUtils.mkdir_p(path)
+ return path
end
# @return [String]
def build()
- root_path = self.folder || File.join(Eddy::Util.root_dir, "build", "transaction_sets")
- path = File.join(root_path, self.summary.id.to_s)
- FileUtils.mkdir_p(path)
- File.open(File.join(path, "loops.rb"), "a") { |f| f.write(self.render_loops) }
- return self.ginny_class.generate(path)
+ self.build_loops()
+ return self.ginny_class.generate(self.folder, file: "#{self.id}.rb")
end
# @return [String]
- def render_loops()
- loops = self.summary.unique_loops.map do |l|
- Eddy::Build::LoopBuilder.from_summary(l, t_set_id: self.summary.normalized_name).ginny_class.render()
- end.join("\n\n")
- return Ginny.mod(("\n" + loops + "\n"), "Eddy", "TransactionSets", self.summary.normalized_name, "Loops")
+ def render()
+ return self.ginny_class.render()
end
+ # @return [String]
+ def build_loops()
+ FileUtils.mkdir_p(File.join(self.folder, "loops"))
+ self.summary.unique_loops.each do |looop|
+ File.open(File.join(self.folder, "loops", "#{looop.normalized_name}.rb"), "a") do |f|
+ f.write(Eddy::Build::Loop.render(looop, self.normalized_name) + "\n")
+ end
+ end
+ end
+
# @return [Ginny::Class]
def ginny_class()
return Ginny::Class.create({
classify_name: false,
- modules: ["Eddy", "TransactionSets", self.summary.normalized_name],
- parent: "Eddy::TransactionSet",
- name: self.summary.normalized_name,
+ modules: ["Eddy", "TransactionSets", self.normalized_name],
+ parent: "Eddy::Models::TransactionSet",
+ name: self.normalized_name,
description: self.summary.doc_comment(header: true),
body: <<~STR,
- ID = "#{self.summary.id}".freeze
- NAME = "#{self.summary.name}".freeze
- FUNCTIONAL_GROUP = "#{self.summary.functional_group}".freeze
+ ID = "#{self.id}".freeze
+ NAME = "#{self.name}".freeze
+ FUNCTIONAL_GROUP = "#{self.functional_group}".freeze
#{self.constructor()}
#{self.accessors()}
STR
@@ -103,38 +103,28 @@
# @return [String]
def declarations()
decs = ""
self.summary.components.each do |comp|
- case comp
- when Eddy::Schema::SegmentSummary
+ if comp.is_a?(Eddy::Summary::Loop) && comp.repeat_limit > 1
+ decs << "@#{comp.var_name} = Eddy::TransactionSets::#{self.normalized_name}::Loops::#{comp.id.upcase}::Base.new(store)\n"
+ else
decs << "@#{comp.id.downcase} = Eddy::Segments::#{comp.id.upcase}.new(store)\n"
- when Eddy::Schema::LoopSummary
- if comp.repeat == 1
- decs << "@#{comp.id.downcase} = Eddy::Segments::#{comp.id.upcase}.new(store)\n"
- else
- decs << "@l_#{comp.id.downcase} = Eddy::TransactionSets::#{self.summary.normalized_name}::Loops::#{comp.id.upcase}.new(store)\n"
- end
end
end
return decs
end
# @return [String]
def super_call()
super_call = "super(\n"
super_call << " store,\n"
self.summary.components.each do |comp|
- case comp
- when Eddy::Schema::SegmentSummary
+ if comp.is_a?(Eddy::Summary::Loop) && comp.repeat_limit > 1
+ super_call << " @#{comp.var_name},\n"
+ else
super_call << " @#{comp.id.downcase},\n"
- when Eddy::Schema::LoopSummary
- if comp.repeat == 1
- super_call << " @#{comp.id.downcase},\n"
- else
- super_call << " @l_#{comp.id.downcase},\n"
- end
end
end
super_call << ")"
return super_call
end
@@ -142,19 +132,14 @@
# @!endgroup Initialize
# @return [String]
def accessors()
defs = self.summary.components.map do |comp|
- case comp
- when Eddy::Schema::SegmentSummary
+ if comp.is_a?(Eddy::Summary::Loop) && comp.repeat_limit > 1
+ Eddy::Build::TransactionSetBuilder.loop_accessor(comp, self.normalized_name)
+ else
Eddy::Build::TransactionSetBuilder.segment_accessor(comp.id)
- when Eddy::Schema::LoopSummary
- if comp.repeat == 1
- Eddy::Build::TransactionSetBuilder.segment_accessor(comp.id)
- else
- Eddy::Build::TransactionSetBuilder.loop_accessor(comp, self.summary.normalized_name)
- end
end
end
return defs.join("\n\n")
end
@@ -164,54 +149,36 @@
upper = segment_id.upcase
lower = segment_id.downcase
return <<~RB.strip
# (see Eddy::Segments::#{upper})
#
- # @yieldparam [Eddy::Segments::#{upper}] #{lower}
+ # @yieldparam [Eddy::Segments::#{upper}]
# @return [Eddy::Segments::#{upper}]
def #{upper}()
yield(@#{lower}) if block_given?
return @#{lower}
end
RB
end
- # @param summary [Eddy::Schema::LoopSummary]
- # @param t_set_name [String]
+ # @param summary [Eddy::Summary::Loop]
+ # @param t_set_id [String]
# @return [String]
- def self.loop_accessor(summary, t_set_name)
- upper = summary.id.upcase
- lower = summary.id.downcase
+ def self.loop_accessor(summary, t_set_id)
return <<~RB.strip
- # (see Eddy::TransactionSets::#{t_set_name}::Loops::#{upper})
+ # (see Eddy::TransactionSets::#{t_set_id}::Loops::#{summary.id.upcase}::Base)
#
- #{self.loop_components(summary, t_set_name)}
+ # @yieldparam [Eddy::TransactionSets::#{t_set_id}::Loops::#{summary.id.upcase}::Repeat]
# @return [void]
- def L_#{upper}(&block)
+ def #{summary.var_name.upcase}(&block)
if block_given?
- @l_#{lower}.add_iteration(&block)
+ @#{summary.var_name}.repeat(&block)
else
raise Eddy::Errors::Error, \"No block given in loop iteration\"
end
return nil
end
RB
- end
-
- # @param summary [Eddy::Schema::LoopSummary]
- # @param t_set_name [String]
- # @return [String]
- def self.loop_components(summary, t_set_name)
- comps = []
- summary.components.each do |comp|
- case comp
- when Eddy::Schema::SegmentSummary
- comps << "# @yieldparam [Eddy::Segments::#{comp.id.upcase}] #{comp.id.downcase}"
- when Eddy::Schema::LoopSummary
- comps << "# @yieldparam [Eddy::TransactionSets::#{t_set_name}::Loops::#{comp.id.upcase}] l_#{comp.id.downcase}"
- end
- end
- return comps.join("\n")
end
end
end
end