sort_alphabetical 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ Adds `sort_alphabetical` and `sort_alphabetical_by` to Enumberable(Array/Hash...),
2
+ which sorts UTF8 Strings alphabetical.
3
+ This sorting is done by placing variants on the same level as base character (A comes before Ä but ÄA comes before AB).
4
+
5
+
6
+ Setup
7
+ =====
8
+ - As Rails plugin: `script/plugin install git://github.com/grosser/sort_alphabetical.git `
9
+ - As gem: ` sudo gem install sort_alphabetical `
10
+
11
+ Usage
12
+ =====
13
+ ['b','á'].sort_alphabetical == ['á','b']
14
+ [['b',1],['á',2]].sort_alphabetical_by(&:first) == [['á',2],['b',1]]
15
+
16
+ TODO
17
+ ====
18
+ - Sort non-ascii-convertables like ß(ss), œ(oe) , fi(fi), see [Ligatures](http://en.wikipedia.org/wiki/Typographical_ligature)
19
+ - Integrate natural sorting e.g. `['a11', 'a2'] => ['a2', 'a11']` like [NaturalSort](http://rubyforge.org/projects/naturalsort)
20
+
21
+ Authors
22
+ =======
23
+ - original string_to_ascii: [Marc-Andre](http://marc-andre.ca/).
24
+
25
+ [Michael Grosser](http://pragmatig.wordpress.com)
26
+ grosser.michael@gmail.com
27
+ Hereby placed under public domain, do what you want, just do not hold me accountable...
@@ -0,0 +1,20 @@
1
+ task :default => :spec
2
+ require 'spec/rake/spectask'
3
+ Spec::Rake::SpecTask.new {|t| t.spec_opts = ['--color']}
4
+
5
+ begin
6
+ project_name = 'sort_alphabetical'
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = project_name
10
+ gem.summary = "Sort UTF8 Strings alphabetical via Enumerable extension"
11
+ gem.email = "grosser.michael@gmail.com"
12
+ gem.homepage = "http://github.com/grosser/#{project_name}"
13
+ gem.authors = ["Michael Grosser"]
14
+ gem.add_dependency "activesupport"
15
+ end
16
+
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
20
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'sort_alphabetical'
@@ -0,0 +1,33 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'sort_alphabetical/core_ext'
4
+
5
+ module SortAlphabetical
6
+ extend self
7
+
8
+ def sort(set)
9
+ set.sort_by do |item|
10
+ if block_given?
11
+ item = yield(item).to_s
12
+ else
13
+ item = item.to_s
14
+ end
15
+ [to_ascii(item),item]#when both á and a are present, sort them a, á
16
+ end
17
+ end
18
+
19
+ def to_ascii(string)
20
+ split_codepoints(string).map(&:to_s).reject{|e| e.length > 1}.join
21
+ end
22
+
23
+ private
24
+
25
+ # returns an array of unicode codepoints, in canonical order
26
+ def split_codepoints(string)
27
+ if defined? ActiveSupport::Multibyte::Handlers #pre ActiveSupport 2.3
28
+ ActiveSupport::Multibyte::Handlers::UTF8Handler.normalize(string,:d).split(//u)
29
+ else
30
+ string.mb_chars.normalize(:d).split(//u)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,9 @@
1
+ module Enumerable
2
+ def sort_alphabetical
3
+ SortAlphabetical.sort(self)
4
+ end
5
+
6
+ def sort_alphabetical_by(&block)
7
+ SortAlphabetical.sort(self,&block)
8
+ end
9
+ end
@@ -0,0 +1,52 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{sort_alphabetical}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Michael Grosser"]
12
+ s.date = %q{2009-11-22}
13
+ s.email = %q{grosser.michael@gmail.com}
14
+ s.extra_rdoc_files = [
15
+ "README.markdown"
16
+ ]
17
+ s.files = [
18
+ "README.markdown",
19
+ "Rakefile",
20
+ "VERSION",
21
+ "init.rb",
22
+ "lib/sort_alphabetical.rb",
23
+ "lib/sort_alphabetical/core_ext.rb",
24
+ "sort_alphabetical.gemspec",
25
+ "spec/README.markdown",
26
+ "spec/sort_alphabetical_spec.rb",
27
+ "spec/spec_helper.rb"
28
+ ]
29
+ s.homepage = %q{http://github.com/grosser/sort_alphabetical}
30
+ s.rdoc_options = ["--charset=UTF-8"]
31
+ s.require_paths = ["lib"]
32
+ s.rubygems_version = %q{1.3.5}
33
+ s.summary = %q{Sort UTF8 Strings alphabetical via Enumerable extension}
34
+ s.test_files = [
35
+ "spec/spec_helper.rb",
36
+ "spec/sort_alphabetical_spec.rb"
37
+ ]
38
+
39
+ if s.respond_to? :specification_version then
40
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
44
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
45
+ else
46
+ s.add_dependency(%q<activesupport>, [">= 0"])
47
+ end
48
+ else
49
+ s.add_dependency(%q<activesupport>, [">= 0"])
50
+ end
51
+ end
52
+
@@ -0,0 +1,2 @@
1
+ Autotest will not work here, since both activesupport 2.1.1 and 2.3 are tested for individually.
2
+ Simply run `rake`
@@ -0,0 +1,38 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe "SortAlphabetical"do
4
+ it "sorts ascii correctly" do
5
+ %w(b c a).sort_alphabetical.should == %w(a b c)
6
+ end
7
+
8
+ it "sorts UTF8 chars without accents" do
9
+ %w(b a á ä o ó x ö í i c).sort_alphabetical.should == %w(a á ä b c i í o ó ö x)
10
+ end
11
+
12
+ it "sorts by merging base with accented" do
13
+ %w(AA AB ÄA).sort_alphabetical.should == %w(AA ÄA AB)
14
+ end
15
+
16
+ it "sorts words" do
17
+ %w(hellö hello hellá).sort_alphabetical.should == %w(hellá hello hellö)
18
+ end
19
+
20
+ it "obeys order for ligatures" do
21
+ pending
22
+ %w(asb aßc asd).sort_alphabetical.should == %w(asb aßc asd)
23
+ end
24
+
25
+ describe :to_ascii do
26
+ it "removes any accents" do
27
+ SortAlphabetical.to_ascii('á').should == 'a'
28
+ end
29
+ end
30
+
31
+ describe :sort_alphabetical_by do
32
+ it "sorts using given block" do
33
+ silence_warnings do
34
+ %w(za yá xä xa).sort_alphabetical_by{|x|x.chars.to_a.reverse.join}.should == %w(xa xä yá za)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH << 'lib'
2
+ require 'sort_alphabetical'
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sort_alphabetical
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Grosser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-22 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description:
26
+ email: grosser.michael@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.markdown
33
+ files:
34
+ - README.markdown
35
+ - Rakefile
36
+ - VERSION
37
+ - init.rb
38
+ - lib/sort_alphabetical.rb
39
+ - lib/sort_alphabetical/core_ext.rb
40
+ - sort_alphabetical.gemspec
41
+ - spec/README.markdown
42
+ - spec/sort_alphabetical_spec.rb
43
+ - spec/spec_helper.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/grosser/sort_alphabetical
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --charset=UTF-8
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.3.5
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Sort UTF8 Strings alphabetical via Enumerable extension
72
+ test_files:
73
+ - spec/spec_helper.rb
74
+ - spec/sort_alphabetical_spec.rb