Sha256: cc88fb8a3a1f6b4413b957f0ff6021870a4b398fbc54828e71cb149daf05eaf6

Contents?: true

Size: 1.7 KB

Versions: 10

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module LunaPark
  module Extensions
    module Repositories
      module Postgres
        module Read
          def find!(pk_value, for_update: false)
            ds = dataset.where(primary_key => pk_value)
            read_one!(ds, for_update: for_update, not_found_meta: pk_value)
          end

          def find(pk_value, for_update: false)
            ds = dataset.where(primary_key => pk_value)
            read_one(ds, for_update: for_update)
          end

          def lock!(pk_value)
            lock(pk_value) || raise(Errors::NotFound, "#{short_class_name} (#{pk_value})")
          end

          def lock(pk_value)
            dataset.for_update.select(primary_key).where(primary_key => pk_value).first ? true : false
          end

          def count
            dataset.count
          end

          def all
            read_all(dataset.order { created_at.desc })
          end

          def last
            to_entity from_row dataset.order(:created_at).last
          end

          private

          def read_one!(dataset, for_update: false, not_found_meta:)
            read_one(dataset, for_update: for_update).tap do |entity|
              raise Errors::NotFound, "#{short_class_name} (#{not_found_meta})" if entity.nil?
            end
          end

          def read_one(dataset, for_update: false)
            dataset = dataset.for_update if for_update
            row = dataset.first
            to_entity from_row(row)
          end

          def read_all(dataset)
            to_entities from_rows(dataset)
          end

          def short_class_name
            @short_class_name ||= self.class.name[/::(\w+)\z/, 1]
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
luna_park-0.12.1 lib/luna_park/extensions/repositories/postgres/read.rb
luna_park-0.12.0 lib/luna_park/extensions/repositories/postgres/read.rb
luna_park-0.11.7 lib/luna_park/extensions/repositories/postgres/read.rb
luna_park-0.11.6 lib/luna_park/extensions/repositories/postgres/read.rb
luna_park-0.11.5 lib/luna_park/extensions/repositories/postgres/read.rb
luna_park-0.11.4 lib/luna_park/extensions/repositories/postgres/read.rb
luna_park-0.11.3 lib/luna_park/extensions/repositories/postgres/read.rb
luna_park-0.11.2 lib/luna_park/extensions/repositories/postgres/read.rb
luna_park-0.11.1 lib/luna_park/extensions/repositories/postgres/read.rb
luna_park-0.11.0 lib/luna_park/extensions/repositories/postgres/read.rb