Class: Logaling::Glossary

Inherits:
Object
  • Object
show all
Defined in:
lib/logaling/glossary.rb

Constant Summary collapse

SUPPORTED_FILE_TYPE =
%w(yml tsv csv)
SUPPORTED_ANNOTATION =
%w(wip)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, source_language, target_language, project = nil) ⇒ Glossary

Returns a new instance of Glossary.



24
25
26
27
28
29
# File 'lib/logaling/glossary.rb', line 24

def initialize(name, source_language, target_language, project=nil)
  @name = name
  @source_language = source_language
  @target_language = target_language
  @project = project
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/logaling/glossary.rb', line 22

def name
  @name
end

#projectObject (readonly)

Returns the value of attribute project.



22
23
24
# File 'lib/logaling/glossary.rb', line 22

def project
  @project
end

#source_languageObject (readonly)

Returns the value of attribute source_language.



22
23
24
# File 'lib/logaling/glossary.rb', line 22

def source_language
  @source_language
end

#target_languageObject (readonly)

Returns the value of attribute target_language.



22
23
24
# File 'lib/logaling/glossary.rb', line 22

def target_language
  @target_language
end

Instance Method Details

#add(source_term, target_term, note, index_after_adding = true) ⇒ Object



56
57
58
59
60
# File 'lib/logaling/glossary.rb', line 56

def add(source_term, target_term, note, index_after_adding=true)
  raise Logaling::TermError if bilingual_pair_exists?(source_term, target_term)
  glossary_source.add(source_term, target_term, note)
  index! if index_after_adding
end

#bilingual_pair_exists?(source_term, target_term, note = nil) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/logaling/glossary.rb', line 52

def bilingual_pair_exists?(source_term, target_term, note=nil)
  !find_bilingual_pairs(source_term, target_term, note).empty?
end

#delete(source_term, target_term) ⇒ Object



71
72
73
74
75
# File 'lib/logaling/glossary.rb', line 71

def delete(source_term, target_term)
  raise Logaling::TermError unless bilingual_pair_exists?(source_term, target_term)
  glossary_source.delete(source_term, target_term)
  index!
end

#delete_all(source_term, force = false) ⇒ Object



77
78
79
80
# File 'lib/logaling/glossary.rb', line 77

def delete_all(source_term, force=false)
  glossary_source.delete_all(source_term, force)
  index!
end

#find_bilingual_pairs(source_term, target_term, note = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/logaling/glossary.rb', line 42

def find_bilingual_pairs(source_term, target_term, note=nil)
  raise Logaling::GlossaryDBNotFound unless File.exist?(@project.glossary_db_path)
  index
  terms = []
  Logaling::GlossaryDB.open(@project.glossary_db_path, "utf8") do |db|
    terms = db.get_bilingual_pair(source_term, target_term, @name, note)
  end
  terms.delete_if {|t| t[:target_language] != @target_language }
end

#glossary_sourceObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/logaling/glossary.rb', line 82

def glossary_source
  if @glossary_source
    @glossary_source
  else
    FileUtils.mkdir_p(@project.source_directory_path)

    file_name = [@name, @source_language, @target_language, 'yml'].join('.')
    source_path = @project.relative_path(file_name)
    @glossary_source = Logaling::GlossarySource.create(source_path, self)
  end
end

#index(options = { force: false }) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/logaling/glossary.rb', line 112

def index(options = { force: false })
  Logaling::GlossaryDB.open(@project.glossary_db_path, "utf8") do |db|
    db.recreate_table
    glossary_sources.each do |glossary_source|
      if !db.glossary_source_exist?(glossary_source) || options[:force] == true
        puts "now index #{@name}..."
        db.index_glossary_source(glossary_source)
      end
    end
    indexed_glossary_sources = db.glossary_sources_related_on_glossary(self)
    (indexed_glossary_sources - glossary_sources).each do |removed_glossary_source|
      puts "now deindex #{@name}..."
      db.deindex_glossary_source(removed_glossary_source)
    end
  end
end

#index!Object



108
109
110
# File 'lib/logaling/glossary.rb', line 108

def index!
  index(force: true)
end

#initialize_glossary_sourceObject



100
101
102
# File 'lib/logaling/glossary.rb', line 100

def initialize_glossary_source
  glossary_source.initialize_source
end

#merge!(glossary) ⇒ Object



94
95
96
97
98
# File 'lib/logaling/glossary.rb', line 94

def merge!(glossary)
  glossary.terms.each do |term|
    add(term[:source_term], term[:target_term], term[:note])
  end
end

#same?(glossary) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/logaling/glossary.rb', line 129

def same?(glossary)
  name == glossary.name
end

#terms(annotation_word = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/logaling/glossary.rb', line 31

def terms(annotation_word=nil)
  raise Logaling::GlossaryDBNotFound unless File.exist?(@project.glossary_db_path)
  index
  terms = []
  filter_option = annotation_word ? '@' + annotation_word : annotation_word
  Logaling::GlossaryDB.open(@project.glossary_db_path, "utf8") do |db|
    terms = db.translation_list(self, filter_option)
  end
  terms
end

#to_sObject



104
105
106
# File 'lib/logaling/glossary.rb', line 104

def to_s
  [@name, @source_language, @target_language].join('.')
end

#update(source_term, target_term, new_target_term, note) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/logaling/glossary.rb', line 62

def update(source_term, target_term, new_target_term, note)
  if (target_term != new_target_term) && bilingual_pair_exists?(source_term, new_target_term)
    raise Logaling::TermError
  end

  glossary_source.update(source_term, target_term, new_target_term, note)
  index!
end