attributes_sort 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/attributes_sort.rb +23 -12
- data/spec/attributes_sort_spec.rb +62 -47
- metadata +12 -5
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/attributes_sort.rb
CHANGED
@@ -1,21 +1,32 @@
|
|
1
1
|
module AttributesSort
|
2
2
|
def self.included(receiver)
|
3
|
+
receiver.instance_eval do
|
4
|
+
def build_attributes(attributes)
|
5
|
+
"[" + attributes.map!{|a| "object.#{a}"}.join(",") + "]"
|
6
|
+
end
|
7
|
+
|
8
|
+
def do_attributes_sort(collection, options={})
|
9
|
+
attributes = build_attributes(options[:sort_by])
|
10
|
+
collection.sort_by{|object| eval(attributes)}
|
11
|
+
end
|
12
|
+
end
|
3
13
|
Array.class_eval do
|
4
|
-
def
|
5
|
-
raise "You must pass in sort_by criteria" unless options[:sort_by]
|
14
|
+
def class_type
|
6
15
|
klass = self.first.class
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
16
|
+
is_same = self.all?{|object| object.class == klass}
|
17
|
+
raise "All objects must be of the same class type" unless is_same
|
18
|
+
klass
|
19
|
+
end
|
20
|
+
|
21
|
+
def attr_sort(options = {})
|
22
|
+
raise "You must pass in sort_by criteria" unless options[:sort_by]
|
23
|
+
begin
|
24
|
+
class_type.do_attributes_sort(self, options)
|
25
|
+
rescue NoMethodError
|
26
|
+
raise "You must sort by an attribute that exists in the object that you are sorting"
|
12
27
|
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
28
|
end
|
19
29
|
end
|
20
30
|
end
|
21
31
|
end
|
32
|
+
|
@@ -4,7 +4,7 @@ class Person
|
|
4
4
|
include AttributesSort
|
5
5
|
|
6
6
|
attr_accessor :firstname,:lastname,:age
|
7
|
-
|
7
|
+
|
8
8
|
def initialize(firstname="",lastname="",age=0)
|
9
9
|
@firstname = firstname
|
10
10
|
@lastname = lastname
|
@@ -14,52 +14,67 @@ end
|
|
14
14
|
|
15
15
|
|
16
16
|
describe "AttributesSort" do
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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 "sort_by standard ruby" do
|
31
|
+
@people.sort_by{|person| [person.age,person.firstname]}.should == [@p4, @p5, @p2, @p3, @p1]
|
32
|
+
end
|
29
33
|
|
30
34
|
it "object must have attr accessible attributes for sort_by to work" do
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
35
|
+
p = Person.new
|
36
|
+
p.should respond_to(:firstname)
|
37
|
+
p.should respond_to(:lastname)
|
38
|
+
p.should respond_to(:age)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "build attributes string array to be eval'd at deferred time in sort_by block'" do
|
42
|
+
criteria = [:lastname, :age, :firstname]
|
43
|
+
Person.build_attributes(criteria).should == "[object.lastname,object.age,object.firstname]"
|
44
|
+
end
|
45
|
+
|
46
|
+
it "sort by last name" do
|
47
|
+
test_sort([:lastname], [@p1, @p2, @p5, @p4, @p3])
|
48
|
+
end
|
49
|
+
|
50
|
+
it "sort by age" do
|
51
|
+
test_sort([:age], [@p4, @p2, @p5, @p3, @p1])
|
52
|
+
end
|
53
|
+
|
54
|
+
it "sort by age then by first name" do
|
55
|
+
test_sort([:age, :firstname], [@p4, @p5, @p2, @p3, @p1])
|
56
|
+
end
|
57
|
+
|
58
|
+
it "sort by age then by last name" do
|
59
|
+
test_sort([:age, :lastname], [@p4, @p2, @p5, @p3, @p1])
|
60
|
+
end
|
61
|
+
|
62
|
+
it "sort by last name then by first name then by age" do
|
63
|
+
test_sort([:lastname,:firstname,:age], [@p2, @p1, @p5, @p4, @p3])
|
64
|
+
end
|
65
|
+
|
66
|
+
it "raise error if criteria is not provided" do
|
67
|
+
lambda {test_sort(nil, [@p1, @p2, @p5, @p4, @p3])}.should raise_error("You must pass in sort_by criteria")
|
68
|
+
end
|
69
|
+
|
70
|
+
it "raise error if criteria is not a valid attribute of class" do
|
71
|
+
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")
|
72
|
+
end
|
73
|
+
|
74
|
+
it "raise error if objects are not of same class type" do
|
75
|
+
@people << "some other object"
|
76
|
+
lambda {test_sort([:age],[@p1, @p2, @p5, @p4, @p3])}.should raise_error("All objects must be of the same class type")
|
77
|
+
end
|
78
|
+
|
65
79
|
end
|
80
|
+
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attributes_sort
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Karmen Blake
|
@@ -9,7 +14,7 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date:
|
17
|
+
date: 2010-03-23 00:00:00 -07:00
|
13
18
|
default_executable:
|
14
19
|
dependencies: []
|
15
20
|
|
@@ -46,18 +51,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
46
51
|
requirements:
|
47
52
|
- - ">="
|
48
53
|
- !ruby/object:Gem::Version
|
54
|
+
segments:
|
55
|
+
- 0
|
49
56
|
version: "0"
|
50
|
-
version:
|
51
57
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
58
|
requirements:
|
53
59
|
- - ">="
|
54
60
|
- !ruby/object:Gem::Version
|
61
|
+
segments:
|
62
|
+
- 0
|
55
63
|
version: "0"
|
56
|
-
version:
|
57
64
|
requirements: []
|
58
65
|
|
59
66
|
rubyforge_project:
|
60
|
-
rubygems_version: 1.3.
|
67
|
+
rubygems_version: 1.3.6
|
61
68
|
signing_key:
|
62
69
|
specification_version: 3
|
63
70
|
summary: A slick way to sort a collection objects.
|