attributes_sort 0.2.2 → 0.2.3
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 +2 -12
- data/spec/attributes_sort_spec.rb +0 -11
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.3
|
data/lib/attributes_sort.rb
CHANGED
@@ -1,13 +1,8 @@
|
|
1
1
|
module AttributesSort
|
2
2
|
def self.included(receiver)
|
3
3
|
receiver.instance_eval do
|
4
|
-
def
|
5
|
-
|
6
|
-
end
|
7
|
-
|
8
|
-
def do_attributes_sort(collection, attributes)
|
9
|
-
attribute_array_string = build_attributes(attributes)
|
10
|
-
collection.sort_by{|object| eval(attribute_array_string)}
|
4
|
+
def do_attributes_sort(collection, attributes)
|
5
|
+
collection.sort_by { |object| attributes.map { |attribute| object.send(attribute) }}
|
11
6
|
end
|
12
7
|
end
|
13
8
|
Array.class_eval do
|
@@ -17,11 +12,6 @@ module AttributesSort
|
|
17
12
|
|
18
13
|
def class_type
|
19
14
|
klass = self.first.class
|
20
|
-
|
21
|
-
#use duck type check respond to check instead
|
22
|
-
#is_same = self.all?{|object| object.class == klass}
|
23
|
-
#raise "All objects must be of the same class type" unless is_same
|
24
|
-
|
25
15
|
raise "All objects must respond to sort criteria"unless sortable_attributes?
|
26
16
|
klass
|
27
17
|
end
|
@@ -38,11 +38,6 @@ describe "AttributesSort" do
|
|
38
38
|
p.should respond_to(:age)
|
39
39
|
end
|
40
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
41
|
it "sort by last name" do
|
47
42
|
test_sort([:lastname], [@p1, @p2, @p5, @p4, @p3])
|
48
43
|
end
|
@@ -71,12 +66,6 @@ describe "AttributesSort" do
|
|
71
66
|
lambda {test_sort([:blah], [@p1, @p2, @p5, @p4, @p3])}.should raise_error("All objects must respond to sort criteria")
|
72
67
|
end
|
73
68
|
|
74
|
-
it "raise error if objects are not of same class type" do
|
75
|
-
pending "using duck type check instead"
|
76
|
-
@people << "some other object"
|
77
|
-
lambda {test_sort([:age],[@p1, @p2, @p5, @p4, @p3])}.should raise_error("All objects must be of the same class type")
|
78
|
-
end
|
79
|
-
|
80
69
|
class Dog
|
81
70
|
attr_accessor :firstname,:lastname,:age
|
82
71
|
|