Sha256: 196a01b449daaa2319f0c868ce4e187702dc32da218eb6997752fd37cab24535

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module LunaPark
  module Extensions
    module Repositories
      module Postgres
        module Read
          def lock!(pk_value)
            transaction { yield find! pk_value, for_update: true }
          end

          def lock(pk_value)
            transaction { yield find pk_value, for_update: true }
          end

          def transaction(&block)
            dataset.transaction(&block)
          end

          def find!(pk_value, **scope)
            found! find(pk_value, **scope), not_found_by: pk_value
          end

          def find(pk_value, **scope)
            read_one scoped(**scope).where(primary_key => pk_value)
          end

          def count(**scope)
            scoped(**scope).count
          end

          def all(**scope)
            read_all(scoped(**scope).order { created_at.desc })
          end

          def first(**scope)
            read_one scoped(**scope).order(:created_at).first
          end

          def last(**scope)
            read_one scoped(**scope).order(:created_at).last
          end

          private

          def scope(dataset, for_update: false, **_)
            dataset = dataset.for_update if for_update
            dataset
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
luna_park-0.13.0 lib/luna_park/extensions/repositories/postgres/read.rb