Sha256: cb9e444489ff846238c004a75ba00d1a179ff71aba5a0c390052b337d894f55f

Contents?: true

Size: 1.95 KB

Versions: 36

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module LinkedRails
  class Collection
    class Sorting < RDF::Node
      include ActiveModel::Model
      include LinkedRails::Model
      DATE_MIN = Date.new(1970, 1, 1)
      DATE_TIME_MIN = Time.new(1970, 1, 1).utc
      STRING_MIN = ('0'.ord - 1).chr
      STRING_MAX = ('z'.ord + 1).chr

      attr_accessor :association_class, :direction, :key, :collection

      def attribute_name
        @attribute_name ||= key_from_mapping || :created_at
      end

      # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
      def default_value
        column = association_class.column_for_attribute(attribute_name)

        case column.type
        when :date
          direction == :asc ? DATE_MIN : Date.current
        when :datetime
          (direction == :asc ? DATE_TIME_MIN : Time.current.utc).iso8601(6)
        when :string, :text
          direction == :asc ? STRING_MIN : STRING_MAX
        else
          ActiveModel::Type::Integer.new(limit: column.limit).send(direction == :asc ? :min_value : :max_value)
        end
      end
      # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength

      def iri(**_opts)
        self
      end

      def sort_direction
        direction == :desc ? :lt : :gt
      end

      def sort_value
        {attribute_name => direction}
      end

      private

      def key_from_mapping
        if key == Vocab.ontola[:primaryKey]
          association_class.try(:primary_key)
        else
          association_class.try(:predicate_mapping).try(:[], key)&.key
        end
      end

      class << self
        def from_array(association_class, array, collection)
          array&.map do |sort|
            new(
              collection: collection,
              association_class: association_class,
              direction: sort[:direction]&.to_sym,
              key: sort[:key]
            )
          end
        end
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
linked_rails-0.0.4.pre.g3aeec2263 app/models/linked_rails/collection/sorting.rb
linked_rails-0.0.4.pre.gacd5f04d4 app/models/linked_rails/collection/sorting.rb
linked_rails-0.0.4.pre.gb0b6aea40 app/models/linked_rails/collection/sorting.rb
linked_rails-0.0.4.pre.g792a1b887 app/models/linked_rails/collection/sorting.rb
linked_rails-0.0.4.pre.g58af47992 app/models/linked_rails/collection/sorting.rb
linked_rails-0.0.4.pre.g0ec6e30ca app/models/linked_rails/collection/sorting.rb
linked_rails-0.0.4.pre.g9e3bcac55 app/models/linked_rails/collection/sorting.rb
linked_rails-0.0.4.pre.g222bfeeca app/models/linked_rails/collection/sorting.rb
linked_rails-0.0.4.pre.gb254a2893 app/models/linked_rails/collection/sorting.rb
linked_rails-0.0.4.pre.g2c6c26e86 app/models/linked_rails/collection/sorting.rb
linked_rails-0.0.4.pre.ge881247c3 app/models/linked_rails/collection/sorting.rb
linked_rails-0.0.4.pre.gfabf7e9f5 app/models/linked_rails/collection/sorting.rb
linked_rails-0.0.4.pre.g8c045be3a app/models/linked_rails/collection/sorting.rb
linked_rails-0.0.4.pre.g8d7195543 app/models/linked_rails/collection/sorting.rb
linked_rails-0.0.4.pre.gfe77aae19 app/models/linked_rails/collection/sorting.rb
linked_rails-0.0.4.pre.gc866c1806 app/models/linked_rails/collection/sorting.rb
linked_rails-0.0.4.pre.g3e8f5731a app/models/linked_rails/collection/sorting.rb
linked_rails-0.0.4.pre.g58e27be7f app/models/linked_rails/collection/sorting.rb
linked_rails-0.0.4.pre.g96885856b app/models/linked_rails/collection/sorting.rb
linked_rails-0.0.4.pre.g1faca9643 app/models/linked_rails/collection/sorting.rb