Module: AttributesSort

Defined in:
lib/attributes_sort.rb

Class Method Summary collapse

Class Method Details

.included(receiver) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/attributes_sort.rb', line 2

def self.included(receiver)
  receiver.instance_eval do
    def build_attributes(attributes)
      "[" + attributes.map!{|a| "object.#{a}"}.join(",") + "]"
    end

    def do_attributes_sort(collection, options={})
      attributes = build_attributes(options[:sort_by])
      collection.sort_by{|object| eval(attributes)}
    end
  end
  Array.class_eval do
    def class_type
      klass = self.first.class
      is_same = self.all?{|object| object.class == klass}
      raise "All objects must be of the same class type" unless is_same
      klass
    end

    def attr_sort(options = {})
      raise "You must pass in sort_by criteria" unless options[:sort_by]
      begin
        class_type.do_attributes_sort(self, options)
      rescue NoMethodError
        raise "You must sort by an attribute that exists in the object that you are sorting"
      end
    end
  end
end