Sha256: e4e64a6c677422f205928f152ab4a09c0f405274bdeb5ba47191384102be2ba6

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

module RailsSqlViews4
  module ConnectionAdapters #:nodoc:
    # Abstract definition of a View
    class ViewDefinition
      attr_accessor :columns, :select_query
      
      def initialize(base, select_query)
        @columns = []
        @base = base
        @select_query = select_query
      end
      
      def column(name)
        column = name.to_s
        @columns << column unless @columns.include? column
        self
      end
      
      def to_sql
        @columns.collect { |c| @base.quote_column_name(c) } * ', '
      end
      
    end
    
    class MappingDefinition
      
      # Generates a hash of the form :old_column => :new_column
      # Initially, it'll map column names to themselves.
      # use map_column to modify the list.
      def initialize(columns)
        @columns = columns
        @map = Hash.new()
        columns.each do |c|
          @map[c] = c
        end
        
      end
      
      # Create a mapping from an old column name to a new one.
      # If the new name is nil, specify that the old column shouldn't
      # appear in this new view.
      def map_column(old_name, new_name)
        unless @map.include?(old_name)
          raise ActiveRecord::ActiveRecordError, "column #{old_name} not found, can't be mapped"
        end
        if new_name.nil?
          @map.delete old_name
          @columns.delete old_name
        else
          @map[old_name] = new_name
        end
      end
      
      def select_cols
        @columns
      end
      
      def view_cols
        @columns.map { |c| @map[c] }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rails_sql_views4-0.0.3 lib/rails_sql_views4/connection_adapters/abstract/schema_definitions.rb
rails_sql_views4-0.0.2 lib/rails_sql_views4/connection_adapters/abstract/schema_definitions.rb
rails_sql_views4-0.0.1 lib/rails_sql_views4/connection_adapters/abstract/schema_definitions.rb