Class: Brant::MigrationFile

Inherits:
Object
  • Object
show all
Defined in:
lib/brant/migration_file.rb

Overview

Representation of the actual file that stores a migration

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ MigrationFile

Constructor logic



60
61
62
63
64
65
66
# File 'lib/brant/migration_file.rb', line 60

def initialize(filename)
  self.filename   = filename

  parts           = self.filename.split('_')
  self.timestamp  = parts.last.split('.').first
  self.klass      = parts[0..-2].join('_').camelize
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



7
8
9
# File 'lib/brant/migration_file.rb', line 7

def filename
  @filename
end

#klassObject

Returns the value of attribute klass.



7
8
9
# File 'lib/brant/migration_file.rb', line 7

def klass
  @klass
end

#timestampObject

Returns the value of attribute timestamp.



7
8
9
# File 'lib/brant/migration_file.rb', line 7

def timestamp
  @timestamp
end

Class Method Details

.allObject

Returns hash of migration file objects of all defnied migrations with their timestamps as the keys



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/brant/migration_file.rb', line 12

def self.all
  migrations = {}

  if File.exists? Brant.migrationsPath
    Dir.entries(Brant.migrationsPath).each do |file|
      next if file.length < 3 || file.match(/^\./)
      migration = new(file)
      migrations[migration.timestamp] = migration
    end
  end

  migrations
end

.existingObject

Returns a hash of migration timestamps that have been performed



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/brant/migration_file.rb', line 29

def self.existing
  migrations = {}
  
  if File.exists? Brant.migrationsConfigPath
    File.open(Brant.migrationsConfigPath,'r') do |file|
      while(line = file.gets)
        migrations[line.chomp] = true
      end
    end
  end

  migrations
end

.update(migrations) ⇒ Object

Update migrations config to show all finished migrations



45
46
47
48
49
# File 'lib/brant/migration_file.rb', line 45

def self.update(migrations)
  File.open(Brant.migrationsConfigPath,'w') do |file|
    file.write migrations.join("\n")
  end
end

Instance Method Details

#<=>(other) ⇒ Object

Compare objects based on timestamp



54
55
56
# File 'lib/brant/migration_file.rb', line 54

def <=>(other)
  self.timestamp <=> other.timestamp
end