Sha256: db4f7226925654bc645c1b4299a32e782903357a2cc33143d9875e04827be9c3

Contents?: true

Size: 1.61 KB

Versions: 6

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

require_relative '../adapter_base'
require_relative '../processor_base'

class ActiveSet
  class Processor::Sort < Processor::Base
    class EnumerableAdapter < Adapter::Base
      def process
        @set.sort do |left, right|
          @instructions.reduce(0) do |diff, instruction|
            # set Adapter::Base#instruction, which many methods depend on
            self.instruction = instruction

            # `left` and `right` differed at an earlier order entry
            next diff if diff != 0

            left_value = sortable_attribute_for(left)
            right_value = sortable_attribute_for(right)

            # handle `nil` values
            next  0 if left_value.nil? && right_value.nil?
            next  1 if left_value.nil?
            next -1 if right_value.nil?

            # do the actual comparison
            comparison = left_value <=> right_value
            next comparison * direction_multiplier(instruction.value)
          end
        end
      end

      private

      def sortable_attribute_for(item)
        value = instruction.value_for(item: item)

        return value.to_s.downcase if case_insensitive?(value)

        value
      end

      def case_insensitive?(value)
        # Cannot sort pure Booleans or Nils, so we _must_ cast to Strings
        return true if value.is_a?(TrueClass) || value.is_a?(FalseClass)
        return true if value.is_a?(NilClass)

        instruction.operator.to_s.downcase == 'i'
      end

      def direction_multiplier(direction)
        return -1 if direction.to_s.downcase.start_with? 'desc'

        1
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
activeset-0.6.5 lib/active_set/processor_sort/enumerable_adapter.rb
activeset-0.6.4 lib/active_set/processor_sort/enumerable_adapter.rb
activeset-0.6.3 lib/active_set/processor_sort/enumerable_adapter.rb
activeset-0.6.2 lib/active_set/processor_sort/enumerable_adapter.rb
activeset-0.6.1 lib/active_set/processor_sort/enumerable_adapter.rb
activeset-0.6.0 lib/active_set/processor_sort/enumerable_adapter.rb