Class: RailsSqlViews::ConnectionAdapters::MappingDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb

Instance Method Summary collapse

Constructor Details

#initialize(columns) ⇒ 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.



30
31
32
33
34
35
36
37
# File 'lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb', line 30

def initialize(columns)
  @columns = columns
  @map = Hash.new()
  columns.each do |c|
    @map[c] = c
  end
  
end

Instance Method Details

#map_column(old_name, new_name) ⇒ Object

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.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb', line 42

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

#select_colsObject



54
55
56
# File 'lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb', line 54

def select_cols
  @columns
end

#view_colsObject



58
59
60
# File 'lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb', line 58

def view_cols
  @columns.map { |c| @map[c] }
end