Sha256: 9d3184090db40f2a68befdd19da8ee7b24864932ea6f47c7fe6d1b6504b31845

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

require "sequel"

module Sequel
  class Dataset
    # Returns a copy of the dataset with the select statements
    # for the given aliased columns replacing the original selects.
    # If no aliases are given, it will return the existing selection.
    # If no columns are currently selected, it will select *.
    def replace_select_with_alias(*columns, &block)
      virtual_row_columns(columns, block)
      aliased_columns = _aliased_columns(columns)
      return self if !@opts[:select] || (@opts[:select] & aliased_columns.keys).empty?

      select(*_replace_aliases(@opts[:select], aliased_columns))
    end

    def _aliased_columns(columns)
      columns.reduce({}) do |aliased_columns, column|
        case column.alias
        when nil
        when Sequel::SQL::Identifier
          aliased_columns[column.alias.value.to_sym] = column
        else
          aliased_columns[column.alias] = column
        end

        aliased_columns
      end
    end

    def _replace_aliases(selects, aliased_columns)
      selects.map { |select| aliased_columns[select] || select }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sequel_replace_select_with_alias-0.0.2 lib/sequel_replace_select_with_alias.rb
sequel_replace_select_with_alias-0.0.1 lib/sequel_replace_select_with_alias.rb