Sha256: 16125516b07012a15e60fe676c30b143272759d22d798f552141f97d6f88c19b
Contents?: true
Size: 1.2 KB
Versions: 4
Compression:
Stored size: 1.2 KB
Contents
require 'set' class Reverser attr_accessor :obj def initialize(obj) @obj = obj end def <=>(other) other.obj <=> self.obj end end class Object def rev Reverser.new(self) end end module Enumerable # Provides sorting on multiple attributes (each directional) where atts is # an array of symbols. # the default is to sort ascending (small to large). # the option :down => Symbol or ArrayOfSymbols # sort_by_attributes(:age,:height,:weight) # -> sorts by age, height, and weight # sort_by_attributes(:age,:height,:weight, :down => :height) # -> same as above, but sorts height from large to small # sort_by_attributes(:age,:height,:weight, :down => [:height,:weight]) # -> same as above, but sorts height and weight from large to small def sort_by_attributes(*atts) down = if atts.last.is_a? Hash hash = atts.pop unless hash[:down].is_a?(Array) hash[:down] = [hash[:down]] end Set.new(hash[:down]) else Set.new end self.sort_by do |obj| atts.collect do |att| if down.include?(att) obj.send(att).rev else obj.send(att) end end end end end
Version data entries
4 entries across 4 versions & 1 rubygems