Class: LangHandler

Inherits:
Handler show all
Defined in:
backend/app/lib/bulk_import/lang_handler.rb

Constant Summary

Constants inherited from Handler

Handler::DISAMB_STR

Instance Method Summary collapse

Methods inherited from Handler

#clear, #save, #search, #stored

Constructor Details

#initialize(current_user) ⇒ LangHandler

Returns a new instance of LangHandler.



2
3
4
5
# File 'backend/app/lib/bulk_import/lang_handler.rb', line 2

def initialize(current_user)
  @language_types = CvList.new("language_iso639_2", current_user)
  @script_types = CvList.new("script_iso15924", current_user)
end

Instance Method Details

#create_language(lang_val, script, langmaterial, publish, report) ⇒ Object



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
66
# File 'backend/app/lib/bulk_import/lang_handler.rb', line 12

def create_language(lang_val, script, langmaterial, publish, report)
  langs = []
  have_lang = !lang_val.nil?
  have_note = !langmaterial.nil?
  if have_lang || have_note
    lang_code = nil
    if !have_lang
      begin
        lang_code = @language_types.value(langmaterial)
        have_note = false
      rescue Exception => n
        #we know that the note isn't just the language
      end
    else
      begin
        lang_code = @language_types.value(lang_val)
      rescue Exception => n
        report.add_errors(I18n.t("bulk_import.error.lang_code", :lang => lang_val))
      end
    end
    if lang_code
      langscript = JSONModel(:language_and_script).new
      langscript.language = lang_code
      if !script.nil?
        begin
          langscript.script = @script_types.value(script)
        rescue Exception => n
          report.add_errors(I18n.t("bulk_import.error.script_code", :script => script))
        end
      end
      lang = JSONModel(:lang_material).new
      lang.language_and_script = langscript
      langs.push lang
    end
    if have_note
      lang = JSONModel(:lang_material).new
      content = langmaterial
      begin
        wellformed(content)
        note = JSONModel(:note_langmaterial).new
        if publish.class.name == "String"
          publish = publish == "1"
        end
        note.publish = publish
        note.type = "langmaterial"
        note.content.push content if !content.nil?
        lang.notes.push note
        langs.push lang
      rescue Exception => e
        report.add_errors(I18n.t("bulk_import.error.bad_note", :type => "langmaterial", :msg => e.message))
      end
    end
  end
  langs
end

#renewObject



7
8
9
10
# File 'backend/app/lib/bulk_import/lang_handler.rb', line 7

def renew
  clear(@language_types)
  clear(@script_types)
end

#wellformed(note) ⇒ Object

currently a repeat from the controller



69
70
71
72
73
# File 'backend/app/lib/bulk_import/lang_handler.rb', line 69

def wellformed(note)
  if note.match("</?[a-zA-Z]+>")
    frag = Nokogiri::XML("<root xmlns:xlink='https://www.w3.org/1999/xlink'>#{note}</root>") { |config| config.strict }
  end
end