sort-by-alphabet 0.1.6 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.textile +41 -1
- data/init.rb +2 -1
- data/lib/alphabets.rb +22 -35
- data/lib/enumerable.rb +2 -10
- data/lib/object.rb +15 -0
- data/lib/sort_by_alphabet.rb +5 -15
- metadata +3 -2
data/README.textile
CHANGED
@@ -1 +1,41 @@
|
|
1
|
-
|
1
|
+
This library implements alphabetical sorting according to given alphabet scheme. Currently, only Czech alphabet is included, but it is super-easy to create a new one.
|
2
|
+
|
3
|
+
h1. Setup
|
4
|
+
|
5
|
+
Install the gem:
|
6
|
+
|
7
|
+
bc. gem install sort-by-alphabet
|
8
|
+
|
9
|
+
Or require it into Rails:
|
10
|
+
|
11
|
+
bc. gem require "sort-by-alphabet"
|
12
|
+
|
13
|
+
h1. Requirements
|
14
|
+
|
15
|
+
Support of @mb_chars@, i.e. _ActiveSupport_ in practice.
|
16
|
+
|
17
|
+
h1. Usage
|
18
|
+
|
19
|
+
Classical sort puts accented chars after normal:
|
20
|
+
|
21
|
+
bc. %w{nazdar ahoj čau}.sort # => %w{ahoj nazdar čau}
|
22
|
+
|
23
|
+
@sort_by_alphabet@ does a better job:
|
24
|
+
|
25
|
+
bc. %w{nazdar ahoj čau}.sort_by_alphabet(CzechAlphabet) # => %w{ahoj čau nazdar}
|
26
|
+
|
27
|
+
@sort_by_alphabet@ can take a block which acts similarly as that in @sort_by@:
|
28
|
+
|
29
|
+
bc. Something.all.sort_by_alphabet(CzechAlphabet) {|o| o.title}
|
30
|
+
|
31
|
+
You can also make a class alphabetically comparable:
|
32
|
+
|
33
|
+
bc. class Something
|
34
|
+
comparable_by_alphabet(CzechAlphabet)
|
35
|
+
end
|
36
|
+
|
37
|
+
or use a block, e.g. for case insensitivity:
|
38
|
+
|
39
|
+
bc. class SomethingElse
|
40
|
+
comparable_by_alphabet(CzechAlphabet) {|obj| obj.mb_chars.downcase}
|
41
|
+
end
|
data/init.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "#{File::dirname __FILE__}/lib/alphabets"
|
2
2
|
require "#{File::dirname __FILE__}/lib/enumerable"
|
3
|
+
require "#{File::dirname __FILE__}/lib/object"
|
3
4
|
require "#{File::dirname __FILE__}/lib/sort_by_alphabet"
|
5
|
+
|
4
6
|
String.send(:include, SortByAlphabet::InstanceMethods)
|
5
|
-
String.send(:extend, SortByAlphabet::ClassMethods)
|
data/lib/alphabets.rb
CHANGED
@@ -1,41 +1,24 @@
|
|
1
1
|
class Alphabet
|
2
2
|
|
3
|
-
def self.
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
if !i1 && !i2 or !j1 && !j2
|
9
|
-
x <=> y
|
10
|
-
elsif i1 && j2
|
11
|
-
-1
|
12
|
-
elsif i2 && j1
|
13
|
-
+1
|
14
|
-
elsif i1 && j1
|
15
|
-
i1 <=> j1
|
16
|
-
elsif i2 && j2
|
17
|
-
i2 <=> j2
|
3
|
+
def self.compare_letters(x, y)
|
4
|
+
i = letters.mb_chars.index(x)
|
5
|
+
j = letters.mb_chars.index(y)
|
6
|
+
if i && j
|
7
|
+
i <=> j
|
18
8
|
else
|
19
|
-
raise "I did not think of something"
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.compare_letters_insensitive(x, y)
|
24
|
-
i = lower_case.mb_chars.index(letter_to_lowercase(x))
|
25
|
-
j = lower_case.mb_chars.index(letter_to_lowercase(y))
|
26
|
-
if !i || !j
|
27
9
|
x <=> y
|
28
|
-
else
|
29
|
-
i <=> j
|
30
10
|
end
|
31
11
|
end
|
32
12
|
|
33
|
-
def self.
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
13
|
+
def self.by_locale(locale)
|
14
|
+
language = locale.split('-').first.downcase
|
15
|
+
case language
|
16
|
+
when 'cs'
|
17
|
+
CzechAlphabet
|
18
|
+
when 'en'
|
19
|
+
EnglishAlphabet
|
20
|
+
else
|
21
|
+
raise "Language #{language} of locale #{locale} is unknown."
|
39
22
|
end
|
40
23
|
end
|
41
24
|
|
@@ -43,12 +26,16 @@ end
|
|
43
26
|
|
44
27
|
class CzechAlphabet < Alphabet
|
45
28
|
|
46
|
-
def self.
|
47
|
-
"
|
29
|
+
def self.letters
|
30
|
+
"AÁBCČDĎEÉĚFGHIÍJKLMNŇOÓPQRŘSŠTŤUÚŮVWXYÝZŽaábcčdďeéěfghiíjklmnňoópqrřsštťuúůvwxyýzž"
|
48
31
|
end
|
49
32
|
|
50
|
-
|
51
|
-
|
33
|
+
end
|
34
|
+
|
35
|
+
class EnglishAlphabet < Alphabet
|
36
|
+
|
37
|
+
def self.letters
|
38
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklnopqrstuvwxyz"
|
52
39
|
end
|
53
40
|
|
54
41
|
end
|
data/lib/enumerable.rb
CHANGED
@@ -2,17 +2,9 @@ module Enumerable
|
|
2
2
|
|
3
3
|
def sort_by_alphabet(alphabet)
|
4
4
|
if block_given?
|
5
|
-
sort{|a, b| yield(a).to_s.
|
5
|
+
sort{|a, b| yield(a).to_s.compare_by_alphabet(yield(b).to_s, alphabet)}
|
6
6
|
else
|
7
|
-
sort{|a, b| a.to_s.
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
def isort_by_alphabet(alphabet)
|
12
|
-
if block_given?
|
13
|
-
sort{|a, b| yield(a).to_s.compare_by_alphabet_insensitive(yield(b).to_s, alphabet)}
|
14
|
-
else
|
15
|
-
sort{|a, b| a.to_s.compare_by_alphabet_insensitive(b.to_s, alphabet)}
|
7
|
+
sort{|a, b| a.to_s.compare_by_alphabet(b.to_s, alphabet)}
|
16
8
|
end
|
17
9
|
end
|
18
10
|
|
data/lib/object.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
class Object
|
2
|
+
|
3
|
+
def self.comparable_by_alphabet(alphabet, &block)
|
4
|
+
if block
|
5
|
+
define_method :"<=>" do |other|
|
6
|
+
block[self].to_s.compare_by_alphabet(block[other].to_s, alphabet)
|
7
|
+
end
|
8
|
+
else
|
9
|
+
define_method :"<=>" do |other|
|
10
|
+
self.to_s.compare_by_alphabet(other.to_s, alphabet)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
data/lib/sort_by_alphabet.rb
CHANGED
@@ -1,27 +1,17 @@
|
|
1
1
|
module SortByAlphabet
|
2
|
-
|
3
|
-
module ClassMethods
|
4
|
-
|
5
|
-
end
|
6
|
-
|
7
|
-
module InstanceMethods
|
8
|
-
def compare_by_alphabet_insensitive(other, alphabet)
|
9
|
-
compare_by_alphabet(other, alphabet, :compare_letters_insensitive)
|
10
|
-
end
|
11
2
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
def compare_by_alphabet(other, alphabet, method)
|
3
|
+
module InstanceMethods
|
4
|
+
|
5
|
+
def compare_by_alphabet(other, alphabet)
|
17
6
|
l1 = self.mb_chars.length
|
18
7
|
l2 = other.mb_chars.length
|
19
8
|
(0...[l1, l2].min).each do |i|
|
20
|
-
c = alphabet.
|
9
|
+
c = alphabet.compare_letters(self.mb_chars[i..i], other.mb_chars[i..i])
|
21
10
|
return c unless c == 0
|
22
11
|
end
|
23
12
|
l1 <=> l2
|
24
13
|
end
|
14
|
+
|
25
15
|
end
|
26
16
|
|
27
17
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sort-by-alphabet
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Frantisek Havluj
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-01-
|
12
|
+
date: 2010-01-22 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -24,6 +24,7 @@ extra_rdoc_files: []
|
|
24
24
|
files:
|
25
25
|
- lib/alphabets.rb
|
26
26
|
- lib/enumerable.rb
|
27
|
+
- lib/object.rb
|
27
28
|
- lib/sort-by-alphabet.rb
|
28
29
|
- lib/sort_by_alphabet.rb
|
29
30
|
- init.rb
|