mongoid_collation 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f8b80c69bc61252c0fb8b6bc6d8a85ddd4aac064
4
+ data.tar.gz: cb7e0fe01839f033e30c2a15ea4112e2d4a9a384
5
+ SHA512:
6
+ metadata.gz: 77e3574dbf049031e0d890c1be52e0a32d63468ac4beaf8974c3b6863e3b298a8d3594f9555d3b3bb3c25e9990985c93fbe9f9b93718d1f895cccc755099e32d
7
+ data.tar.gz: c6662e28f89b409701f6fd2ced2843dfd6eda4e2dc374627e7160fc0d2012f6aedce7f378fd87d5f11c46c00497a0e256db219bb43c36628ddb8b3be21e9e697
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Maciej Majewski
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ mongoid_collation
2
+ =================
3
+ [![Build Status](https://travis-ci.org/maciejmajewski/mongoid_collation.svg?branch=master)](https://travis-ci.org/maciejmajewski/mongoid_collation)
4
+
5
+ Support for sorting by collation for Mongoid
6
+
7
+ ### Problem description
8
+ * [Natural Language Sorting with MongoDB, Derick Rethans](http://derickrethans.nl/mongodb-collation.html)
9
+ * [SERVER-1920 in mongoDB JIRA (4 years old)](https://jira.mongodb.org/browse/SERVER-1920)
10
+ * http://www.unicode.org/reports/tr10/#Step_3
11
+
12
+ ### Contributing to mongoid_collation
13
+
14
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
15
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
16
+ * Fork the project.
17
+ * Start a feature/bugfix branch.
18
+ * Commit and push until you are happy with your contribution.
19
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
20
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
21
+
22
+ ### Copyright
23
+
24
+ Copyright (c) 2014 Maciej Majewski. See LICENSE.txt for
25
+ further details.
26
+
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module Collation
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,88 @@
1
+ require 'twitter_cldr'
2
+
3
+ module Mongoid
4
+ module Contextual
5
+ class Mongo
6
+ def sort_with_collation(values = nil, &block)
7
+ if klass.respond_to?(:collation_aliases)
8
+ values = replace_collation_aliases(values)
9
+ end
10
+
11
+ sort_without_collation(values, &block)
12
+ end
13
+
14
+ alias_method_chain :sort, :collation
15
+
16
+ private
17
+
18
+ def replace_collation_aliases(values)
19
+ case values
20
+ when Hash
21
+ values.dup.tap do |values_copy|
22
+ values_copy.keys.each do |key|
23
+ if is_collated?(key)
24
+ values_copy[collated_alias(key)] = values[key]
25
+ values_copy.delete(key)
26
+ end
27
+ end
28
+ end
29
+ else
30
+ Array(values).dup.tap do |values_copy|
31
+ values_copy.each do |key|
32
+ if key.is_a?(Origin::Key)
33
+ if is_collated?(key.name)
34
+ values_copy.delete(key)
35
+
36
+ values_copy <<
37
+ rebuild_origin_key(key, collated_alias(key.name))
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ def is_collated?(key)
46
+ klass.collation_aliases.has_key?(key.to_s)
47
+ end
48
+
49
+ def collated_alias(key)
50
+ klass.collation_aliases[key.to_s]
51
+ end
52
+
53
+ def rebuild_origin_key(key, name)
54
+ ::Origin::Key.new(name, key.strategy, key.operator, key.expanded,
55
+ &key.block)
56
+ end
57
+ end
58
+ end
59
+
60
+ module Collation
61
+ extend ActiveSupport::Concern
62
+
63
+ included do
64
+ class_attribute :collation_aliases
65
+ self.collation_aliases = {}
66
+ end
67
+
68
+ module ClassMethods
69
+ def collation(field, options = {})
70
+ raise ':locale option is required' unless options[:locale]
71
+
72
+ options[:suffix] ||= :collated
73
+
74
+ field_collated = "#{field}_#{options[:suffix]}"
75
+ field field_collated, type: String
76
+
77
+ self.collation_aliases[field.to_s] = field_collated
78
+
79
+ collator = TwitterCldr::Collation::Collator.new(options[:locale])
80
+
81
+ before_save do
82
+ sort_key = collator.get_sort_key(send(field))
83
+ send("#{field_collated}=", sort_key.map { |i| i.to_s(16) }.join)
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,49 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe Mongoid::Collation do
5
+ describe '.collation' do
6
+ before do
7
+ class Container
8
+ include Mongoid::Document
9
+ include Mongoid::Collation
10
+
11
+ field :string, type: String
12
+ collation :string, locale: :pl
13
+ end
14
+ end
15
+
16
+ after do
17
+ Object.send(:remove_const, :Container)
18
+ end
19
+
20
+ context 'given multiple strings' do
21
+ let(:strings) { ['Ą', 'A', 'Ś', 'Ł', 'Ó', 'W'] }
22
+ let(:strings_ordered) { ['A', 'Ą', 'Ł', 'Ó', 'Ś', 'W'] }
23
+
24
+ before do
25
+ strings.shuffle.each do |string|
26
+ Container.create!(string: string)
27
+ end
28
+ end
29
+
30
+ context 'given sorting by hash' do
31
+ let(:sorted_by_string) { Container.all.sort(string: 1).to_a }
32
+
33
+ it 'sorts according to collation' do
34
+ expect(sorted_by_string.map(&:string)).
35
+ to eq(strings_ordered)
36
+ end
37
+ end
38
+
39
+ context 'given sorting by Origin::Key' do
40
+ let(:sorted_by_string) { Container.all.sort(:string.asc).to_a }
41
+
42
+ it 'sorts according to collation' do
43
+ expect(sorted_by_string.map(&:string)).
44
+ to eq(strings_ordered)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,22 @@
1
+ require 'mongoid'
2
+ require 'mongoid_collation'
3
+ require 'database_cleaner'
4
+
5
+ Mongoid::Config.connect_to('mongoid_collation', {read: :primary,
6
+ write: {fsync: true}})
7
+
8
+ Mongoid.logger = Logger.new(STDOUT)
9
+ Moped.logger = Mongoid.logger
10
+
11
+ RSpec.configure do |config|
12
+ config.before(:suite) do
13
+ DatabaseCleaner.strategy = :truncation
14
+ DatabaseCleaner.clean_with(:truncation)
15
+ end
16
+
17
+ config.around(:each) do |example|
18
+ DatabaseCleaner.cleaning do
19
+ example.run
20
+ end
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_collation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Maciej Majewski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mongoid
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: twitter_cldr
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: database_cleaner
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Mongoid support for sorting with collation
98
+ email:
99
+ - maciej.majewski@gooddesign.pl
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - LICENSE
105
+ - README.md
106
+ - lib/mongoid/collation/version.rb
107
+ - lib/mongoid_collation.rb
108
+ - spec/mongoid_collation_spec.rb
109
+ - spec/spec_helper.rb
110
+ homepage: https://github.com/maciejmajewski/mongoid_collation
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.2.2
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Mongoid collation support
134
+ test_files:
135
+ - spec/mongoid_collation_spec.rb
136
+ - spec/spec_helper.rb