Class: Tc211::Termbase::MetadataSection

Inherits:
SheetSection show all
Defined in:
lib/tc211/termbase/metadata_section.rb

Constant Summary collapse

GLOSSARY_HEADER_ROW_MATCH =
{
  # "English" uses "".
  # "Arabic" uses "A".
  # This is fixed in the MLGT as of 2018 Aug 6.
  "A" => [nil, "Item", "A"],

  "C" => ["Data Type"],
  "D" => ["Special Instruction"],

  # "Malay" has it empty ("")
  # This is fixed in the MLGT as of 2018 Aug 6.
  "E" => ["ISO 19135 Class.attribute", nil],

  "F" => ["Domain"],
}.freeze
GLOSSARY_ROW_KEY_MAP =
{
  "A" => "name",
  "B" => "value",
  "C" => "datatype",
  "D" => "special-instruction",
  "E" => "19135-class-attribute",
  "F" => "value-domain",
}.freeze

Instance Attribute Summary collapse

Attributes inherited from SheetSection

#sheet_content

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SheetSection

identify_type

Constructor Details

#initialize(rows, options = {}) ⇒ MetadataSection

Returns a new instance of MetadataSection.



33
34
35
36
37
38
39
40
41
# File 'lib/tc211/termbase/metadata_section.rb', line 33

def initialize(rows, options = {})
  super

  self.class.match_header(@rows[0])
  @header_row = @rows[0]
  @body_rows = @rows[1..-1]
  attributes
  self
end

Instance Attribute Details

#attributesObject



97
98
99
100
101
102
103
104
105
106
# File 'lib/tc211/termbase/metadata_section.rb', line 97

def attributes
  return @attributes if @attributes

  @attributes = {}
  @body_rows.each do |row|
    result = parse_row(row)
    @attributes.merge!(result) if result
  end
  @attributes
end

#header_rowObject

Returns the value of attribute header_row.



5
6
7
# File 'lib/tc211/termbase/metadata_section.rb', line 5

def header_row
  @header_row
end

Class Method Details

.match_header(columns) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tc211/termbase/metadata_section.rb', line 43

def self.match_header(columns)
  # puts "row #{row}"
  columns.each do |key, value|
    # puts "#{key}, #{value}"
    header_row_match = GLOSSARY_HEADER_ROW_MATCH[key]
    if header_row_match && !header_row_match.include?(value)
      raise RowHeaderMatchError.new(
        "Metadata section header for column `#{key}` does not match \
        expected value `#{value}`",
      )
    end
  end
end

Instance Method Details

#clean_key(key) ⇒ Object



61
62
63
64
65
66
# File 'lib/tc211/termbase/metadata_section.rb', line 61

def clean_key(key)
  key.strip
    .downcase
    .gsub(/[()]/, "")
    .gsub(" ", "-")
end

#clean_value(value) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/tc211/termbase/metadata_section.rb', line 68

def clean_value(value)
  return nil if value.nil?

  case value
  when String
    value.strip
  else
    value
  end
end

#fieldsObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/tc211/termbase/metadata_section.rb', line 108

def fields
  # "operating-language-country"=>
  #  {"name"=>"Operating Language Country",
  #   "value"=>"410",
  #   "datatype"=>"Country Code",
  #   "special-instruction"=>
  #    "ftp.ics.uci.edu/pub/ietf/http/related/iso3166.txt",
  #   "19135-class-attribute"=>"RE_Register.operatingLanguage",
  #   "value-domain"=>
  #    "<<Data Type>>RE_Locale.country \n" +
  #    "[ISO 3166-1 3-character numeric country code]"},
  #

  attributes.inject({}) do |acc, (k, v)|
    acc.merge({ k => v["value"] })
  end
end

#parse_row(row) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/tc211/termbase/metadata_section.rb', line 79

def parse_row(row)
  return nil if row.empty?

  attribute = {}

  structure.each_pair do |key, value|
    attribute_value = clean_value(row[key])
    next if attribute_value.nil?

    attribute[clean_key(value)] = attribute_value
  end

  # TODO: "Chinese" name is empty!
  key = clean_key(attribute["name"] || "(empty)")

  { key => attribute }
end

#structureObject



57
58
59
# File 'lib/tc211/termbase/metadata_section.rb', line 57

def structure
  GLOSSARY_ROW_KEY_MAP
end

#to_hashObject



126
127
128
129
130
# File 'lib/tc211/termbase/metadata_section.rb', line 126

def to_hash
  {
    "metadata" => fields,
  }
end