Class: Migratrix::Migration

Inherits:
Object show all
Includes:
ActiveModel::AttributeMethods, Callbacks, Loggable, MigrationStrategy, ValidOptions
Defined in:
lib/migratrix/migration.rb

Overview

Superclass for all migrations. Migratrix COULD check to see that a loaded migration inherits from this class, but hey, duck typing.

Constant Summary

Constants included from Callbacks

Callbacks::CALLBACKS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Callbacks

#extract, #load, #migrate, #transform

Methods included from MigrationStrategy

#extract, #load, #migrate, #transform

Constructor Details

#initialize(options = {}) ⇒ Migration

Returns a new instance of Migration.



16
17
18
19
# File 'lib/migratrix/migration.rb', line 16

def initialize(options={})
  @options = options.deep_copy.symbolize_keys
  Migratrix.log_to($stdout) if @options[:console]
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



13
14
15
# File 'lib/migratrix/migration.rb', line 13

def options
  @options
end

Class Method Details

.extend_extraction(extraction_name, options = {}) ⇒ Object



52
53
54
55
56
57
# File 'lib/migratrix/migration.rb', line 52

def self.extend_extraction(extraction_name, options={})
  migration = ancestors.detect {|k| k.respond_to?(:extractions) && k.extractions[extraction_name]}
  raise ExtractionNotDefined.new("Could not extend extractar '%s'; no parent Migration defines it" % extraction_name) unless migration
  extraction = migration.extractions[extraction_name]
  extractions[extraction_name] = extraction.class.new(extraction_name, extraction.options.merge(options))
end

.extend_load(load_name, options = {}) ⇒ Object

Raises:



92
93
94
95
96
97
# File 'lib/migratrix/migration.rb', line 92

def self.extend_load(load_name, options={})
  migration = ancestors.detect {|k| k.respond_to?(:loads) && k.loads[load_name]}
  raise LoadNotDefined.new("Could not extend extractar '%s'; no parent Migration defines it" % load_name) unless migration
  load = migration.loads[load_name]
  loads[load_name] = load.class.new(load_name, load.options.merge(options))
end

.extend_transform(transform_name, options = {}) ⇒ Object



72
73
74
75
76
77
# File 'lib/migratrix/migration.rb', line 72

def self.extend_transform(transform_name, options={})
  migration = ancestors.detect {|k| k.respond_to?(:transforms) && k.transforms[transform_name]}
  raise TransformNotDefined.new("Could not extend extractar '%s'; no parent Migration defines it" % transform_name) unless migration
  transform = migration.transforms[transform_name]
  transforms[transform_name] = transform.class.new(transform_name, transform.options.merge(options))
end

.extractionsObject



59
60
61
# File 'lib/migratrix/migration.rb', line 59

def self.extractions
  @extractions ||= {}
end

.loadsObject



99
100
101
# File 'lib/migratrix/migration.rb', line 99

def self.loads
  @loads ||= {}
end

.set_extraction(extraction_name, class_name, options = {}) ⇒ Object

extraction crap



48
49
50
# File 'lib/migratrix/migration.rb', line 48

def self.set_extraction(extraction_name, class_name, options={})
  extractions[extraction_name] = Migratrix.extraction(class_name, extraction_name, options)
end

.set_load(name, type, options = {}) ⇒ Object

load crap



88
89
90
# File 'lib/migratrix/migration.rb', line 88

def self.set_load(name, type, options={})
  loads[name] = Migratrix.load(name, type, options)
end

.set_transform(name, type, options = {}) ⇒ Object

transform crap



68
69
70
# File 'lib/migratrix/migration.rb', line 68

def self.set_transform(name, type, options={})
  transforms[name] = Migratrix.transform(name, type, options)
end

.transformsObject



79
80
81
# File 'lib/migratrix/migration.rb', line 79

def self.transforms
  @transforms ||= {}
end

.valid_optionsObject

TODO: Technically, we need to ask our extractions, transformers and loaders for THEIR valid options as well. limit, offset, order and where are all extraction-only options, and fetchall is an ActiveRecord-specific option



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/migratrix/migration.rb', line 25

def self.valid_options
  opts = super # wacky, I know, but the extended ValidOptions module is in the super chain. (I <3 Ruby)
  if extractions
    extractions.each do |name, extraction|
      opts += extraction.valid_options
    end
  end
  if transforms
    transforms.each do |name, transform|
      opts += transform.valid_options
    end
  end
#       if loads
#         loads.each do |name, load|
#           opts += load.valid_options
#         end
#       end
  opts.uniq.sort
end

Instance Method Details

#extractionsObject



63
64
65
# File 'lib/migratrix/migration.rb', line 63

def extractions
  self.class.extractions
end

#loadsObject



103
104
105
# File 'lib/migratrix/migration.rb', line 103

def loads
  self.class.loads
end

#transformsObject



83
84
85
# File 'lib/migratrix/migration.rb', line 83

def transforms
  self.class.transforms
end