Sha256: ae5c464c205759b19bea24e7f556d82dcdbc20b7cceebbb095164166b506fbb2

Contents?: true

Size: 719 Bytes

Versions: 28

Compression:

Stored size: 719 Bytes

Contents

# frozen_string_literal: true

require "attr_extras"

module SQL
  # Example:
  #
  #     indexed_case_stmt(:code, "DT", "AC", "XY")
  #
  # Will return the string:
  #
  #    CASE code
  #       WHEN 'DT' THEN 1
  #       WHEN 'AC' THEN 2
  #    END
  #
  # Used for creating an explicit sort order in conjunction with a find:
  #
  #    Description.where(code: codes).order(indexed_case_stmt(:code, codes))
  #
  class IndexedCaseStmt
    pattr_initialize :column, :items

    def generate
      return if items.blank?
      clauses = []
      Array(items).each_with_index do |item, index|
        clauses << "WHEN '#{item}' THEN #{index}"
      end

      "CASE #{column} #{clauses.join(' ')} END"
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
renalware-core-2.0.46 lib/sql/index_case_stmt.rb
renalware-core-2.0.45 lib/sql/index_case_stmt.rb
renalware-core-2.0.44 lib/sql/index_case_stmt.rb
renalware-core-2.0.43 lib/sql/index_case_stmt.rb
renalware-core-2.0.42 lib/sql/index_case_stmt.rb
renalware-core-2.0.41 lib/sql/index_case_stmt.rb
renalware-core-2.0.40 lib/sql/index_case_stmt.rb
renalware-core-2.0.39 lib/sql/index_case_stmt.rb
renalware-core-2.0.38 lib/sql/index_case_stmt.rb
renalware-core-2.0.37 lib/sql/index_case_stmt.rb
renalware-core-2.0.36 lib/sql/index_case_stmt.rb
renalware-core-2.0.35 lib/sql/index_case_stmt.rb
renalware-core-2.0.34 lib/sql/index_case_stmt.rb
renalware-core-2.0.33 lib/sql/index_case_stmt.rb
renalware-core-2.0.32 lib/sql/index_case_stmt.rb
renalware-core-2.0.31 lib/sql/index_case_stmt.rb
renalware-core-2.0.30 lib/sql/index_case_stmt.rb
renalware-core-2.0.28 lib/sql/index_case_stmt.rb
renalware-core-2.0.27 lib/sql/index_case_stmt.rb
renalware-core-2.0.26 lib/sql/index_case_stmt.rb