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

Version Path
aaf-lipstick-4.10.0 lib/lipstick/sortable.rb
aaf-lipstick-4.9.2 lib/lipstick/sortable.rb
aaf-lipstick-4.9.1 lib/lipstick/sortable.rb
aaf-lipstick-4.9.0 lib/lipstick/sortable.rb
aaf-lipstick-4.8.0 lib/lipstick/sortable.rb
aaf-lipstick-4.7.0 lib/lipstick/sortable.rb
aaf-lipstick-4.6.0 lib/lipstick/sortable.rb
aaf-lipstick-4.5.1 lib/lipstick/sortable.rb
aaf-lipstick-4.4.0 lib/lipstick/sortable.rb
aaf-lipstick-4.3.0 lib/lipstick/sortable.rb
aaf-lipstick-4.2.0 lib/lipstick/sortable.rb
aaf-lipstick-4.1.0 lib/lipstick/sortable.rb
aaf-lipstick-4.0.1 lib/lipstick/sortable.rb
aaf-lipstick-3.2.0 lib/lipstick/sortable.rb