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.



34
35
36
37
38
39
40
41
# File 'lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb', line 34

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.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb', line 46

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



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

def select_cols
  @columns
end

#view_colsObject



62
63
64
# File 'lib/rails_sql_views/connection_adapters/abstract/schema_definitions.rb', line 62

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