class Eddy::Build::TransactionSetBuilder

Generate Ruby code from JSON/YAML EDI definitions.

Attributes

summary[RW]

@return [Eddy::Summary::TransactionSet]

Public Class Methods

from_file(path, **kwargs) click to toggle source

@param path [String] Path to a JSON or YAML file containing a valid Transaction Set definition. @param (see initialize) @return [Eddy::Build::TransactionSetBuilder]

# File lib/eddy/build/transaction_set_builder.rb, line 32
def self.from_file(path, **kwargs)
  raise Eddy::Errors::Error, "Invalid transaction set definition" unless Eddy::Summary.valid_transaction_set_data?(path)
  data = Eddy::Util.read_json_or_yaml(path)
  return new(Eddy::Summary::TransactionSet.create(data), **kwargs)
end
loop_accessor(summary, t_set_id) click to toggle source

@param summary [Eddy::Summary::Loop] @param t_set_id [String] @return [String]

# File lib/eddy/build/transaction_set_builder.rb, line 166
      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}(&block)
            if block_given?
              @#{summary.var_name}.repeat(&block)
            else
              raise Eddy::Errors::Error, \"No block given in loop iteration\"
            end
            return nil
          end
        RB
      end
new(summary, folder: nil) click to toggle source

@param summary [Eddy::Summary::TransactionSet] @param folder [String] (nil) @return [void]

# File lib/eddy/build/transaction_set_builder.rb, line 16
def initialize(summary, folder: nil)
  self.summary = summary
  @folder = folder
end
segment_accessor(segment_id) click to toggle source

@param segment_id [String] @return [String]

# File lib/eddy/build/transaction_set_builder.rb, line 148
      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}]
          # @return [Eddy::Segments::#{upper}]
          def #{upper}()
            yield(@#{lower}) if block_given?
            return @#{lower}
          end
        RB
      end

Public Instance Methods

accessors() click to toggle source

@return [String]

# File lib/eddy/build/transaction_set_builder.rb, line 135
def accessors()
  defs = self.summary.components.map do |comp|
    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)
    end
  end
  return defs.join("\n\n")
end
build() click to toggle source

@return [String]

# File lib/eddy/build/transaction_set_builder.rb, line 47
def build()
  self.build_loops()
  return self.ginny_class.generate(self.folder, file: "#{self.id}.rb")
end
build_loops() click to toggle source

@return [String]

# File lib/eddy/build/transaction_set_builder.rb, line 58
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.class_name}.rb"), "a") do |f|
      f.write(Eddy::Build::Loop.render(looop, self.normalized_name) + "\n")
    end
  end
end
constructor() click to toggle source

@return [String]

# File lib/eddy/build/transaction_set_builder.rb, line 91
      def constructor()
        return Ginny::Func.create({
          name: "initialize",
          params: [{ name: "store", type: "Eddy::Data::Store" }],
          body: <<~RB,
            #{self.declarations()}


            #{self.super_call()}
          RB
        }).render()
      end
declarations() click to toggle source

@return [String]

# File lib/eddy/build/transaction_set_builder.rb, line 105
def declarations()
  decs = ""
  self.summary.components.each do |comp|
    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"
    end
  end
  return decs
end
folder() click to toggle source

@return [String]

# File lib/eddy/build/transaction_set_builder.rb, line 39
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
ginny_class() click to toggle source

@return [Ginny::Class]

# File lib/eddy/build/transaction_set_builder.rb, line 68
      def ginny_class()
        return Ginny::Class.create({
          classify_name: false,
          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.id}".freeze
            NAME = "#{self.name}".freeze
            FUNCTIONAL_GROUP = "#{self.functional_group}".freeze

            #{self.constructor()}

            #{self.accessors()}
          STR
        })
      end
render() click to toggle source

@return [String]

# File lib/eddy/build/transaction_set_builder.rb, line 53
def render()
  return self.ginny_class.render()
end
super_call() click to toggle source

@return [String]

# File lib/eddy/build/transaction_set_builder.rb, line 118
def super_call()
  super_call = "super(\n"
  super_call << "  store,\n"
  self.summary.components.each do |comp|
    if comp.is_a?(Eddy::Summary::Loop) && comp.repeat_limit > 1
      super_call << "  @#{comp.var_name},\n"
    else
      super_call << "  @#{comp.id.downcase},\n"
    end
  end
  super_call << ")"
  return super_call
end