Class: Logaling::Repository

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

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Repository

Returns a new instance of Repository.



24
25
26
# File 'lib/logaling/repository.rb', line 24

def initialize(path)
  @path = path
end

Instance Method Details

#config_pathObject



165
166
167
168
# File 'lib/logaling/repository.rb', line 165

def config_path
  path = File.join(logaling_home, "config")
  File.exist?(path) ? path : nil
end

#create_personal_project(project_name, source_language, target_language) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/logaling/repository.rb', line 49

def create_personal_project(project_name, source_language, target_language)
  if glossary_exists?(project_name, source_language, target_language)
    raise Logaling::GlossaryAlreadyRegistered, "The glossary '#{project_name}' already exists."
  end
  personal_project_path = relative_path(personal_glossary_root_path)
  PersonalProject.create(personal_project_path, project_name, source_language, target_language, self)
end

#except_annotation(terms) ⇒ Object



106
107
108
109
110
111
# File 'lib/logaling/repository.rb', line 106

def except_annotation(terms)
  fixed_terms = terms.reject do |term|
    Logaling::Glossary::SUPPORTED_ANNOTATION.any? {|ann| term[:note].index(ann) }
  end
  fixed_terms
end

#expand_path(relative_path) ⇒ Object



174
175
176
# File 'lib/logaling/repository.rb', line 174

def expand_path(relative_path)
  File.expand_path(File.join(logaling_home, relative_path))
end

#find_glossary(project_name, source_language, target_language) ⇒ Object



158
159
160
161
162
163
# File 'lib/logaling/repository.rb', line 158

def find_glossary(project_name, source_language, target_language)
  project = projects.detect do |project|
    project.name == project_name and project.has_glossary?(source_language, target_language)
  end
  project ? project.glossary(source_language, target_language) : nil
end

#find_project(project_name) ⇒ Object



154
155
156
# File 'lib/logaling/repository.rb', line 154

def find_project(project_name)
  projects.detect {|project| project.name == project_name }
end

#glossary_countsObject



150
151
152
# File 'lib/logaling/repository.rb', line 150

def glossary_counts
  [registered_project_paths, imported_glossary_paths].map(&:size).inject(&:+)
end

#import(glossary_source) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/logaling/repository.rb', line 70

def import(glossary_source)
  FileUtils.mkdir_p(cache_path)
  Dir.chdir(cache_path) do
    glossary_source.import
  end
  index
rescue
  raise Logaling::CommandFailed, "Failed import #{glossary_source.class.name} to #{cache_path}."
end

#import_tmx(glossary_source, glossary, url) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/logaling/repository.rb', line 80

def import_tmx(glossary_source, glossary, url)
  FileUtils.mkdir_p(cache_path)
  Dir.chdir(cache_path) do
    glossary_source.import(glossary, url)
  end
  index
rescue Logaling::GlossaryNotFound => e
  raise e
rescue
  raise Logaling::CommandFailed, "Failed import_tmx #{glossary_source.class.name} to #{cache_path}."
end

#imported_projectsObject



124
125
126
127
128
# File 'lib/logaling/repository.rb', line 124

def imported_projects
  imported_glossary_paths.map do |imported_project_path|
    Logaling::ImportedProject.new(relative_path(imported_project_path), self)
  end
end

#indexObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/logaling/repository.rb', line 130

def index
  all_glossary_sources = projects.map {|project| project.glossary_sources }.flatten

  Logaling::GlossaryDB.open(logaling_db_home, "utf8") do |db|
    db.recreate_table
    all_glossary_sources.each do |glossary_source|
      glossary = glossary_source.glossary
      unless db.glossary_source_exist?(glossary_source)
        puts "now index #{glossary.name}..."
        db.index_glossary(glossary, glossary_source)
      end
    end
    (db.get_all_glossary_sources - all_glossary_sources).each do |glossary_source|
      glossary = glossary_source.glossary
      puts "now deindex #{glossary.name}..."
      db.deindex_glossary(glossary, glossary_source)
    end
  end
end

#logaling_db_homeObject



170
171
172
# File 'lib/logaling/repository.rb', line 170

def logaling_db_home
  File.join(logaling_home, "db")
end

#lookup(source_term, glossary, options = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/logaling/repository.rb', line 92

def lookup(source_term, glossary, options={})
  raise Logaling::GlossaryDBNotFound unless File.exist?(logaling_db_home)

  terms = []
  Logaling::GlossaryDB.open(logaling_db_home, "utf8") do |db|
    if options['dictionary']
      terms = db.lookup_dictionary(source_term)
    else
      terms = db.lookup(source_term, glossary)
    end
  end
  options['fixed'] ? except_annotation(terms) : terms
end

#projectsObject



113
114
115
116
117
118
119
120
121
122
# File 'lib/logaling/repository.rb', line 113

def projects
  projects = registered_project_paths.map do |project_path|
    Logaling::Project.new(relative_path(project_path), self)
  end
  projects += personal_glossary_paths.map do |personal_glossary_path|
    Logaling::PersonalProject.new(relative_path(personal_glossary_path), self)
  end
  projects += imported_projects
  projects.sort_by(&:path)
end

#register(dot_logaling_path, register_name) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/logaling/repository.rb', line 28

def register(dot_logaling_path, register_name)
  FileUtils.mkdir_p(logaling_projects_path)
  symlink_path = File.join(logaling_projects_path, register_name)
  unless File.exist?(symlink_path)
    FileUtils.ln_s(dot_logaling_path, symlink_path)
  else
    raise Logaling::GlossaryAlreadyRegistered, register_name
  end
  index
rescue Logaling::GlossaryAlreadyRegistered => e
  raise e
rescue
  raise Logaling::CommandFailed, "Failed register #{register_name} to #{logaling_projects_path}."
end

#relative_path(full_path) ⇒ Object



178
179
180
181
182
183
# File 'lib/logaling/repository.rb', line 178

def relative_path(full_path)
  require 'pathname'
  path = Pathname.new(full_path)
  base = Pathname.new(logaling_home)
  path.relative_path_from(base).to_s
end

#remove_personal_project(project_name, source_language, target_language) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/logaling/repository.rb', line 57

def remove_personal_project(project_name, source_language, target_language)
  unless glossary_exists?(project_name, source_language, target_language)
    raise Logaling::GlossaryNotFound, "The glossary '#{project_name}' not found."
  end
  personal_project_path = relative_path(personal_glossary_root_path)
  PersonalProject.remove(personal_project_path, project_name, source_language, target_language, self)
  index
rescue Logaling::GlossaryNotFound => e
  raise e
rescue
  raise Logaling::CommandFailed, "Failed remove the glossary #{project_name}."
end

#unregister(project) ⇒ Object



43
44
45
46
47
# File 'lib/logaling/repository.rb', line 43

def unregister(project)
  raise Logaling::ProjectNotFound unless project
  FileUtils.rm_rf(expand_path(project.path), :secure => true)
  index
end