logaling-command 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/logaling/command.rb +18 -11
- data/lib/logaling/glossary.rb +37 -18
- data/lib/logaling/glossary_db.rb +1 -1
- data/spec/logaling/command_spec.rb +66 -1
- data/spec/logaling/glossary_spec.rb +74 -9
- metadata +10 -10
data/lib/logaling/command.rb
CHANGED
@@ -4,7 +4,7 @@ require 'thor'
|
|
4
4
|
require "logaling/glossary"
|
5
5
|
|
6
6
|
class Logaling::Command < Thor
|
7
|
-
VERSION = "0.0.
|
7
|
+
VERSION = "0.0.6"
|
8
8
|
LOGALING_CONFIG = '.logaling'
|
9
9
|
|
10
10
|
map '-a' => :add,
|
@@ -13,12 +13,13 @@ class Logaling::Command < Thor
|
|
13
13
|
'-l' => :lookup,
|
14
14
|
'-n' => :new,
|
15
15
|
'-r' => :register,
|
16
|
-
'-U' => :unregister
|
16
|
+
'-U' => :unregister,
|
17
|
+
'-v' => :version
|
17
18
|
|
18
19
|
class_option "glossary", type: :string, aliases: "-g"
|
19
20
|
class_option "source-language", type: :string, aliases: "-S"
|
20
21
|
class_option "target-language", type: :string, aliases: "-T"
|
21
|
-
class_option "logaling-home", type: :string,
|
22
|
+
class_option "logaling-home", type: :string, aliases: "-h"
|
22
23
|
|
23
24
|
desc 'new [PROJECT NAME] [SOURCE LANGUAGE] [TARGET LANGUAGE(optional)]', 'Create .logaling'
|
24
25
|
method_option "no-register", type: :boolean, default: false
|
@@ -77,9 +78,10 @@ class Logaling::Command < Thor
|
|
77
78
|
say e.message
|
78
79
|
end
|
79
80
|
|
80
|
-
desc 'delete [SOURCE TERM] [TARGET TERM]', 'Delete term.'
|
81
|
-
|
82
|
-
|
81
|
+
desc 'delete [SOURCE TERM] [TARGET TERM(optional)] [--force(optional)]', 'Delete term.'
|
82
|
+
method_option "force", type: :boolean, default: false
|
83
|
+
def delete(source_term, target_term='')
|
84
|
+
glossary.delete(source_term, target_term, options["force"])
|
83
85
|
rescue Logaling::CommandFailed, Logaling::TermError => e
|
84
86
|
say e.message
|
85
87
|
rescue Logaling::GlossaryNotFound => e
|
@@ -100,13 +102,13 @@ class Logaling::Command < Thor
|
|
100
102
|
glossary.index
|
101
103
|
terms = glossary.lookup(source_term)
|
102
104
|
|
103
|
-
puts "\nlookup word : #{source_term}"
|
104
105
|
unless terms.empty?
|
106
|
+
is_glossary_name_print = terms.map{|t| t[:name]}.uniq.size > 1 ? true : false
|
105
107
|
terms.each do |term|
|
106
|
-
|
107
|
-
|
108
|
-
puts
|
109
|
-
puts "
|
108
|
+
str_term = "#{term[:source_term]} : #{term[:target_term]}"
|
109
|
+
str_term << " # #{term[:note]}" unless term[:note].empty?
|
110
|
+
puts str_term
|
111
|
+
puts "(#{term[:name]})" if is_glossary_name_print
|
110
112
|
end
|
111
113
|
else
|
112
114
|
"source-term <#{source_term}> not found"
|
@@ -115,6 +117,11 @@ class Logaling::Command < Thor
|
|
115
117
|
say e.message
|
116
118
|
end
|
117
119
|
|
120
|
+
desc 'version', 'Show version.'
|
121
|
+
def version
|
122
|
+
say "logaling-command version #{Logaling::Command::VERSION}"
|
123
|
+
end
|
124
|
+
|
118
125
|
private
|
119
126
|
def glossary
|
120
127
|
config = load_config
|
data/lib/logaling/glossary.rb
CHANGED
@@ -21,24 +21,23 @@ module Logaling
|
|
21
21
|
def add(source_term, target_term, note)
|
22
22
|
FileUtils.touch(@path) unless File.exists?(@path)
|
23
23
|
|
24
|
-
|
24
|
+
glossary = load_glossary(@path)
|
25
|
+
if bilingual_pair_exists?(glossary, source_term, target_term)
|
25
26
|
raise TermError, "term '#{source_term}: #{target_term}' already exists in '#{@glossary}'"
|
26
27
|
end
|
27
28
|
|
28
|
-
glossary = load_glossary_yml(@path)
|
29
29
|
glossary << build_term(source_term, target_term, note)
|
30
|
-
|
31
30
|
dump_glossary(glossary)
|
32
31
|
end
|
33
32
|
|
34
33
|
def update(source_term, target_term, new_target_term, note)
|
35
34
|
raise GlossaryNotFound unless File.exists?(@path)
|
36
35
|
|
37
|
-
|
36
|
+
glossary = load_glossary(@path)
|
37
|
+
if bilingual_pair_exists?(glossary, source_term, new_target_term)
|
38
38
|
raise TermError, "term '#{source_term}: #{target_term}' already exists in '#{@glossary}'"
|
39
39
|
end
|
40
40
|
|
41
|
-
glossary = load_glossary_yml(@path)
|
42
41
|
target_index = find_term_index(glossary, source_term, target_term)
|
43
42
|
if target_index
|
44
43
|
glossary[target_index] = rebuild_term(glossary[target_index], source_term, new_target_term, note)
|
@@ -48,16 +47,29 @@ module Logaling
|
|
48
47
|
end
|
49
48
|
end
|
50
49
|
|
51
|
-
def delete(source_term, target_term)
|
50
|
+
def delete(source_term, target_term='', force=false)
|
52
51
|
raise GlossaryNotFound unless File.exists?(@path)
|
53
52
|
|
54
|
-
glossary =
|
55
|
-
|
56
|
-
if
|
53
|
+
glossary = load_glossary(@path)
|
54
|
+
target_num = find_term_num(glossary, source_term)
|
55
|
+
if target_num == 0
|
56
|
+
raise TermError, "Can't found term '#{source_term} #{target_term}' in '#{@glossary}'"
|
57
|
+
elsif !target_term.empty? || target_num == 1
|
58
|
+
target_index = find_term_index(glossary, source_term, target_term)
|
59
|
+
raise TermError, "Can't found term '#{source_term} #{target_term}' in '#{@glossary}'" unless target_index
|
57
60
|
glossary.delete_at(target_index)
|
58
61
|
dump_glossary(glossary)
|
59
62
|
else
|
60
|
-
|
63
|
+
if force
|
64
|
+
while target_index = find_term_index(glossary, source_term) do
|
65
|
+
glossary.delete_at(target_index)
|
66
|
+
dump_glossary(glossary)
|
67
|
+
end
|
68
|
+
else
|
69
|
+
raise TermError, "There are duplicate terms in glossary.\n" +
|
70
|
+
"If you really want to delete, please put `loga delete [SOURCE_TERM] --force`\n" +
|
71
|
+
" or `loga delete [SOURCE_TERM] [TARGET_TERM]`"
|
72
|
+
end
|
61
73
|
end
|
62
74
|
end
|
63
75
|
|
@@ -146,20 +158,27 @@ module Logaling
|
|
146
158
|
build_term(source_term, target_term, note)
|
147
159
|
end
|
148
160
|
|
149
|
-
def find_term_index(
|
150
|
-
|
151
|
-
|
161
|
+
def find_term_index(glossary, source_term, target_term='')
|
162
|
+
glossary.find_index do |term|
|
163
|
+
if target_term.empty?
|
164
|
+
term['source_term'] == source_term
|
165
|
+
else
|
166
|
+
term['source_term'] == source_term && term['target_term'] == target_term
|
167
|
+
end
|
152
168
|
end
|
153
169
|
end
|
154
170
|
|
155
|
-
def
|
156
|
-
|
171
|
+
def find_term_num(glossary, source_term)
|
172
|
+
glossary.select{|term| term["source_term"] == source_term}.size
|
173
|
+
end
|
174
|
+
|
175
|
+
def bilingual_pair_exists?(glossary, source_term, target_term)
|
176
|
+
target_terms(glossary, source_term).any?{|data| data['target_term'] == target_term }
|
157
177
|
end
|
158
178
|
|
159
|
-
def target_terms(
|
179
|
+
def target_terms(glossary, source_term)
|
160
180
|
target_terms = []
|
161
|
-
|
162
|
-
glossaly.each do |term|
|
181
|
+
glossary.each do |term|
|
163
182
|
target_terms << term if term['source_term'] == source_term
|
164
183
|
end
|
165
184
|
target_terms
|
data/lib/logaling/glossary_db.rb
CHANGED
@@ -232,7 +232,72 @@ describe Logaling::Command do
|
|
232
232
|
end
|
233
233
|
|
234
234
|
it 'succeed at find by term without command.index' do
|
235
|
-
@stdout.should
|
235
|
+
@stdout.should include "spec : スペック # 備考"
|
236
|
+
@stdout.should_not include "(spec)"
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
describe "#delete" do
|
242
|
+
before do
|
243
|
+
command.new('spec', 'en', 'ja')
|
244
|
+
command.add('spec', 'スペック', '備考')
|
245
|
+
command.add('user', 'ユーザ', '備考')
|
246
|
+
command.add('test', 'てすと1', '備考')
|
247
|
+
command.add('test', 'てすと2', '備考')
|
248
|
+
end
|
249
|
+
|
250
|
+
context 'with arguments exist term' do
|
251
|
+
before do
|
252
|
+
command.delete('spec', 'スペック')
|
253
|
+
@stdout = capture(:stdout) {command.lookup("spec")}
|
254
|
+
end
|
255
|
+
|
256
|
+
it 'should delete the term' do
|
257
|
+
@stdout.should_not include "spec : スペック # 備考"
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
context 'without target_term' do
|
262
|
+
context 'only 1 bilingual pair exist' do
|
263
|
+
before do
|
264
|
+
command.delete('user')
|
265
|
+
@stdout = capture(:stdout) {command.lookup("user")}
|
266
|
+
end
|
267
|
+
|
268
|
+
it 'should delete the term' do
|
269
|
+
@stdout.should_not include "user : ユーザ # 備考"
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
context 'some bilingual pair exist' do
|
274
|
+
context "called without '--force=true'" do
|
275
|
+
before do
|
276
|
+
@stdout = capture(:stdout) {command.delete('test')}
|
277
|
+
end
|
278
|
+
|
279
|
+
it "should print usage" do
|
280
|
+
@stdout.should == "There are duplicate terms in glossary.\n" +
|
281
|
+
"If you really want to delete, please put `loga delete [SOURCE_TERM] --force`\n" +
|
282
|
+
" or `loga delete [SOURCE_TERM] [TARGET_TERM]`\n"
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
context "and called with '--force=true'" do
|
287
|
+
before do
|
288
|
+
command.options = base_options.merge("force" => true)
|
289
|
+
command.new('spec', 'en', 'ja')
|
290
|
+
command.add('term', '用語1', '備考')
|
291
|
+
command.add('term', '用語2', '備考')
|
292
|
+
command.delete("term")
|
293
|
+
@stdout = capture(:stdout) {command.lookup("term")}
|
294
|
+
end
|
295
|
+
|
296
|
+
it 'should delete bilingual pairs' do
|
297
|
+
@stdout.should_not include "term : 用語1 # 備考"
|
298
|
+
@stdout.should_not include "term : 用語2 # 備考"
|
299
|
+
end
|
300
|
+
end
|
236
301
|
end
|
237
302
|
end
|
238
303
|
end
|
@@ -74,14 +74,79 @@ module Logaling
|
|
74
74
|
end
|
75
75
|
|
76
76
|
describe '#delete' do
|
77
|
-
|
78
|
-
|
77
|
+
context 'given target_term' do
|
78
|
+
context 'bilingual pair exists' do
|
79
|
+
before do
|
80
|
+
glossary.add("delete", "てすと1", "備考")
|
81
|
+
glossary.add("delete", "てすと2", "備考")
|
82
|
+
glossary.delete("delete", "てすと1")
|
83
|
+
glossary.index
|
84
|
+
@result = glossary.lookup("delete")
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'should delete the bilingual pair' do
|
88
|
+
@result.should include({:name=>"spec", :source_language=>"en", :target_language=>"ja", :source_term=>"delete", :target_term=>"てすと2", :note=>"備考"})
|
89
|
+
@result.should_not include({:name=>"spec", :source_language=>"en", :target_language=>"ja", :source_term=>"delete", :target_term=>"てすと1", :note=>"備考"})
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context 'bilingual pair does not exist' do
|
94
|
+
before do
|
95
|
+
glossary.add("user", "ユーザ", "ユーザーではない")
|
96
|
+
end
|
97
|
+
|
98
|
+
it {
|
99
|
+
-> { glossary.delete("user", "ユーザー") }.should raise_error(Logaling::TermError)
|
100
|
+
}
|
101
|
+
end
|
79
102
|
end
|
80
103
|
|
81
|
-
context '
|
82
|
-
|
83
|
-
|
84
|
-
|
104
|
+
context 'not given target_term' do
|
105
|
+
context 'source_term not found' do
|
106
|
+
before do
|
107
|
+
glossary.add("user", "ユーザ", "備考")
|
108
|
+
end
|
109
|
+
|
110
|
+
it {
|
111
|
+
-> { glossary.delete("usr") }.should raise_error(Logaling::TermError)
|
112
|
+
}
|
113
|
+
end
|
114
|
+
|
115
|
+
context 'source_term found' do
|
116
|
+
context 'there is only 1 bilingual pair' do
|
117
|
+
before do
|
118
|
+
glossary.add("user", "ユーザ", "備考")
|
119
|
+
glossary.delete("user")
|
120
|
+
glossary.index
|
121
|
+
@result = glossary.lookup("user")
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'should delete the term' do
|
125
|
+
@result.should_not include({:name=>"spec", :source_language=>"en", :target_language=>"ja", :source_term=>"user", :target_term=>"ユーザ", :note=>"備考"})
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
context 'there are more than 1 bilingual pair' do
|
130
|
+
before do
|
131
|
+
glossary.add("user", "ユーザ1", "備考")
|
132
|
+
glossary.add("user", "ユーザ2", "備考")
|
133
|
+
glossary.add("delete", "てすと1", "備考")
|
134
|
+
glossary.add("delete", "てすと2", "備考")
|
135
|
+
glossary.delete("delete", "", true)
|
136
|
+
glossary.index
|
137
|
+
@result = glossary.lookup("delete")
|
138
|
+
end
|
139
|
+
|
140
|
+
it {
|
141
|
+
-> { glossary.delete("user") }.should raise_error(Logaling::TermError)
|
142
|
+
}
|
143
|
+
|
144
|
+
it "should delete terms when force option is true" do
|
145
|
+
@result.should_not include({:name=>"spec", :source_language=>"en", :target_language=>"ja", :source_term=>"delete", :target_term=>"てすと1", :note=>"備考"})
|
146
|
+
@result.should_not include({:name=>"spec", :source_language=>"en", :target_language=>"ja", :source_term=>"delete", :target_term=>"てすと2", :note=>"備考"})
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
85
150
|
end
|
86
151
|
end
|
87
152
|
|
@@ -112,7 +177,7 @@ module Logaling
|
|
112
177
|
subject {glossary.lookup("user")}
|
113
178
|
|
114
179
|
it 'succeed at find by term' do
|
115
|
-
should be_include({:name=>"spec", :source_language=>"en", :target_language=>"ja", :source_term=>"user", :target_term=>"ユーザー", :note=>
|
180
|
+
should be_include({:name=>"spec", :source_language=>"en", :target_language=>"ja", :source_term=>"user", :target_term=>"ユーザー", :note=>''})
|
116
181
|
end
|
117
182
|
|
118
183
|
after do
|
@@ -157,7 +222,7 @@ module Logaling
|
|
157
222
|
subject { logaling_db.open(db_home, "utf8"){|db| logaling_db.lookup("user")} }
|
158
223
|
|
159
224
|
it 'glossaries should be indexed' do
|
160
|
-
should == [{:name=>"spec", :source_language=>"en", :target_language=>"ja", :source_term=>"user", :target_term=>"ユーザ", :note=>
|
225
|
+
should == [{:name=>"spec", :source_language=>"en", :target_language=>"ja", :source_term=>"user", :target_term=>"ユーザ", :note=>''}]
|
161
226
|
end
|
162
227
|
|
163
228
|
after do
|
@@ -176,7 +241,7 @@ module Logaling
|
|
176
241
|
subject { logaling_db.open(db_home, "utf8"){|db| logaling_db.lookup("test")} }
|
177
242
|
|
178
243
|
it 'glossaries should be indexed' do
|
179
|
-
should == [{:name=>"spec", :source_language=>"en", :target_language=>"ja", :source_term=>"test", :target_term=>"テスト", :note=>
|
244
|
+
should == [{:name=>"spec", :source_language=>"en", :target_language=>"ja", :source_term=>"test", :target_term=>"テスト", :note=>''}]
|
180
245
|
end
|
181
246
|
|
182
247
|
after do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: logaling-command
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,11 +13,11 @@ authors:
|
|
13
13
|
autorequire:
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
|
-
date: 2011-
|
16
|
+
date: 2011-12-06 00:00:00.000000000Z
|
17
17
|
dependencies:
|
18
18
|
- !ruby/object:Gem::Dependency
|
19
19
|
name: thor
|
20
|
-
requirement: &
|
20
|
+
requirement: &2152321920 !ruby/object:Gem::Requirement
|
21
21
|
none: false
|
22
22
|
requirements:
|
23
23
|
- - ! '>='
|
@@ -25,10 +25,10 @@ dependencies:
|
|
25
25
|
version: 0.14.6
|
26
26
|
type: :runtime
|
27
27
|
prerelease: false
|
28
|
-
version_requirements: *
|
28
|
+
version_requirements: *2152321920
|
29
29
|
- !ruby/object:Gem::Dependency
|
30
30
|
name: bundler
|
31
|
-
requirement: &
|
31
|
+
requirement: &2152321280 !ruby/object:Gem::Requirement
|
32
32
|
none: false
|
33
33
|
requirements:
|
34
34
|
- - ! '>='
|
@@ -36,10 +36,10 @@ dependencies:
|
|
36
36
|
version: '1.0'
|
37
37
|
type: :runtime
|
38
38
|
prerelease: false
|
39
|
-
version_requirements: *
|
39
|
+
version_requirements: *2152321280
|
40
40
|
- !ruby/object:Gem::Dependency
|
41
41
|
name: rake
|
42
|
-
requirement: &
|
42
|
+
requirement: &2152320420 !ruby/object:Gem::Requirement
|
43
43
|
none: false
|
44
44
|
requirements:
|
45
45
|
- - ! '>='
|
@@ -47,10 +47,10 @@ dependencies:
|
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
|
-
version_requirements: *
|
50
|
+
version_requirements: *2152320420
|
51
51
|
- !ruby/object:Gem::Dependency
|
52
52
|
name: rspec
|
53
|
-
requirement: &
|
53
|
+
requirement: &2152319460 !ruby/object:Gem::Requirement
|
54
54
|
none: false
|
55
55
|
requirements:
|
56
56
|
- - ! '>='
|
@@ -58,7 +58,7 @@ dependencies:
|
|
58
58
|
version: '0'
|
59
59
|
type: :development
|
60
60
|
prerelease: false
|
61
|
-
version_requirements: *
|
61
|
+
version_requirements: *2152319460
|
62
62
|
description: A command line interface for logaling.
|
63
63
|
email:
|
64
64
|
- koji.shimada@enishi-tech.com
|