Class: Odca::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/odca/parser.rb

Defined Under Namespace

Classes: OverlayDto

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records, output_dir) ⇒ Parser

Returns a new instance of Parser.



11
12
13
14
15
# File 'lib/odca/parser.rb', line 11

def initialize(records, output_dir)
  @overlay_dtos = []
  @records = records
  @output_dir = output_dir
end

Instance Attribute Details

#output_dirObject (readonly)

Returns the value of attribute output_dir.



9
10
11
# File 'lib/odca/parser.rb', line 9

def output_dir
  @output_dir
end

#overlay_dtosObject (readonly)

Returns the value of attribute overlay_dtos.



9
10
11
# File 'lib/odca/parser.rb', line 9

def overlay_dtos
  @overlay_dtos
end

#recordsObject (readonly)

Returns the value of attribute records.



9
10
11
# File 'lib/odca/parser.rb', line 9

def records
  @records
end

Instance Method Details

#callObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/odca/parser.rb', line 17

def call
  columns_number = records[0].size

  (6..columns_number - 1).each do |i|
    overlay_dtos << OverlayDto.new(
      index: i,
      name: records[2][i],
      role: records[0][i],
      purpose: records[1][i],
      language: records[3][i]
    )
  end
  records.slice!(0, 4)

  schemas = separate_schemas(records)
  schemas.each do |schema|
    schema_base, overlays = schema.call
    next unless schema_base && overlays
    save(schema_base: schema_base, overlays: overlays)
  end
end

#save(schema_base:, overlays:) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/odca/parser.rb', line 54

def save(schema_base:, overlays:)
  path = "#{output_dir}/#{schema_base.name}"

  save_schema_base(schema_base, path: path)

  overlays.each do |overlay|
    next if overlay.empty?
    save_overlay(overlay, path: path)
  end
end

#save_overlay(headful_overlay, path:) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/odca/parser.rb', line 73

def save_overlay(headful_overlay, path:)
  overlay_class_name = headful_overlay.overlay.class
    .name.split('::').last
  hl = HashlinkGenerator.call(headful_overlay)

  File.open("#{path}/#{overlay_class_name}-#{hl}.json", 'w') do |f|
    f.write(JSON.pretty_generate(headful_overlay))
  end
end

#save_schema_base(schema_base, path:) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/odca/parser.rb', line 65

def save_schema_base(schema_base, path:)
  FileUtils.mkdir_p(path) unless Dir.exist?(path)

  File.open("#{path}.json", 'w') do |f|
    f.write(JSON.pretty_generate(schema_base))
  end
end