Sha256: 08afa2ec5dce6cecd46917e2f4c6b4480c88665f36bd331c9e0ad8cde1e2e9b6

Contents?: true

Size: 568 Bytes

Versions: 4

Compression:

Stored size: 568 Bytes

Contents

# Macro to implement a comparison method for Ruby classes.
# origin: RM
module SortableTrait

  as_trait do |options|

    send(:define_method, :"<=>") do |other|
      fields = Array(options[:by])
      cmp = 0
      for field in fields
        left = send(field)
        left = left.downcase if left.respond_to?(:downcase)
        right = other.send(field)
        right = right.downcase if right.respond_to?(:downcase)
        cmp = left <=> right
        break unless cmp == 0
      end
      cmp *= -1 if options[:descending]
      cmp
    end
      
  end
  
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
serum-rails-0.2.1 spec/test_apps/rails-2-3/app/models/shared/sortable_trait.rb
serum-rails-0.2.0 spec/test_apps/rails-2-3/app/models/shared/sortable_trait.rb
serum-rails-0.1.1 spec/test_app/app/models/shared/sortable_trait.rb
serum-rails-0.1.0 spec/test_app/app/models/shared/sortable_trait.rb