Sha256: e46012e878fce009519621bd0596227ed31fd7fdc12f3fb2b5e60de204400635

Contents?: true

Size: 869 Bytes

Versions: 1

Compression:

Stored size: 869 Bytes

Contents

# frozen_string_literal: true

require_relative '../base_adapter'
require_relative '../base_processor'

class ActiveSet
  class SortProcessor < BaseProcessor
    class EnumerableAdapter < BaseAdapter
      def process(set)
        set.sort_by do |item|
          attribute_value = @instruction.value_for(item: item)
          case_insensitive?(attribute_value) ? insensify(attribute_value) : attribute_value
        end.tap { |c| c.reverse! if @instruction.value.to_s == 'desc' }
      end

      private

      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 == 'i'
      end

      def insensify(value)
        value.to_s.downcase
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activeset-0.3.1 lib/active_set/processors/sort/enumerable_adapter.rb