Class: Cassanity::MigrationProxy
- Inherits:
-
Object
- Object
- Cassanity::MigrationProxy
- Includes:
- Comparable
- Defined in:
- lib/cassanity/migration_proxy.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Public: The name of the migration.
-
#path ⇒ Object
readonly
Public: The full path to the migration on disk.
-
#version ⇒ Object
readonly
Public: The version of the migration.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #build_migration(migrator) ⇒ Object
- #constant ⇒ Object
- #down(migrator) ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
- #hash ⇒ Object
-
#initialize(path) ⇒ MigrationProxy
constructor
A new instance of MigrationProxy.
- #log(migrator) ⇒ Object
- #migration_class ⇒ Object
- #up(migrator) ⇒ Object
Constructor Details
#initialize(path) ⇒ MigrationProxy
Returns a new instance of MigrationProxy.
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
#name ⇒ Object (readonly)
Public: The name of the migration.
14 15 16 |
# File 'lib/cassanity/migration_proxy.rb', line 14 def name @name end |
#path ⇒ Object (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 |
#version ⇒ Object (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 |
#constant ⇒ Object
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: ==
67 68 69 |
# File 'lib/cassanity/migration_proxy.rb', line 67 def eql?(other) self.class.eql?(other.class) && path == other.path end |
#hash ⇒ Object
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_class ⇒ Object
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 |