Sha256: 5c1765fd8eb7f3a2d4bb2d59027678a9c414a75b5044b02fc790902383f453ce
Contents?: true
Size: 1 KB
Versions: 14
Compression:
Stored size: 1 KB
Contents
# frozen_string_literal: true module Lipstick module Sortable class << self DIRECTIONS = %w[asc desc].freeze def direction(direction) direction = DIRECTIONS.find { |x| x == direction&.downcase } direction ||= DIRECTIONS.first direction end def opposite_direction(direction) (DIRECTIONS - [direction(direction)]).first end end module ClassMethods attr_reader :sortable_by_cases, :sortable_by_else def sortable_by(sort_cases, sort_else) @sortable_by_cases = sort_cases @sortable_by_else = sort_else end def sortable_sort(sort_by, direction) sort_by = sort_by&.to_sym direction = Lipstick::Sortable.direction(direction) if sort_by && sortable_by_cases.key?(sort_by) sortable_by_cases[sort_by].call(direction) else sortable_by_else.call(direction) end end end def self.included(base) base.extend(ClassMethods) end end end
Version data entries
14 entries across 14 versions & 1 rubygems