attributes_sort 0.1.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/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +37 -0
- data/Rakefile +60 -0
- data/VERSION +1 -0
- data/lib/attributes_sort.rb +21 -0
- data/spec/attributes_sort_spec.rb +65 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +66 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Karmen Blake
|
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.rdoc
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
= attributes_sort
|
2
|
+
|
3
|
+
This gem allows you to take a collection of any type of object and sort them using the attributes of the object.
|
4
|
+
|
5
|
+
Example:
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
require 'rubygems'
|
10
|
+
require 'attributes_sort'
|
11
|
+
|
12
|
+
class Person
|
13
|
+
|
14
|
+
include AttributesSort
|
15
|
+
|
16
|
+
attr_accessor :firstname,:lastname,:age
|
17
|
+
|
18
|
+
def initialize(firstname="", lastname="" ,age=0)
|
19
|
+
@firstname = firstname
|
20
|
+
@lastname = lastname
|
21
|
+
@age = age
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
people = [Person.new("joe","blow",12),Person.new("joe","blow",89),Person.new("mary","watson",32),Person.new("annie","watson",9),Person.new("bob","builder",12)]
|
26
|
+
|
27
|
+
p people.attr_sort(:sort_by=>[:lastname])
|
28
|
+
|
29
|
+
p people.attr_sort(:sort_by=>[:age])
|
30
|
+
|
31
|
+
p people.attr_sort(:sort_by=>[:lastname,:firstname,:age])
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
== Copyright
|
36
|
+
|
37
|
+
Copyright (c) 2009 Karmen Blake. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "attributes_sort"
|
8
|
+
gem.summary = %Q{A slick way to sort a collection objects.}
|
9
|
+
gem.description = %Q{Sort a collection of objects using one or more of the attributes of your object.}
|
10
|
+
gem.email = "karmenblake@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/kblake/attributes_sort"
|
12
|
+
gem.authors = ["Karmen Blake"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'spec/rake/spectask'
|
21
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
22
|
+
spec.libs << 'lib' << 'spec'
|
23
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
24
|
+
end
|
25
|
+
|
26
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
27
|
+
spec.libs << 'lib' << 'spec'
|
28
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
29
|
+
spec.rcov = true
|
30
|
+
end
|
31
|
+
|
32
|
+
task :spec => :check_dependencies
|
33
|
+
|
34
|
+
task :default => :spec
|
35
|
+
|
36
|
+
require 'rake/rdoctask'
|
37
|
+
Rake::RDocTask.new do |rdoc|
|
38
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
39
|
+
|
40
|
+
rdoc.rdoc_dir = 'rdoc'
|
41
|
+
rdoc.title = "attributes_sort #{version}"
|
42
|
+
rdoc.rdoc_files.include('README*')
|
43
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
44
|
+
end
|
45
|
+
|
46
|
+
begin
|
47
|
+
require 'jeweler'
|
48
|
+
Jeweler::Tasks.new do |gem|
|
49
|
+
gem.name = "attributes_sort"
|
50
|
+
gem.summary = "A slick way to sort a collection objects."
|
51
|
+
gem.description = "Sort a collection of objects using one or more of the attributes of your object."
|
52
|
+
gem.email = "karmenblake@gmail.com"
|
53
|
+
gem.homepage = "http://github.com/kblake/attributes_sort"
|
54
|
+
gem.authors = ["Karmen Blake"]
|
55
|
+
end
|
56
|
+
Jeweler::GemcutterTasks.new
|
57
|
+
rescue LoadError
|
58
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
59
|
+
end
|
60
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module AttributesSort
|
2
|
+
def self.included(receiver)
|
3
|
+
Array.class_eval do
|
4
|
+
def attr_sort(options = {})
|
5
|
+
raise "You must pass in sort_by criteria" unless options[:sort_by]
|
6
|
+
klass = self.first.class
|
7
|
+
klass.instance_eval do
|
8
|
+
def do_attributes_sort(collection, options={})
|
9
|
+
attributes = options[:sort_by].inject("["){|attribute_string, attribute| attribute_string << "object.#{attribute},"} + "]"
|
10
|
+
collection.sort_by{|object| eval(attributes)}
|
11
|
+
end
|
12
|
+
end
|
13
|
+
begin
|
14
|
+
klass.do_attributes_sort(self, options)
|
15
|
+
rescue NoMethodError
|
16
|
+
raise "You must sort by an attribute that exists in the object that you are sorting"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
class Person
|
4
|
+
include AttributesSort
|
5
|
+
|
6
|
+
attr_accessor :firstname,:lastname,:age
|
7
|
+
|
8
|
+
def initialize(firstname="",lastname="",age=0)
|
9
|
+
@firstname = firstname
|
10
|
+
@lastname = lastname
|
11
|
+
@age = age
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
describe "AttributesSort" do
|
17
|
+
before(:each) do
|
18
|
+
@p1 = Person.new("joe","blow",89)
|
19
|
+
@p2 = Person.new("joe","blow",12)
|
20
|
+
@p3 = Person.new("mary","watson",32)
|
21
|
+
@p4 = Person.new("annie","watson",9)
|
22
|
+
@p5 = Person.new("bob","builder",12)
|
23
|
+
@people = [@p1, @p2, @p3, @p4, @p5]
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_sort(criteria, result)
|
27
|
+
@people.attr_sort(:sort_by => criteria).should == result
|
28
|
+
end
|
29
|
+
|
30
|
+
it "object must have attr accessible attributes for sort_by to work" do
|
31
|
+
p = Person.new
|
32
|
+
p.should respond_to(:firstname)
|
33
|
+
p.should respond_to(:lastname)
|
34
|
+
p.should respond_to(:age)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "sort by last name" do
|
38
|
+
test_sort([:lastname], [@p1, @p2, @p5, @p4, @p3])
|
39
|
+
end
|
40
|
+
|
41
|
+
it "sort by age" do
|
42
|
+
test_sort([:age], [@p4, @p2, @p5, @p3, @p1])
|
43
|
+
end
|
44
|
+
|
45
|
+
it "sort by age then by first name" do
|
46
|
+
test_sort([:age, :firstname], [@p4, @p5, @p2, @p3, @p1])
|
47
|
+
end
|
48
|
+
|
49
|
+
it "sort by age then by last name" do
|
50
|
+
test_sort([:age, :lastname], [@p4, @p2, @p5, @p3, @p1])
|
51
|
+
end
|
52
|
+
|
53
|
+
it "sort by last name then by first name then by age" do
|
54
|
+
test_sort([:lastname,:firstname,:age], [@p2, @p1, @p5, @p4, @p3])
|
55
|
+
end
|
56
|
+
|
57
|
+
it "raise error if criteria is not provided" do
|
58
|
+
lambda {test_sort(nil, [@p1, @p2, @p5, @p4, @p3])}.should raise_error("You must pass in sort_by criteria")
|
59
|
+
end
|
60
|
+
|
61
|
+
it "raise error if criteria is not a valid attribute of class" do
|
62
|
+
lambda {test_sort([:blah], [@p1, @p2, @p5, @p4, @p3])}.should raise_error("You must sort by an attribute that exists in the object that you are sorting")
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attributes_sort
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Karmen Blake
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-30 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Sort a collection of objects using one or more of the attributes of your object.
|
17
|
+
email: karmenblake@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- .document
|
27
|
+
- .gitignore
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
- Rakefile
|
31
|
+
- VERSION
|
32
|
+
- lib/attributes_sort.rb
|
33
|
+
- spec/attributes_sort_spec.rb
|
34
|
+
- spec/spec.opts
|
35
|
+
- spec/spec_helper.rb
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://github.com/kblake/attributes_sort
|
38
|
+
licenses: []
|
39
|
+
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options:
|
42
|
+
- --charset=UTF-8
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.3.5
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: A slick way to sort a collection objects.
|
64
|
+
test_files:
|
65
|
+
- spec/attributes_sort_spec.rb
|
66
|
+
- spec/spec_helper.rb
|