Sha256: 8116972ddc2665a97348d8f731d8ab241d21d20746b1f177aeb5d84dc427881f

Contents?: true

Size: 1.02 KB

Versions: 5

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

class PGTrunk::Operation
  # @private
  # Add helpers for building SQL queries
  module SQLHelpers
    extend ActiveSupport::Concern

    class_methods do
      include Enumerable

      # Get/set the block to extract operation definitions
      # from the database.
      # @yield [Proc] the block returning sql
      # @yieldparam [#to_s] version The current version of the database
      def from_sql(&block)
        @from_sql = block if block
        @from_sql ||= nil
      end

      # Iterate by sorted operation definitions
      # extracted from the database
      def each(&block)
        return to_enum unless block_given?

        fetch
          .map { |item| new(**item.symbolize_keys) }
          .sort
          .each { |op| block.call(op) }
      end

      private

      def fetch
        query = from_sql&.call(PGTrunk.database.server_version)
        query.blank? ? [] : PGTrunk.database.execute(query)
      end
    end

    def quote(str)
      PGTrunk.database.quote(str)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pg_trunk-0.2.0 lib/pg_trunk/core/operation/sql_helpers.rb
pg_trunk-0.1.3 lib/pg_trunk/core/operation/sql_helpers.rb
pg_trunk-0.1.2 lib/pg_trunk/core/operation/sql_helpers.rb
pg_trunk-0.1.1 lib/pg_trunk/core/operation/sql_helpers.rb
pg_trunk-0.1.0 lib/pg_trunk/core/operation/sql_helpers.rb