Class: Odca::SchemaParser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records, overlay_dtos) ⇒ SchemaParser

Returns a new instance of SchemaParser.



5
6
7
8
# File 'lib/odca/schema_parser.rb', line 5

def initialize(records, overlay_dtos)
  @records = records
  @overlay_dtos = overlay_dtos
end

Instance Attribute Details

#overlay_dtosObject (readonly)

Returns the value of attribute overlay_dtos.



3
4
5
# File 'lib/odca/schema_parser.rb', line 3

def overlay_dtos
  @overlay_dtos
end

#recordsObject (readonly)

Returns the value of attribute records.



3
4
5
# File 'lib/odca/schema_parser.rb', line 3

def records
  @records
end

Instance Method Details

#add_attribute(to:, attr_name:, value:) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/odca/schema_parser.rb', line 67

def add_attribute(to:, attr_name:, value:)
  overlay = to
  overlay_name = overlay.class.name.split('::').last
  begin
    attribute_class = overlay.class
      .const_get(overlay_name.gsub('Overlay', 'Attribute'))
    validator_class = overlay.class
      .const_get('InputValidator')
  rescue => e
    raise "Not found Attribute Class for '#{overlay_name}': #{e}"
  end

  overlay.add_attribute(
    attribute_class.new(
      validator_class.new(
        attr_name: attr_name,
        value: value
      ).call
    )
  )
end

#callObject



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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/odca/schema_parser.rb', line 10

def call
  schema_base = Odca::SchemaBase.new(
    Odca::SchemaBase::Header.new(
      name: records.first[0],
      description: records.first[1],
      classification: records.first[2]
    )
  )

  overlay_attrs = {}
  overlay_indexes = overlay_dtos.map(&:index)

  records.each do |row|
    attr_name = row[3]
    attr_type = row[4]

    schema_base.add_attribute(
      SchemaBase::Attribute.new(
        name: attr_name,
        type: attr_type,
        pii: row[5]
      )
    )
    overlay_indexes.each do |ov_index|
      next unless row[ov_index]
      (overlay_attrs[ov_index] ||= []) << {
        name: attr_name,
        value: row[ov_index]
      }
    end
  end

  overlays = []
  overlay_dtos.each do |overlay_dto|
    attrs = overlay_attrs[overlay_dto.index]
    next unless attrs

    overlay = create_overlay(overlay_dto)
    attrs.each do |attr|
      add_attribute(
        to: overlay,
        attr_name: attr[:name],
        value: attr[:value]
      )
    end
    overlays << Odca::HeadfulOverlay.new(
      parentful_overlay: Odca::ParentfulOverlay.new(
        parent: schema_base, overlay: overlay
      ),
      role: overlay_dto.role,
      purpose: overlay_dto.purpose
    )
  end

  [schema_base, overlays]
end