module Eddy::Build::Elements

Public Class Methods

build_code_list(id) click to toggle source

@param id [String] @return [String]

# File lib/eddy/build/elements/id.rb, line 28
def self.build_code_list(id)
  file = File.join(Eddy::Util.data_dir, "code-lists", "#{id}.tsv")
  begin
    data = Eddy::Util.parse_tsv(file)
  rescue Errno::ENOENT
    puts("Missing code-list for element #{id}")
    return nil
  rescue CSV::MalformedCSVError => e
    puts("Error reading csv file '#{file}':#{e}")
    return nil
  end
  # body = "return [\n" + data.map { |c| %("#{c[:id]}").indent(2) }.join(",\n") + "\n]\n"
  body = ""
  body << "return [\n"
  data.each do |c|
    body << %("#{c[:id]}", # #{c[:definition]}\n).indent(2)
  end
  body << "]\n"
  return Ginny::Func.create({
    name: "code_list",
    return_type: "Array<String>",
    body: body,
  }).render()
end
default_constructor(el) click to toggle source

@param el [Eddy::Summary::Element] @return [String]

# File lib/eddy/build/elements/element.rb, line 19
      def self.default_constructor(el)
        return Ginny::Func.create({
          name: "initialize",
          params: self.element_params(el),
          body: <<~RB,
            @id = "#{el.id}"
            @name = "#{el.name}"
            @description = "#{el.description}"
            super(
              min: #{el.min},
              max: #{el.max},
              req: req,
              ref: ref,
              val: val,
            )
          RB
        }).render()
      end
determine_decimals(type) click to toggle source

@param type [String] @return [Integer]

# File lib/eddy/build/elements/n.rb, line 40
def self.determine_decimals(type)
  match = type.match(/(?<=N)(?<decimal>\d)/)
  return 0 if match.nil?
  return match[:decimal].to_i
end
element(el, test: false) click to toggle source

@param el [Eddy::Summary::Element] @param test [Boolean] (false) When true, returns output as a string instead of writing to a file. @return [void]

# File lib/eddy/build/elements/element.rb, line 55
def self.element(el, test: false)
  c = self.ginny_class(el, self.default_constructor(el))
  return c.render if test
  c.generate(
    File.join(Eddy::Util.root_dir, "build", "elements"),
    file: "#{el.id}.#{Eddy::Util.snake_case(el.name)}.rb",
  )
  return nil
end
element_params(el) click to toggle source

@param el [Eddy::Summary::Element] @return [Array<Hash>]

# File lib/eddy/build/elements/element.rb, line 9
def self.element_params(el)
  return [
    { name: "val", type: el.yard_type, optional: true, keyword: true },
    { name: "req", type: "String",     optional: true, keyword: true },
    { name: "ref", type: "String",     optional: true, keyword: true },
  ]
end
generate_element_data() click to toggle source

Generate usable data from `data/elements.tsv`.

@return [Array<Eddy::Summary::Element>]

# File lib/eddy/build/elements/elements.rb, line 35
def self.generate_element_data()
  data = Eddy::Util.raw_element_data()
  elements = data.map do |el|
    next if el[:type].nil? || el[:description].nil?
    Eddy::Summary::Element.create(el)
  end
  return elements.compact
end
generate_elements(elements = self.generate_element_data()) click to toggle source

Generate `Eddy::Element` classes for all data elements defined in `data/elements.tsv`

@param elements [Array<Eddy::Summary::Element>] @return [void]

# File lib/eddy/build/elements/elements.rb, line 14
def self.generate_elements(elements = self.generate_element_data())
  existing = (Eddy::Util.list_element_classes() + Eddy::Util.list_built_elements()).uniq()
  elements.each do |el|
    next if existing.include?(el.id)
    existing.append(el.id)
    case el.edi_type()
    when "B"  then next
    when "ID" then self.id(el)
    when "N"  then self.n(el)
    when "AN" then self.element(el)
    when "DT" then self.element(el)
    when "R"  then self.element(el)
    when "TM" then self.element(el)
    end
  end
  return nil
end
ginny_class(el, body) click to toggle source

@param el [Eddy::Summary::Element] @param body [String] @return [Ginny::Class]

# File lib/eddy/build/elements/element.rb, line 41
def self.ginny_class(el, body)
  return Ginny::Class.create({
    classify_name: false,
    name:          Eddy::Util.normalize_id(el.id),
    description:   el.doc_comment,
    parent:        "Eddy::Models::Element::#{el.edi_type}",
    modules:       ["Eddy", "Elements"],
    body:          body,
  })
end
id(el, test: false) click to toggle source

@param el [Eddy::Summary::Element] @param test [Boolean] (false) When true, returns output as a string instead of writing to a file. @return [void]

# File lib/eddy/build/elements/id.rb, line 10
def self.id(el, test: false)
  code_list = self.build_code_list(el.id)
  return nil if code_list.nil?
  c = self.ginny_class(
    el,
    ("\n" + self.default_constructor(el) + "\n\n" + code_list + "\n"),
    # "\n#{self.default_constructor(el)}\n\n#{code_list}\n",
  )
  return c.render if test
  c.generate(
    File.join(Eddy::Util.root_dir, "build", "elements"),
    file: "#{el.id}.#{Eddy::Util.snake_case(el.name)}.rb",
  )
  return nil
end
n(el, test: false) click to toggle source

@param el [Eddy::Summary::Element] @param test [Boolean] (false) When true, returns output as a string instead of writing to a file. @return [void]

# File lib/eddy/build/elements/n.rb, line 10
      def self.n(el, test: false)
        decimal_points = self.determine_decimals(el.type)
        constructor = Ginny::Func.create({
          name:   "initialize",
          params: self.element_params(el),
          body:   <<~RB,
            @id = "#{el.id}"
            @name = "#{el.name}"
            @description = "#{el.description}"
            super(
              min: #{el.min},
              max: #{el.max},
              req: req,
              ref: ref,
              val: val,
              decimals: #{decimal_points},
            )
          RB
        }).render()
        c = self.ginny_class(el, constructor)
        return c.render if test
        c.generate(
          File.join(Eddy::Util.root_dir, "build", "elements"),
          file: "#{el.id}.#{Eddy::Util.snake_case(el.name)}.rb",
        )
        return nil
      end