Sha256: 84fc2f393cc585df92c43466e3a2cdba22d710afe17d1c5dd7defaa89450e637

Contents?: true

Size: 1.11 KB

Versions: 6

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

#
# Copyright (c) 2019-present, Blue Marble Payroll, LLC
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
#

require_relative 'key_path'

module Dbee
  class Query
    # Abstract representation of the ORDER BY part of a SQL statement.
    class Sorter
      acts_as_hashable

      module Direction
        ASCENDING = :ascending
        DESCENDING = :descending
      end
      include Direction

      attr_reader :direction, :key_path

      def initialize(key_path:, direction: ASCENDING)
        raise ArgumentError, 'key_path is required' if key_path.to_s.empty?

        @direction  = Direction.const_get(direction.to_s.upcase.to_sym)
        @key_path   = KeyPath.get(key_path)

        freeze
      end

      def descending?
        direction == DESCENDING
      end

      def ascending?
        !descending?
      end

      def hash
        "#{key_path}#{direction}".hash
      end

      def ==(other)
        other.key_path == key_path && other.direction == direction
      end
      alias eql? ==
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dbee-1.0.1 lib/dbee/query/sorter.rb
dbee-1.0.0 lib/dbee/query/sorter.rb
dbee-1.0.0.pre.alpha.3 lib/dbee/query/sorter.rb
dbee-1.0.0.pre.alpha.2 lib/dbee/query/sorter.rb
dbee-1.0.0.pre.alpha.1 lib/dbee/query/sorter.rb
dbee-1.0.0.pre.alpha lib/dbee/query/sorter.rb