Sha256: 7a2fce6321dc4028f9bfbf369a9fbe3a1a6eb35866f1ccefaa873e8dcd57e2cb

Contents?: true

Size: 999 Bytes

Versions: 2

Compression:

Stored size: 999 Bytes

Contents

module ROM
  module SQL

    # @private
    class Header
      include Charlatan.new(:columns)
      include Equalizer.new(:columns, :table)

      attr_reader :table

      def initialize(columns, table)
        super
        @table = table
      end

      def to_ary
        columns
      end
      alias_method :to_a, :to_ary

      def to_h
        columns.each_with_object({}) { |col, h|
          left, right = col.to_s.split('___')
          h[left.to_sym] = (right || left).to_sym
        }
      end

      def project(*names)
        find_all { |col| names.include?(col) }
      end

      def qualified
        map { |col| :"#{table}__#{col}" }
      end

      def rename(options)
        map { |col|
          new_name = options[col]

          if new_name
            :"#{col}___#{new_name}"
          else
            col
          end
        }
      end

      def prefix(col_prefix)
        rename(Hash[map { |col| [col, :"#{col_prefix}_#{col}"] }])
      end

    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rom-sql-0.2.0 lib/rom/sql/header.rb
rom-sql-0.1.1 lib/rom/sql/header.rb