Class: Logaling::Command

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

Constant Summary

VERSION =
"0.1.1"
LOGALING_CONFIG =
'.logaling'

Instance Method Summary (collapse)

Instance Method Details

- (Object) add(source_term, target_term, note = '')



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/logaling/command.rb', line 122

def add(source_term, target_term, note='')
  config = load_config_and_merge_options
  repository.index

  if repository.bilingual_pair_exists?(source_term, target_term, config["glossary"])
    raise Logaling::TermError, "term '#{source_term}: #{target_term}' already exists in '#{config["glossary"]}'"
  end

  glossary.add(source_term, target_term, note)
rescue Logaling::CommandFailed, Logaling::TermError => e
  say e.message
end

- (Object) config(key, value)



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/logaling/command.rb', line 106

def config(key, value)
  support_keys = %w(glossary source-language target-language)
  raise Logaling::CommandFailed, "#{key} is unsupported option" unless support_keys.include?(key)

  config_path = options["global"] ? File.join(LOGALING_HOME, "config") : File.join(find_dotfile, "config")
  FileUtils.touch(config_path) unless File.exist?(config_path)

  config = load_config(config_path)
  config = merge_options({key => value}, config)
  write_config(config_path, config)
  say "Successfully set config."
rescue Logaling::CommandFailed => e
  say e.message
end

- (Object) delete(source_term, target_term = nil)



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/logaling/command.rb', line 137

def delete(source_term, target_term=nil)
  if target_term
    glossary.delete(source_term, target_term)
  else
    glossary.delete_all(source_term, options["force"])
  end
rescue Logaling::CommandFailed, Logaling::TermError => e
  say e.message
rescue Logaling::GlossaryNotFound => e
  say "Try 'loga new or register' first."
end

- (Object) import(external_glossary = nil)



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/logaling/command.rb', line 62

def import(external_glossary=nil)
  require "logaling/external_glossary"
  Logaling::ExternalGlossary.load
  if options["list"]
    Logaling::ExternalGlossary.list.each {|glossary| say "#{glossary.name.bright} : #{glossary.description}" }
  else
    repository.import(Logaling::ExternalGlossary.get(external_glossary))
  end
rescue Logaling::ExternalGlossaryNotFound
  say "'#{external_glossary}' can't find in import list."
  say "Try 'loga import --list' and confirm import list."
end

- (Object) list



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/logaling/command.rb', line 228

def list
  repository.index
  glossaries = repository.list
  unless glossaries.empty?
    run_pager
    glossaries.each do |glossary|
      printf("  %s\n", glossary)
    end
  else
    "There is no registered glossary."
  end

rescue Logaling::CommandFailed, Logaling::GlossaryDBNotFound => e
  say e.message
end

- (Object) lookup(source_term)



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/logaling/command.rb', line 166

def lookup(source_term)
  config = load_config_and_merge_options
  repository.index
  terms = repository.lookup(source_term, config["source_language"], config["target_language"], config["glossary"])

  unless terms.empty?
    max_str_size = terms.map{|term| term[:source_term].size}.sort.last
    run_pager
    terms.each do |term|
      target_string = "#{term[:target_term].bright}"
      target_string <<  "\t# #{term[:note]}" unless term[:note].empty?
      if repository.glossary_counts > 1
        target_string << "\t"
        glossary_name = "(#{term[:glossary_name]})"
        if term[:glossary_name] == config["glossary"]
          target_string << glossary_name.foreground(:white).background(:green)
        else
          target_string << glossary_name
        end
      end
      source_string = term[:snipped_source_term].map{|word|  word.is_a?(Hash) ? word[:keyword].bright : word }.join
      printf("  %-#{max_str_size+10}s %s\n", source_string, target_string)
    end
  else
    "source-term <#{source_term}> not found"
  end
rescue Logaling::CommandFailed, Logaling::TermError => e
  say e.message
end

- (Object) new(project_name, source_language, target_language = nil)



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/logaling/command.rb', line 46

def new(project_name, source_language, target_language=nil)
  unless File.exist?(LOGALING_CONFIG)
    FileUtils.mkdir_p(File.join(LOGALING_CONFIG, "glossary"))
    config = {"glossary" => project_name, "source-language" => source_language}
    config["target-language"] = target_language if target_language
    write_config(File.join(LOGALING_CONFIG, "config"), config)

    register unless options["no-register"]
    say "Successfully created #{LOGALING_CONFIG}"
  else
    say "#{LOGALING_CONFIG} already exists."
  end
end

- (Object) register



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/logaling/command.rb', line 76

def register
  logaling_path = find_dotfile

  required_options = {"glossary" => "input glossary name '-g <glossary name>'"}
  config = load_config_and_merge_options(required_options)

  repository.register(logaling_path, config["glossary"])
  say "#{config['glossary']} is now registered to logaling."
rescue Logaling::CommandFailed => e
  say e.message
  say "Try 'loga new' first."
rescue Logaling::GlossaryAlreadyRegistered => e
  say "#{config['glossary']} is already registered."
end

- (Object) show



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/logaling/command.rb', line 202

def show
  required_options = {
    "glossary" => "input glossary name '-g <glossary name>'",
    "source-language" => "input source-language code '-S <source-language code>'",
    "target-language" => "input target-language code '-T <target-language code>'"
  }
  config = load_config_and_merge_options(required_options)
  repository.index
  terms = repository.show_glossary(config["glossary"], config["source-language"], config["target-language"])
  unless terms.empty?
    run_pager
    max_str_size = terms.map{|term| term[:source_term].size}.sort.last
    terms.each do |term|
      target_string = "#{term[:target_term]}"
      target_string <<  "\t# #{term[:note]}" unless term[:note].empty?
      printf("  %-#{max_str_size+10}s %s\n", term[:source_term], target_string)
    end
  else
    "glossary <#{config['glossary']}> not found"
  end

rescue Logaling::CommandFailed, Logaling::GlossaryDBNotFound => e
  say e.message
end

- (Object) unregister



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

def unregister
  required_options = {"glossary" => "input glossary name '-g <glossary name>'"}
  config = load_config_and_merge_options(required_options)

  repository.unregister(config["glossary"])
  say "#{config['glossary']} is now unregistered."
rescue Logaling::CommandFailed => e
  say e.message
rescue Logaling::GlossaryNotFound => e
  say "#{config['glossary']} is not yet registered."
end

- (Object) update(source_term, target_term, new_target_term, note = '')



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/logaling/command.rb', line 150

def update(source_term, target_term, new_target_term, note='')
  config = load_config_and_merge_options
  repository.index

  if repository.bilingual_pair_exists_and_has_same_note?(source_term, new_target_term, note, config["glossary"])
    raise Logaling::TermError, "term '#{source_term}: #{new_target_term}' already exists in '#{config["glossary"]}'"
  end

  glossary.update(source_term, target_term, new_target_term, note)
rescue Logaling::CommandFailed, Logaling::TermError => e
  say e.message
rescue Logaling::GlossaryNotFound => e
  say "Try 'loga new or register' first."
end

- (Object) version



197
198
199
# File 'lib/logaling/command.rb', line 197

def version
  say "logaling-command version #{Logaling::Command::VERSION}"
end