Sha256: 247afd7b452509d1aab6e6f581d8e49c1d5ca74023e9908b408cf528d216c19b

Contents?: true

Size: 881 Bytes

Versions: 1

Compression:

Stored size: 881 Bytes

Contents

# frozen_string_literal: true

module Cursed
  module Adapter
    class Base
      attr_reader :relation

      def initialize(array)
        @relation = array
      end

      def apply_to(cursor)
        attr = cursor.attribute

        after(attr, cursor.after) if cursor.after?

        before(attr, cursor.before) if cursor.before?

        if cursor.forward?
          ascend_by(attr)
        else
          descend_by(attr)
        end

        limit(cursor.clamped_limit)

        relation
      end
    end
  end

  module_function

  def Adapter(value)
    case value
    when -> (x) { x.is_a?(Class) && x.ancestors.include?(Adapter::Base) } then value
    when Sequel::Dataset then Adapter::Sequel
    when ActiveRecord::Base, ActiveRecord::Relation then Adapter::ActiveRecord
    else raise ArgumentError, "unable to cast #{value.inspect} to Adapter"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cursed-0.2.0 lib/cursed/adapter/base.rb