Sha256: 9514211331acc2325e0ea54d10dbda3620504589b8a1bd4909b8c197c1e1d540

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

require "food_truck"

module Eddy
  module Build

    # @param e [Hash]
    # @return [void]
    def self.build_id(e)
      code_list = self.build_code_list(e[:id])
      return nil if code_list.nil?
      constructor = FoodTruck::Func.create({
        name: "initialize",
        body: <<~FUNC_BODY,
          @id = "#{e[:id]}"
          @name = "#{e[:name]}"
          @type = "ID"
          self.min = #{e[:min]}
          self.max = #{e[:max]}
        FUNC_BODY
      }).render()
      data = {
        name: e[:name],
        description: e[:description],
        parent: "Eddy::Element::ID",
        modules: ["Eddy", "Elements"],
        body: constructor + "\n\n" + code_list + "\n",
        file_prefix: "#{e[:id]}.",
      }
      c = FoodTruck::Class.create(data)
      c.generate(File.join(Eddy.root_dir, "build", "elements", "id"))
      return nil
    end

    # @param id [String]
    # @return [String]
    def self.build_code_list(id)
      file = File.join(Eddy.data_dir, "004010", "code-lists", "#{id}.tsv")
      begin
        data = Eddy.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"
      return FoodTruck::Func.create({
        name: "code_list",
        return_type: "Array<String>",
        body: body,
      }).render()
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
eddy-0.1.0 lib/eddy/build/build_id.rb