logaling-command 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec CHANGED
@@ -1 +1,2 @@
1
1
  --color
2
+ --format documentation
data/Rakefile CHANGED
@@ -1,6 +1,7 @@
1
- require 'bundler/gem_tasks'
2
- require "rspec/core/rake_task"
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
3
 
4
+ require "rspec/core/rake_task"
4
5
  desc "Run all specs"
5
6
  RSpec::Core::RakeTask.new(:spec) do |t|
6
7
  t.verbose = true
data/lib/logaling.rb CHANGED
@@ -3,5 +3,7 @@
3
3
  require "logaling/command"
4
4
 
5
5
  module Logaling
6
- class CommandFailed < RuntimeError; end
6
+ class LogalingError < RuntimeError; end
7
+ class TermError < LogalingError; end
8
+ class CommandFailed < LogalingError; end
7
9
  end
@@ -5,7 +5,7 @@ require "logaling/glossary"
5
5
  require "logaling/glossary_db"
6
6
 
7
7
  class Logaling::Command < Thor
8
- VERSION = "0.0.1"
8
+ VERSION = "0.0.2"
9
9
 
10
10
  map '-c' => :create,
11
11
  '-a' => :add,
@@ -13,15 +13,10 @@ class Logaling::Command < Thor
13
13
  '-u' => :update,
14
14
  '-l' => :lookup
15
15
 
16
- class_option :glossary, type: :string, aliases: "-g"
17
- class_option :source_language, type: :string, aliases: "-S"
18
- class_option :target_language, type: :string, aliases: "-T"
19
- class_option :logaling_home, type: :string, required: false, aliases: "-h"
20
-
21
- def initialize(*args)
22
- super
23
- @dot_options = Hash.new
24
- end
16
+ class_option "glossary", type: :string, aliases: "-g"
17
+ class_option "source-language", type: :string, aliases: "-S"
18
+ class_option "target-language", type: :string, aliases: "-T"
19
+ class_option "logaling-home", type: :string, required: false, aliases: "-h"
25
20
 
26
21
  desc 'create', 'Create glossary.'
27
22
  def create
@@ -31,52 +26,42 @@ class Logaling::Command < Thor
31
26
  error(e.message)
32
27
  end
33
28
 
34
- desc 'add', 'Add term to glossary.'
35
- method_option :source_term, type: :string, required: true, aliases: "-s"
36
- method_option :target_term, type: :string, required: true, aliases: "-t"
37
- method_option :note, type: :string, aliases: "-n"
38
- def add
29
+ desc 'add [source term] [target term] [note]', 'Add term to glossary.'
30
+ def add(source_term, target_term, note='')
39
31
  load_config
40
- glossary.add(options[:source_term], options[:target_term], options[:note])
41
- rescue Logaling::CommandFailed => e
32
+ glossary.add(source_term, target_term, note)
33
+ rescue Logaling::CommandFailed, Logaling::TermError => e
42
34
  error(e.message)
43
35
  end
44
36
 
45
- desc 'delete', 'Delete term.'
46
- method_option :source_term, type: :string, required: true, aliases: "-s"
47
- method_option :target_term, type: :string, required: true, aliases: "-t"
48
- def delete
37
+ desc 'delete [source term] [target term]', 'Delete term.'
38
+ def delete(source_term, target_term)
49
39
  load_config
50
- glossary.delete(options[:source_term], options[:target_term])
51
- rescue Logaling::CommandFailed => e
40
+ glossary.delete(source_term, target_term)
41
+ rescue Logaling::CommandFailed, Logaling::TermError => e
52
42
  error(e.message)
53
43
  end
54
44
 
55
- desc 'update', 'Update term.'
56
- method_option :source_term, type: :string, required: true, aliases: "-s"
57
- method_option :target_term, type: :string, required: true, aliases: "-t"
58
- method_option :new_target_term, type: :string, required: true, aliases: "-nt"
59
- method_option :note, type: :string, required: true, aliases: "-n"
60
- def update
45
+ desc 'update [source term] [target term] [new_target_term], [note]', 'Update term.'
46
+ def update(source_term, target_term, new_target_term, note='')
61
47
  load_config
62
- glossary.update(options[:source_term], options[:target_term], options[:new_target_term], options[:note])
63
- rescue Logaling::CommandFailed => e
48
+ glossary.update(source_term, target_term, new_target_term, note)
49
+ rescue Logaling::CommandFailed, Logaling::TermError => e
64
50
  error(e.message)
65
51
  end
66
52
 
67
- desc 'lookup', 'Lookup terms.'
68
- method_option :source_term, type: :string, required: true, aliases: "-s"
69
- def lookup
53
+ desc 'lookup [source term]', 'Lookup terms.'
54
+ def lookup(source_term)
70
55
  load_config
71
- glossary.lookup(options[:source_term])
72
- rescue Logaling::CommandFailed => e
56
+ glossary.lookup(source_term)
57
+ rescue Logaling::CommandFailed, Logaling::TermError => e
73
58
  error(e.message)
74
59
  end
75
60
 
76
61
  desc 'index', 'Index glossaries to groonga DB.'
77
62
  def index
78
63
  load_config
79
- home = find_option("logaling_home") || LOGALING_HOME
64
+ home = find_option("logaling-home") || LOGALING_HOME
80
65
  db_home = File.join(home, ".logadb")
81
66
  glossarydb = Logaling::GlossaryDB.new
82
67
  glossarydb.open(db_home, "utf8") do |db|
@@ -90,13 +75,13 @@ class Logaling::Command < Thor
90
75
  glossary = find_option("glossary")
91
76
  raise(Logaling::CommandFailed, "input glossary name '-g <glossary name>'") unless glossary
92
77
 
93
- source_language = find_option("source_language")
78
+ source_language = find_option("source-language")
94
79
  raise(Logaling::CommandFailed, "input source-language code '-S <source-language code>'") unless source_language
95
80
 
96
- target_language = find_option("target_language")
81
+ target_language = find_option("target-language")
97
82
  raise(Logaling::CommandFailed, "input target-language code '-T <target-language code>'") unless target_language
98
83
 
99
- logaling_home = find_option("logaling_home")
84
+ logaling_home = find_option("logaling-home")
100
85
 
101
86
  Logaling::Glossary.new(glossary, source_language, target_language, logaling_home)
102
87
  end
@@ -111,6 +96,7 @@ class Logaling::Command < Thor
111
96
  end
112
97
 
113
98
  def load_config
99
+ @dot_options ||= {}
114
100
  if path = find_dotfile
115
101
  tmp_options = File.readlines(path).map {|l| l.chomp.split " "}
116
102
  tmp_options.each do |option|
@@ -38,8 +38,7 @@ module Logaling
38
38
  check_glossary_exists
39
39
 
40
40
  if bilingual_pair_exists?(source_term, target_term)
41
- puts "[#{source_term}] [#{target_term}] pair already exists"
42
- return
41
+ raise TermError, "[#{source_term}] [#{target_term}] pair already exists"
43
42
  end
44
43
 
45
44
  glossary = load_glossary_yml
@@ -52,8 +51,7 @@ module Logaling
52
51
  check_glossary_exists
53
52
 
54
53
  if bilingual_pair_exists?(source_term, new_target_term)
55
- puts "[#{source_term}] [#{new_target_term}] pair already exists"
56
- return
54
+ raise TermError, "[#{source_term}] [#{new_target_term}] pair already exists"
57
55
  end
58
56
 
59
57
  glossary = load_glossary_yml
@@ -62,7 +60,7 @@ module Logaling
62
60
  glossary[target_index] = rebuild_term(glossary[target_index], source_term, new_target_term, note)
63
61
  dump_glossary(glossary)
64
62
  else
65
- puts "source_term:#{source_term} target_term:#{target_term} not found in glossary #{@path}"
63
+ raise TermError, "source_term:#{source_term} target_term:#{target_term} not found in glossary #{@path}"
66
64
  end
67
65
  end
68
66
 
@@ -75,7 +73,7 @@ module Logaling
75
73
  glossary.delete_at(target_index)
76
74
  dump_glossary(glossary)
77
75
  else
78
- puts "source_term:#{source_term} target_term:#{target_term} not found in glossary #{@path}"
76
+ raise TermError, "source_term:#{source_term} target_term:#{target_term} not found in glossary #{@path}"
79
77
  end
80
78
  end
81
79
 
@@ -89,8 +87,7 @@ module Logaling
89
87
  term[:source_language] != @source_language || term[:target_language] != @target_language
90
88
  end
91
89
  if glossaries.empty?
92
- puts "source_term <#{source_term}> not found"
93
- return
90
+ raise TermError, "source-term <#{source_term}> not found"
94
91
  end
95
92
  # order by glossary
96
93
  specified = glossaries.select{|term| term[:name] == @glossary}
@@ -117,11 +114,12 @@ module Logaling
117
114
  end
118
115
 
119
116
  def build_term(source_term, target_term, note)
117
+ note ||= ''
120
118
  {'source_term' => source_term, 'target_term' => target_term, 'note' => note}
121
119
  end
122
120
 
123
121
  def rebuild_term(current, source_term, target_term, note)
124
- note = current['note'] if note == ""
122
+ note = current['note'] if note.nil? || note == ""
125
123
  target_term = current['target_term'] if target_term == ""
126
124
  build_term(source_term, target_term, note)
127
125
  end
@@ -1,6 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
- require "logaling/command.rb"
3
+ require "logaling/command"
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "logaling-command"
@@ -18,6 +18,9 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
+ s.add_runtime_dependency 'thor', ['>= 0.14.6']
22
+ s.add_runtime_dependency 'bundler', ['>= 1.0']
23
+
21
24
  s.add_development_dependency 'rake'
22
25
  s.add_development_dependency 'rspec'
23
26
  end
@@ -0,0 +1,101 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(__FILE__), "..", "spec_helper")
3
+
4
+ describe Logaling::Command do
5
+ let(:base_options) { {"glossary"=>"spec", "source-language"=>"en", "target-language"=>"ja"} }
6
+ let(:command) { Logaling::Command.new([], base_options) }
7
+ let(:glossary_path) { File.join(LOGALING_HOME, "/spec.en.ja.yml") }
8
+
9
+ before do
10
+ FileUtils.remove_file(glossary_path, true)
11
+ end
12
+
13
+ describe '#create' do
14
+ context 'with arguments show non-existent glossary' do
15
+ before do
16
+ command.create
17
+ end
18
+
19
+ it "glossary yaml should be newly created" do
20
+ File.exists?(glossary_path).should be_true
21
+ end
22
+ end
23
+ end
24
+
25
+ describe '#add' do
26
+ before do
27
+ command.create
28
+ end
29
+
30
+ context 'with arguments have only bilingual pair' do
31
+ before do
32
+ command.add("spec", "テスト")
33
+ end
34
+
35
+ subject { YAML::load_file(glossary_path).find{|h| h["source_term"] == "spec" }}
36
+
37
+ it "glossary yaml should contain that term" do
38
+ subject["target_term"].should == "テスト"
39
+ end
40
+
41
+ it "term should note have note" do
42
+ subject["note"].should == ""
43
+ end
44
+ end
45
+
46
+ context 'with arguments have bilingual pair and note' do
47
+ before do
48
+ command.add("spec", "テスト", "備考")
49
+ end
50
+
51
+ subject { YAML::load_file(glossary_path).find{|h| h["source_term"] == "spec" }}
52
+
53
+ it "glossary yaml should contain that term" do
54
+ subject["target_term"].should == "テスト"
55
+ end
56
+
57
+ it "term should have note" do
58
+ subject["note"].should == "備考"
59
+ end
60
+ end
61
+ end
62
+
63
+ describe "#update" do
64
+ before do
65
+ command.create
66
+ command.add("spec", "テスト", "備考")
67
+ end
68
+
69
+ context "not given source-term option" do
70
+ # should show err
71
+ end
72
+
73
+ context "not given target-term option" do
74
+ #should show err
75
+ end
76
+
77
+ context "not given new-target-term option" do
78
+ #should show err
79
+ end
80
+
81
+ context "with arguments except note" do
82
+ before do
83
+ command.update("spec", "テスト", "スペック")
84
+ end
85
+
86
+ subject { YAML::load_file(glossary_path).find{|h| h["source_term"] == "spec" }}
87
+
88
+ it "term's target_term should be updated" do
89
+ subject["target_term"].should == "スペック"
90
+ end
91
+
92
+ it "term's note should not be updated" do
93
+ subject["note"].should == "備考"
94
+ end
95
+ end
96
+ end
97
+
98
+ after do
99
+ FileUtils.remove_file(glossary_path, true)
100
+ end
101
+ end
@@ -0,0 +1,116 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(__FILE__), "..", "spec_helper")
3
+ require "fileutils"
4
+
5
+ module Logaling
6
+ describe Glossary do
7
+ let(:glossary) { Glossary.new('spec', 'en', 'ja', Dir.pwd) }
8
+ let(:glossary_path) { File.join(Dir.pwd, 'spec.en.ja.yml') }
9
+
10
+ describe '#add' do
11
+ before do
12
+ FileUtils.remove_file(glossary_path, true)
13
+ glossary.create
14
+ end
15
+
16
+ context 'with arguments show new bilingual pair' do
17
+ before do
18
+ glossary.add("spec", "スペック", "テストスペック")
19
+ end
20
+
21
+ it 'glossary yaml should have that bilingual pair' do
22
+ yaml = YAML::load_file(glossary_path)
23
+ term = yaml.index({"source_term"=>"spec", "target_term"=>"スペック", "note"=>"テストスペック"})
24
+ term.should_not be_nil
25
+ end
26
+ end
27
+
28
+ context 'with arguments show existing bilingual pair' do
29
+ before do
30
+ glossary.add("user", "ユーザ", "ユーザーではない")
31
+ end
32
+
33
+ it {
34
+ -> { glossary.add("user", "ユーザ", "ユーザーではない") }.should raise_error(Logaling::TermError)
35
+ }
36
+ end
37
+
38
+ after do
39
+ FileUtils.remove_file(glossary_path, true)
40
+ end
41
+ end
42
+
43
+ describe '#update' do
44
+ before do
45
+ FileUtils.remove_file(glossary_path, true)
46
+ glossary.create
47
+ glossary.add("user", "ユーザ", "ユーザーではない")
48
+ end
49
+
50
+ context 'with new-terget-term show existing bilingual pair' do
51
+ it {
52
+ -> { glossary.update("user", "ユーザー", "ユーザ", "やっぱりユーザー") }.should raise_error(Logaling::TermError)
53
+ }
54
+ end
55
+
56
+ context 'with source-term arguments show not existing bilingual pair' do
57
+ it {
58
+ -> { glossary.update("use", "ユーザ", "ユーザー", "やっぱりユーザー") }.should raise_error(Logaling::TermError)
59
+ }
60
+ end
61
+
62
+ context 'with target-term arguments show not existing bilingual pair' do
63
+ it {
64
+ -> { glossary.update("user", "ユー", "ユーザー", "やっぱりユーザー") }.should raise_error(Logaling::TermError)
65
+ }
66
+ end
67
+
68
+ after do
69
+ FileUtils.remove_file(glossary_path, true)
70
+ end
71
+ end
72
+
73
+ describe '#delete' do
74
+ before do
75
+ FileUtils.remove_file(glossary_path, true)
76
+ glossary.create
77
+ glossary.add("user", "ユーザ", "ユーザーではない")
78
+ end
79
+
80
+ context 'with arguments show not existing bilingual pair' do
81
+ it {
82
+ -> { glossary.delete("user", "ユーザー") }.should raise_error(Logaling::TermError)
83
+ }
84
+ end
85
+
86
+ after do
87
+ FileUtils.remove_file(glossary_path, true)
88
+ end
89
+ end
90
+
91
+ describe '#lookup' do
92
+ before do
93
+ FileUtils.remove_file(glossary_path, true)
94
+ glossary.create
95
+ glossary.add("user", "ユーザ", "ユーザーではない")
96
+
97
+ db_home = File.join(LOGALING_HOME, ".logadb")
98
+ glossarydb = Logaling::GlossaryDB.new
99
+ glossarydb.open(db_home, "utf8") do |db|
100
+ db.recreate_table(db_home)
101
+ db.load_glossaries(LOGALING_HOME)
102
+ end
103
+ end
104
+
105
+ context 'with arguments show not existing bilingual pair' do
106
+ it {
107
+ -> { glossary.delete("user", "ユーザー") }.should raise_error(Logaling::TermError)
108
+ }
109
+ end
110
+
111
+ after do
112
+ FileUtils.remove_file(glossary_path, true)
113
+ end
114
+ end
115
+ end
116
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,25 @@
1
- require "rubygems"
2
- require "bundler/setup"
3
-
4
- require 'rspec'
5
1
  $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
2
+ require 'logaling'
3
+
4
+ require "fileutils"
5
+ require 'stringio'
6
+
7
+ LOGALING_HOME = File.expand_path("~/.logaling.d")
8
+
9
+ RSpec.configure do |config|
10
+ def capture(stream)
11
+ begin
12
+ stream = stream.to_s
13
+ eval "$#{stream} = StringIO.new"
14
+ yield
15
+ result = eval("$#{stream}").string
16
+ ensure
17
+ eval("$#{stream} = #{stream.upcase}")
18
+ end
19
+
20
+ result
21
+ end
6
22
 
7
- require 'logaling_command'
23
+ alias :silence :capture
24
+ end
8
25
 
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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,11 +13,33 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2011-11-09 00:00:00.000000000Z
16
+ date: 2011-11-16 00:00:00.000000000Z
17
17
  dependencies:
18
+ - !ruby/object:Gem::Dependency
19
+ name: thor
20
+ requirement: &2152051920 !ruby/object:Gem::Requirement
21
+ none: false
22
+ requirements:
23
+ - - ! '>='
24
+ - !ruby/object:Gem::Version
25
+ version: 0.14.6
26
+ type: :runtime
27
+ prerelease: false
28
+ version_requirements: *2152051920
29
+ - !ruby/object:Gem::Dependency
30
+ name: bundler
31
+ requirement: &2152050600 !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '1.0'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: *2152050600
18
40
  - !ruby/object:Gem::Dependency
19
41
  name: rake
20
- requirement: &2151902900 !ruby/object:Gem::Requirement
42
+ requirement: &2152049620 !ruby/object:Gem::Requirement
21
43
  none: false
22
44
  requirements:
23
45
  - - ! '>='
@@ -25,10 +47,10 @@ dependencies:
25
47
  version: '0'
26
48
  type: :development
27
49
  prerelease: false
28
- version_requirements: *2151902900
50
+ version_requirements: *2152049620
29
51
  - !ruby/object:Gem::Dependency
30
52
  name: rspec
31
- requirement: &2151901960 !ruby/object:Gem::Requirement
53
+ requirement: &2152047900 !ruby/object:Gem::Requirement
32
54
  none: false
33
55
  requirements:
34
56
  - - ! '>='
@@ -36,7 +58,7 @@ dependencies:
36
58
  version: '0'
37
59
  type: :development
38
60
  prerelease: false
39
- version_requirements: *2151901960
61
+ version_requirements: *2152047900
40
62
  description: A command line interface for logaling.
41
63
  email:
42
64
  - koji.shimada@enishi-tech.com
@@ -59,6 +81,8 @@ files:
59
81
  - lib/logaling/glossary.rb
60
82
  - lib/logaling/glossary_db.rb
61
83
  - logaling-command.gemspec
84
+ - spec/logaling/command_spec.rb
85
+ - spec/logaling/glossary_spec.rb
62
86
  - spec/spec_helper.rb
63
87
  homepage: http://logaling.github.com/
64
88
  licenses: []
@@ -85,4 +109,6 @@ signing_key:
85
109
  specification_version: 3
86
110
  summary: A command line interface for logaling.
87
111
  test_files:
112
+ - spec/logaling/command_spec.rb
113
+ - spec/logaling/glossary_spec.rb
88
114
  - spec/spec_helper.rb