sort-by-alphabet 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,2 @@
1
+ 0.1.0 2010-01-12
2
+ intial release
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Frantisek Havluj
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1 @@
1
+ bc. %w{ahoj čau nazdar}.sort_by_alphabet(CzechAlphabet)
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ String.send(:include, SortByAlphabet::InstanceMethods)
2
+ String.send(:extend, SortByAlphabet::ClassMethods)
data/lib/alphabets.rb ADDED
@@ -0,0 +1,54 @@
1
+ class Alphabet
2
+
3
+ def self.compare_letters_sensitive(x, y)
4
+ i1 = lower_case.index(x)
5
+ i2 = upper_case.index(x)
6
+ j1 = lower_case.index(y)
7
+ j2 = upper_case.index(y)
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
18
+ 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.index(letter_to_lowercase(x))
25
+ j = lower_case.index(letter_to_lowercase(y))
26
+ if !i || !j
27
+ x <=> y
28
+ else
29
+ i <=> j
30
+ end
31
+ end
32
+
33
+ def self.letter_to_lowercase(a)
34
+ i = upper_case.index(a)
35
+ if i
36
+ lower_case[i]
37
+ else
38
+ a
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+ class CzechAlphabet < Alphabet
45
+
46
+ def self.lower_case
47
+ "aábcčdďeéěfghiíjklmnňoópqrřsštťuúůvwxyýzž"
48
+ end
49
+
50
+ def self.upper_case
51
+ "AÁBCČDĎEÉĚFGHIÍJKLMNŇOÓPQRŘSŠTŤUÚŮVWXYÝZŽ"
52
+ end
53
+
54
+ end
data/lib/enumerable.rb ADDED
@@ -0,0 +1,11 @@
1
+ module Enumerable
2
+
3
+ def sort_by_alphabet(alphabet)
4
+ sort{|a, b| a.compare_by_alphabet_sensitive(b, alphabet)}
5
+ end
6
+
7
+ def isort_by_alphabet(alphabet)
8
+ sort{|a, b| a.compare_by_alphabet_insensitive(b, alphabet)}
9
+ end
10
+
11
+ end
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/../init.rb'
@@ -0,0 +1,27 @@
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
+
12
+ def compare_by_alphabet_sensitive(other, alphabet)
13
+ compare_by_alphabet(other, alphabet, :compare_letters_sensitive)
14
+ end
15
+
16
+ def compare_by_alphabet(other, alphabet, method)
17
+ l1 = self.mb_chars.length
18
+ l2 = other.mb_chars.length
19
+ (0...[l1, l2].min).each do |i|
20
+ c = alphabet.send(method, self.mb_chars[i..i], other.mb_chars[i..i])
21
+ return c unless c == 0
22
+ end
23
+ l1 <=> l2
24
+ end
25
+ end
26
+
27
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sort-by-alphabet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Frantisek Havluj
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-19 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: moskyt@rozhled.cz
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/alphabets.rb
26
+ - lib/enumerable.rb
27
+ - lib/sort-by-alphabet.rb
28
+ - lib/sort_by_alphabet.rb
29
+ - init.rb
30
+ - README.textile
31
+ - MIT-LICENSE
32
+ - CHANGELOG
33
+ has_rdoc: true
34
+ homepage: http://github.com/moskyt/sort-by-alphabet
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.3.5
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Library for lexical sorting in various languages.
61
+ test_files: []
62
+