Sha256: 6e4925b51aba5dfb01a6ab3cd5cbd2a7644466d374a7c121203c4749e82a71e0

Contents?: true

Size: 1.56 KB

Versions: 19

Compression:

Stored size: 1.56 KB

Contents

module RailsSqlViews
  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

19 entries across 19 versions & 8 rubygems

Version Path
mereghost-rails_sql_views-0.8.6 lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb
robdimarco_rails_sql_views-0.9.0 lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb
qubole_rails_sql_views-0.10.0 lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb
mereghost-rails_sql_views-0.8.5 lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb
mereghost-rails_sql_views-0.8.3 lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb
mereghost-rails_sql_views-0.8.2 lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb
mereghost-rails_sql_views-0.8.1 lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb
lunks-rails_sql_views-0.9.2 lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb
ctreatma-rails_sql_views-0.9.1 lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb
ctreatma-rails_sql_views-0.9.0 lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb
molo-0.7.6 vendor/rails_sql_views/lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb
molo-0.7.5 vendor/rails_sql_views/lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb
molo-0.7.4 vendor/rails_sql_views/lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb
molo-0.7.3 vendor/rails_sql_views/lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb
molo-0.7.2 vendor/rails_sql_views/lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb
molo-0.7.1 vendor/rails_sql_views/lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb
molo-0.7.0 vendor/rails_sql_views/lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb
rails_sql_views-0.8.0 lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb
rubaidh-rails_sql_views-0.7.1 lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb