Module: Eddy::Build::Elements

Defined in:
lib/eddy/build/elements/n.rb,
lib/eddy/build/elements/id.rb,
lib/eddy/build/elements/element.rb,
lib/eddy/build/elements/elements.rb

Class Method Summary collapse

Class Method Details

.build_code_list(id) ⇒ String



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ String



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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: "      @id = \"\#{el.id}\"\n      @name = \"\#{el.name}\"\n      @description = \"\#{el.description}\"\n      super(\n        min: \#{el.min},\n        max: \#{el.max},\n        req: req,\n        ref: ref,\n        val: val,\n      )\n    RB\n  }).render()\nend\n",

.determine_decimals(type) ⇒ Integer



40
41
42
43
44
# 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) ⇒ void



55
56
57
58
59
60
61
62
63
# 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) ⇒ Array<Hash>



9
10
11
12
13
14
15
# 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_dataArray<Eddy::Summary::Element>

Generate usable data from data/elements.tsv.



35
36
37
38
39
40
41
42
# 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()) ⇒ void

This method returns an undefined value.

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



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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) ⇒ Ginny::Class



41
42
43
44
45
46
47
48
49
50
# 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) ⇒ void



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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) ⇒ void



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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:   "      @id = \"\#{el.id}\"\n      @name = \"\#{el.name}\"\n      @description = \"\#{el.description}\"\n      super(\n        min: \#{el.min},\n        max: \#{el.max},\n        req: req,\n        ref: ref,\n        val: val,\n        decimals: \#{decimal_points},\n      )\n    RB\n  }).render()\n  c = self.ginny_class(el, constructor)\n  return c.render if test\n  c.generate(\n    File.join(Eddy::Util.root_dir, \"build\", \"elements\"),\n    file: \"\#{el.id}.\#{Eddy::Util.snake_case(el.name)}.rb\",\n  )\n  return nil\nend\n",