Class: Migraine::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/migraine/map.rb

Direct Known Subclasses

Migration

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_and_destination) ⇒ Map

Sets the source and destination to use for mapping. Takes a Hash containing a single element as argument, where key specifies source and value specifies destination.

This constructor should not be used publicly, but rather through the ‘Migraine::Map#map` DSL method which adds instances to the @maps array.

Migraine::Map.new 'source_table' => 'destinatin_table'

Parameters:

  • Hash (Hash)

    containing source and destination.



18
19
20
21
22
23
24
25
# File 'lib/migraine/map.rb', line 18

def initialize(source_and_destination)
  begin
    set_source_and_destination_from(source_and_destination)
  rescue ArgumentError => e
    puts e.message
    exit
  end
end

Instance Attribute Details

#destinationObject

Returns the value of attribute destination.



4
5
6
# File 'lib/migraine/map.rb', line 4

def destination
  @destination
end

#mapsObject

Returns the value of attribute maps.



5
6
7
# File 'lib/migraine/map.rb', line 5

def maps
  @maps
end

#sourceObject

Returns the value of attribute source.



3
4
5
# File 'lib/migraine/map.rb', line 3

def source
  @source
end

Instance Method Details

#map(source_and_destination, &block) ⇒ Object

Adds a new map to the @maps array. In other words, maps an old location to a new location one level below the current, be it at database, table or column level. This lets us have nested Maps, using the same class for any ‘level’.

For example, if the current source and destination are databases, this will map a source table to a destination table within that database. If current is a table, it will map column names.

It accepts a block as optional argument, which will be evaluated against the new Map instance so that ‘#map` calls can be nested using a more convenient DSL.

Usage in migration/mapping DSL:

# Map#map at database-level, mapping table names
@migration.map 'source_table' => 'destination_table' do
  # Map#map at table-level, mapping column names
  map 'source_column' => 'destination_column'
end


48
49
50
51
52
53
54
55
56
57
# File 'lib/migraine/map.rb', line 48

def map(source_and_destination, &block)
  @maps ||= []
  map = Migraine::Map.new(source_and_destination)
  
  if block_given?
    map.instance_eval(&block)
  end

  @maps << map
end