Class: Cassanity::MigrationProxy

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/cassanity/migration_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ MigrationProxy

Returns a new instance of MigrationProxy.

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cassanity/migration_proxy.rb', line 16

def initialize(path)
  raise ArgumentError, "path cannot be nil" if path.nil?

  basename = File.basename(path, '.rb')
  version, name = basename.split('_', 2)

  raise ArgumentError, "version cannot be nil" if version.nil?
  raise ArgumentError, "name cannot be nil" if name.nil?

  @path = Pathname(path)
  @version = version.to_i
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Public: The name of the migration.



14
15
16
# File 'lib/cassanity/migration_proxy.rb', line 14

def name
  @name
end

#pathObject (readonly)

Public: The full path to the migration on disk.



8
9
10
# File 'lib/cassanity/migration_proxy.rb', line 8

def path
  @path
end

#versionObject (readonly)

Public: The version of the migration.



11
12
13
# File 'lib/cassanity/migration_proxy.rb', line 11

def version
  @version
end

Instance Method Details

#<=>(other) ⇒ Object



72
73
74
# File 'lib/cassanity/migration_proxy.rb', line 72

def <=>(other)
  @path <=> other.path
end

#build_migration(migrator) ⇒ Object



47
48
49
# File 'lib/cassanity/migration_proxy.rb', line 47

def build_migration(migrator)
  migration_class.new(migrator)
end

#constantObject



59
60
61
# File 'lib/cassanity/migration_proxy.rb', line 59

def constant
  name.split('_').map { |word| word.capitalize }.join('')
end

#down(migrator) ⇒ Object



34
35
36
# File 'lib/cassanity/migration_proxy.rb', line 34

def down(migrator)
  log(migrator) { build_migration(migrator).down }
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


67
68
69
# File 'lib/cassanity/migration_proxy.rb', line 67

def eql?(other)
  self.class.eql?(other.class) && path == other.path
end

#hashObject



63
64
65
# File 'lib/cassanity/migration_proxy.rb', line 63

def hash
  path.hash
end

#log(migrator) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/cassanity/migration_proxy.rb', line 38

def log(migrator)
  migrator.log "== #{@name}: migrating ".ljust(80, "=")
  start = Time.now
  result = yield
  duration = (Time.now - start).round(3)
  migrator.log "== #{@name}: migrated (#{duration}s) ".ljust(80, "=")
  result
end

#migration_classObject



51
52
53
54
55
56
57
# File 'lib/cassanity/migration_proxy.rb', line 51

def migration_class
  @migration_class ||= begin
    require path
    # TODO: handle constant not found
    Kernel.const_get(constant)
  end
end

#up(migrator) ⇒ Object



30
31
32
# File 'lib/cassanity/migration_proxy.rb', line 30

def up(migrator)
  log(migrator) { build_migration(migrator).up }
end