attributes_sort 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.2
@@ -2,29 +2,34 @@ module AttributesSort
2
2
  def self.included(receiver)
3
3
  receiver.instance_eval do
4
4
  def build_attributes(attributes)
5
- "[" + attributes.map!{|attribute| "object.#{attribute}"}.join(",") + "]"
5
+ "[" << attributes.map!{|attribute| "object.#{attribute}"}.join(",") << "]"
6
6
  end
7
7
 
8
8
  def do_attributes_sort(collection, attributes)
9
- attributes = build_attributes(attributes)
10
- collection.sort_by{|object| eval(attributes)}
9
+ attribute_array_string = build_attributes(attributes)
10
+ collection.sort_by{|object| eval(attribute_array_string)}
11
11
  end
12
12
  end
13
13
  Array.class_eval do
14
+ def sortable_attributes?
15
+ self.all? {|object| @attributes.all?{|attribute| object.respond_to?(attribute)}}
16
+ end
17
+
14
18
  def class_type
15
19
  klass = self.first.class
16
- is_same = self.all?{|object| object.class == klass}
17
- raise "All objects must be of the same class type" unless is_same
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
+ raise "All objects must respond to sort criteria"unless sortable_attributes?
18
26
  klass
19
27
  end
20
28
 
21
29
  def attr_sort(options = {})
22
- raise "You must pass in sort_by criteria" unless attributes = options[:sort_by]
23
- begin
24
- class_type.do_attributes_sort(self, attributes)
25
- rescue NoMethodError
26
- raise "You must sort by an attribute that exists in the object that you are sorting"
27
- end
30
+ @attributes = options[:sort_by]
31
+ raise "You must pass in sort_by criteria" unless @attributes
32
+ class_type.do_attributes_sort(self, @attributes)
28
33
  end
29
34
  end
30
35
  end
@@ -67,14 +67,31 @@ describe "AttributesSort" do
67
67
  lambda {test_sort(nil, [@p1, @p2, @p5, @p4, @p3])}.should raise_error("You must pass in sort_by criteria")
68
68
  end
69
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")
70
+ it "raise error object does not respond to method call" do
71
+ lambda {test_sort([:blah], [@p1, @p2, @p5, @p4, @p3])}.should raise_error("All objects must respond to sort criteria")
72
72
  end
73
73
 
74
74
  it "raise error if objects are not of same class type" do
75
+ pending "using duck type check instead"
75
76
  @people << "some other object"
76
77
  lambda {test_sort([:age],[@p1, @p2, @p5, @p4, @p3])}.should raise_error("All objects must be of the same class type")
77
78
  end
78
79
 
80
+ class Dog
81
+ attr_accessor :firstname,:lastname,:age
82
+
83
+ def initialize(firstname="",lastname="",age=0)
84
+ @firstname = firstname
85
+ @lastname = lastname
86
+ @age = age
87
+ end
88
+ end
89
+
90
+ it "duck typed objects can be sorted as long as they all respond to same methods" do
91
+ dog = Dog.new("fido", "zib", 7)
92
+ @people << dog
93
+ test_sort([:lastname,:firstname,:age], [@p2, @p1, @p5, @p4, @p3, dog])
94
+ end
95
+
79
96
  end
80
97
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 1
9
- version: 0.2.1
8
+ - 2
9
+ version: 0.2.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Karmen Blake
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-23 00:00:00 -07:00
17
+ date: 2010-03-24 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency