a-b-chi 1.2.4 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +6 -14
- data/README.md +27 -8
- data/lib/a-b-chi.rb +31 -12
- data/lib/bopomofo.txt +15671 -0
- data/lib/characters.txt +7764 -43
- data/lib/chinese.rb +46 -0
- data/spec/a-b-chi_spec.rb +46 -0
- data/test/test_a-b-chi.rb +14 -0
- metadata +12 -9
data/lib/chinese.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
module Chinese
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
module ClassMethods
|
5
|
+
|
6
|
+
def sort_chi(*fields)
|
7
|
+
#this just gathers up the characters into an array -> @characters
|
8
|
+
spec = Gem::Specification.find_by_name("a-b-chi")
|
9
|
+
gem_root = spec.gem_dir
|
10
|
+
@gem_lib = gem_root + "/lib"
|
11
|
+
@characters = File.read("#{@gem_lib}/characters.txt").split("\n")
|
12
|
+
#this section compiles the sort_query by checking the max length of the content of each of the supplied fields
|
13
|
+
sort_query = '['
|
14
|
+
fields.each do |field|
|
15
|
+
puts "#{field} - " + self.maximum("#{field}").length.to_s
|
16
|
+
(0..(self.maximum("#{field}").length-1)).each { |i| sort_query << " @characters.index(sort_string.#{field}[#{i}]),"}
|
17
|
+
end
|
18
|
+
sort_query.chop!
|
19
|
+
sort_query << ']'
|
20
|
+
#this should sort the collection (and does when run outside of this module but within the module i get the no method error for sort_by)
|
21
|
+
self.all.sort_by { |sort_string| eval(sort_query) }
|
22
|
+
end
|
23
|
+
|
24
|
+
def sort_chi!(*fields)
|
25
|
+
#this just gathers up the characters into an array -> @characters
|
26
|
+
spec = Gem::Specification.find_by_name("a-b-chi")
|
27
|
+
gem_root = spec.gem_dir
|
28
|
+
@gem_lib = gem_root + "/lib"
|
29
|
+
@characters = File.read("#{@gem_lib}/characters.txt").split("\n")
|
30
|
+
#this section compiles the sort_query by checking the max length of the content of each of the supplied fields
|
31
|
+
sort_query = '['
|
32
|
+
fields.each do |field|
|
33
|
+
puts "#{field} - " + self.maximum("#{field}").length.to_s
|
34
|
+
(0..(self.maximum("#{field}").length-1)).each { |i| sort_query << " @characters.index(sort_string.#{field}[#{i}]),"}
|
35
|
+
end
|
36
|
+
sort_query.chop!
|
37
|
+
sort_query << ']'
|
38
|
+
#this should sort the collection (and does when run outside of this module but within the module i get the no method error for sort_by)
|
39
|
+
self.all.sort_by! { |sort_string| eval(sort_query) }
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
#goddam!
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'a-b-chi'
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
describe ArrayChinese do
|
6
|
+
it "should sort an array correctly" do
|
7
|
+
arr = ArrayChinese.new
|
8
|
+
arr.push '項小珍','李瑞雰','李瑞芳','李則欣','李宗翰','李彩雲','李璨亘','李素霞','李依帆','李若喬','李依恬'
|
9
|
+
arr.sort.should eq(["李若喬", "李瑞雰", "李瑞芳", "李則欣", "李宗翰", "李彩雲", "李璨亘", "李素霞", "李依帆", "李依恬", "項小珍"])
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns an empty array if one is passed to it" do
|
13
|
+
arr = ArrayChinese.new
|
14
|
+
arr.push
|
15
|
+
arr.sort.should eq([])
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should sort english after chinese" do
|
19
|
+
arr = ArrayChinese.new
|
20
|
+
arr.push "雲Z雲", "NallaN", "雲彩雲", "Z雲雲", "雲雲Z", "ZZ雲", "雲ZZ"
|
21
|
+
arr.sort.should eq(["雲彩雲", "雲雲Z", "雲Z雲", "雲ZZ", "NallaN", "Z雲雲", "ZZ雲"])
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should sort unknown characters after chinese and english" do
|
25
|
+
arr = ArrayChinese.new
|
26
|
+
arr.push "Z∂ß", "NallaN", "雲彩雲", " "
|
27
|
+
arr.sort.should eq(["雲彩雲", "NallaN", "Z∂ß", " "])
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
describe StringChinese do
|
33
|
+
|
34
|
+
it "should return correct non-markup Characters and BoPoMoFo" do
|
35
|
+
str = StringChinese.new
|
36
|
+
str << "珍項頇芳"
|
37
|
+
str.to_ruby_unmarked.should eq("珍(ㄓㄢ)項(ㄩㄣˇ)頇(ㄓㄨˋ)芳(ㄐㄧˊ)")
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should return correctly marked up Characters and BoPoMoFo" do
|
41
|
+
str = StringChinese.new
|
42
|
+
str << "珍項頇芳"
|
43
|
+
str.to_ruby_markup.should eq("<ruby><rb>珍</rb><rp>(</rp><rt>ㄓㄢ</rt><rp>)</rp><rb>項</rb><rp>(</rp><rt>ㄩㄣˇ</rt><rp>)</rp><rb>頇</rb><rp>(</rp><rt>ㄓㄨˋ</rt><rp>)</rp><rb>芳</rb><rp>(</rp><rt>ㄐㄧˊ</rt><rp>)</rp></ruby>")
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
data/test/test_a-b-chi.rb
CHANGED
@@ -32,4 +32,18 @@ class ABChiTest < Test::Unit::TestCase
|
|
32
32
|
arr.sort
|
33
33
|
end
|
34
34
|
|
35
|
+
def test_return_of_correct_non_markup
|
36
|
+
str = StringChinese.new
|
37
|
+
str << "珍項頇芳"
|
38
|
+
assert_equal "珍(ㄓㄢ)項(ㄩㄣˇ)頇(ㄓㄨˋ)芳(ㄐㄧˊ)",
|
39
|
+
str.to_ruby_unmarked
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_return_of_correct_markup
|
43
|
+
str = StringChinese.new
|
44
|
+
str << "珍項頇芳"
|
45
|
+
assert_equal "<ruby><rb>珍</rb><rp>(</rp><rt>ㄓㄢ</rt><rp>)</rp><rb>項</rb><rp>(</rp><rt>ㄩㄣˇ</rt><rp>)</rp><rb>頇</rb><rp>(</rp><rt>ㄓㄨˋ</rt><rp>)</rp><rb>芳</rb><rp>(</rp><rt>ㄐㄧˊ</rt><rp>)</rp></ruby>",
|
46
|
+
str.to_ruby_markup
|
47
|
+
end
|
48
|
+
|
35
49
|
end
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: a-b-chi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathanial Allan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-08-
|
11
|
+
date: 2013-08-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: A gem that
|
14
|
-
|
13
|
+
description: A convenient gem that will allow you to sort arrays and activerecord
|
14
|
+
relations without needing to mess with set-months-ago database collation settings.
|
15
15
|
email: nathanial.allan@gmail.com
|
16
16
|
executables: []
|
17
17
|
extensions: []
|
@@ -20,30 +20,33 @@ files:
|
|
20
20
|
- Rakefile
|
21
21
|
- README.md
|
22
22
|
- lib/a-b-chi.rb
|
23
|
+
- lib/chinese.rb
|
23
24
|
- lib/characters.txt
|
25
|
+
- lib/bopomofo.txt
|
24
26
|
- test/test_a-b-chi.rb
|
27
|
+
- spec/a-b-chi_spec.rb
|
25
28
|
homepage: http://rubygems.org/gems/a-b-chi
|
26
29
|
licenses:
|
27
30
|
- MIT
|
28
31
|
metadata: {}
|
29
|
-
post_install_message:
|
32
|
+
post_install_message:
|
30
33
|
rdoc_options: []
|
31
34
|
require_paths:
|
32
35
|
- lib
|
33
36
|
required_ruby_version: !ruby/object:Gem::Requirement
|
34
37
|
requirements:
|
35
|
-
- -
|
38
|
+
- - '>='
|
36
39
|
- !ruby/object:Gem::Version
|
37
40
|
version: '0'
|
38
41
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
42
|
requirements:
|
40
|
-
- -
|
43
|
+
- - '>='
|
41
44
|
- !ruby/object:Gem::Version
|
42
45
|
version: '0'
|
43
46
|
requirements: []
|
44
47
|
rubyforge_project:
|
45
|
-
rubygems_version: 2.0.
|
48
|
+
rubygems_version: 2.0.6
|
46
49
|
signing_key:
|
47
50
|
specification_version: 4
|
48
|
-
summary: Chinese Traditional
|
51
|
+
summary: Chinese Traditional Sorter and Manipulator
|
49
52
|
test_files: []
|