Class: Eddy::Build::TransactionSetBuilder

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/eddy/build/transaction_set_builder.rb

Overview

Generate Ruby code from JSON/YAML EDI definitions.

Instance Attribute Summary collapse

Initialize collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(summary, folder: nil) ⇒ void

Parameters:



16
17
18
19
# File 'lib/eddy/build/transaction_set_builder.rb', line 16

def initialize(summary, folder: nil)
  self.summary = summary
  @folder = folder
end

Instance Attribute Details

#summaryEddy::Summary::TransactionSet



11
12
13
# File 'lib/eddy/build/transaction_set_builder.rb', line 11

def summary
  @summary
end

Class Method Details

.from_file(path, **kwargs) ⇒ Eddy::Build::TransactionSetBuilder

Parameters:

  • path (String)

    Path to a JSON or YAML file containing a valid Transaction Set definition.

  • summary (Eddy::Summary::TransactionSet)
  • folder (String)

    (nil)

Returns:

Raises:



32
33
34
35
36
# 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) ⇒ String

Parameters:

Returns:

  • (String)


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# 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

.segment_accessor(segment_id) ⇒ String

Parameters:

  • segment_id (String)

Returns:

  • (String)


148
149
150
151
152
153
154
155
156
157
158
159
160
161
# 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

Instance Method Details

#accessorsString

Returns:

  • (String)


135
136
137
138
139
140
141
142
143
144
# 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

#buildString

Returns:

  • (String)


47
48
49
50
# 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_loopsString

Returns:

  • (String)


58
59
60
61
62
63
64
65
# 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.normalized_name}.rb"), "a") do |f|
      f.write(Eddy::Build::Loop.render(looop, self.normalized_name) + "\n")
    end
  end
end

#constructorString

Returns:

  • (String)


91
92
93
94
95
96
97
98
99
100
101
102
# 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

#declarationsString

Returns:

  • (String)


105
106
107
108
109
110
111
112
113
114
115
# 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

#folderString

Returns:

  • (String)


39
40
41
42
43
44
# 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_classGinny::Class

Returns:

  • (Ginny::Class)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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

#renderString

Returns:

  • (String)


53
54
55
# File 'lib/eddy/build/transaction_set_builder.rb', line 53

def render()
  return self.ginny_class.render()
end

#super_callString

Returns:

  • (String)


118
119
120
121
122
123
124
125
126
127
128
129
130
# 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