Sha256: f6c17a1d58d3ecc624e3a6ccc2f2f341d597fed5b5a311d9f93a4d888c7f2754

Contents?: true

Size: 680 Bytes

Versions: 17

Compression:

Stored size: 680 Bytes

Contents

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
    def initialize(column, items)
      @column = column
      @items = items
    end

    def generate
      clauses = []
      @items.each_with_index do |item, index|
        clauses << "WHEN '#{item}' THEN #{index}"
      end

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

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
renalware-core-2.0.0.pre.rc9 lib/sql/index_case_stmt.rb
renalware-core-2.0.0.pre.rc8 lib/sql/index_case_stmt.rb
renalware-core-2.0.0.pre.rc7 lib/sql/index_case_stmt.rb
renalware-core-2.0.0.pre.rc6 lib/sql/index_case_stmt.rb
renalware-core-2.0.0.pre.rc5 lib/sql/index_case_stmt.rb
renalware-core-2.0.0.pre.rc4 lib/sql/index_case_stmt.rb
renalware-core-2.0.0.pre.rc3 lib/sql/index_case_stmt.rb
renalware-core-2.0.0.pre.rc1 lib/sql/index_case_stmt.rb
renalware-core-2.0.0.pre.beta12 lib/sql/index_case_stmt.rb
renalware-core-2.0.0.pre.beta11 lib/sql/index_case_stmt.rb
renalware-core-2.0.0.pre.beta10 lib/sql/index_case_stmt.rb
renalware-core-2.0.0.pre.beta9 lib/sql/index_case_stmt.rb
renalware-core-2.0.0.pre.beta8 lib/sql/index_case_stmt.rb
renalware-core-2.0.0.pre.beta7 lib/sql/index_case_stmt.rb
renalware-core-2.0.0.pre.beta6 lib/sql/index_case_stmt.rb
renalware-core-2.0.0.pre.beta5 lib/sql/index_case_stmt.rb
renalware-core-2.0.0.pre.beta4 lib/sql/index_case_stmt.rb