Sha256: e74e41ea062e936d130bffce27defe81184e6d41761e45c51565c1add87412c4

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

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 names
        map { |col| :"#{col.to_s.split('___').last}" }
      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

1 entries across 1 versions & 1 rubygems

Version Path
rom-sql-0.3.0 lib/rom/sql/header.rb