Class: Tc211::Termbase::TerminologySheet

Inherits:
Object
  • Object
show all
Defined in:
lib/tc211/termbase/terminology_sheet.rb

Direct Known Subclasses

InformationSheet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sheet) ⇒ TerminologySheet

Returns a new instance of TerminologySheet.



8
9
10
11
# File 'lib/tc211/termbase/terminology_sheet.rb', line 8

def initialize(sheet)
  @sheet = sheet
  self
end

Instance Attribute Details

#language_codeObject

Returns the value of attribute language_code.

Raises:

  • (StandardError)


6
7
8
# File 'lib/tc211/termbase/terminology_sheet.rb', line 6

def language_code
  @language_code
end

#sheetObject

Returns the value of attribute sheet.



6
7
8
# File 'lib/tc211/termbase/terminology_sheet.rb', line 6

def sheet
  @sheet
end

Instance Method Details

#get_section(x) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/tc211/termbase/terminology_sheet.rb', line 106

def get_section(x)
  section = nil

  %w(MetadataSection TermsSection).each do |t|
    break if section

    begin
      # puts "rows: #{x.inspect}"
      section = ::Tc211::Termbase.const_get(t)
        .new(x, { parent_sheet: self })
    rescue SheetSection::RowHeaderMatchError
    end
  end

  section
end

#languageObject



13
14
15
# File 'lib/tc211/termbase/terminology_sheet.rb', line 13

def language
  @sheet.name
end

#metadata_sectionObject



67
68
69
70
71
72
73
# File 'lib/tc211/termbase/terminology_sheet.rb', line 67

def 
  sections

  sections.detect do |section|
    section.is_a?(MetadataSection)
  end
end

#sectionsObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/tc211/termbase/terminology_sheet.rb', line 75

def sections
  return @sections if @sections

  @sections = []
  sections_raw.each_with_index do |x,i|
    section = get_section(x)

    unless section
      raise SheetSection::UnknownHeaderError.new(
        "Unable to find header row match for section #{i} header," +
        " contents: #{x.inspect}",
      )
    end

    # MetadataSections always go first, so the language_code must already
    # be set at the time of parsing the TermsSection
    if section.is_a?(MetadataSection)
      code = section.fields["operating-language-code"]
      # puts "lang code is detected as #{code}, #{@language_code}"
      unless code.nil?
        # puts "setting lang code is detected as #{code}"
        set_language_code(code)
      end
    end

    puts "--------- Section #{i} is a #{section.class.name} ---------"

    @sections << section
  end
end

#sections_rawObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tc211/termbase/terminology_sheet.rb', line 47

def sections_raw
  # Sections either start with "A" => "Item",
  # or they have empty lines between
  raw_sections = @sheet.simple_rows.to_a

  raw_sections.reject!(&:empty?)

  raw_sections.slice_before do |row|
    row["A"].to_s == "Item" || row["A"].to_s.match(/^ISO 19135 Field/)
  end.to_a
end

#set_language_code(code) ⇒ Object

Read language_code from sheet



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tc211/termbase/terminology_sheet.rb', line 26

def set_language_code(code)
  # puts "language_code is #{code}"
  return @language_code unless @language_code.nil?

  @language_code = case code
  when "dut/nld"
    "nld"
  when "chi"
    "zho"
  when "ger"
    "deu"
  # below for backwards compatibility
  when "工作语言代码"
    "chn"
  when "pl"
    "pol"
  else
    code
  end
end

#terms_sectionObject



59
60
61
62
63
64
65
# File 'lib/tc211/termbase/terminology_sheet.rb', line 59

def terms_section
  sections

  sections.detect do |section|
    section.is_a?(TermsSection)
  end
end