Class: Monark::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/monark/context.rb

Overview

Monark runtime. Encapsulates state and manages migrations.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, migrations_file_name = Monark::DEFAULT_MIGRATIONS_FILE_NAME) ⇒ Context

Returns a new instance of Context.



10
11
12
13
14
# File 'lib/monark/context.rb', line 10

def initialize(root, migrations_file_name = Monark::DEFAULT_MIGRATIONS_FILE_NAME)
  self.root                 = Pathname.new(root)
  self.migrations_file_name = migrations_file_name
  @migrations               = []
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



5
6
7
# File 'lib/monark/context.rb', line 5

def connection
  @connection
end

#envObject



16
17
18
# File 'lib/monark/context.rb', line 16

def env
  defined?(@env) ? @env : Monark.env
end

#manifestObject



20
21
22
# File 'lib/monark/context.rb', line 20

def manifest
  @manifest ||= root.join(migrations_file_name).read
end

#migrationsObject (readonly)

Returns the value of attribute migrations.



6
7
8
# File 'lib/monark/context.rb', line 6

def migrations
  @migrations
end

#migrations_file_nameObject

Returns the value of attribute migrations_file_name.



5
6
7
# File 'lib/monark/context.rb', line 5

def migrations_file_name
  @migrations_file_name
end

#rootObject

Returns the value of attribute root.



5
6
7
# File 'lib/monark/context.rb', line 5

def root
  @root
end

#versionObject

Returns the value of attribute version.



6
7
8
# File 'lib/monark/context.rb', line 6

def version
  @version
end

Instance Method Details

#assume_doneObject



46
47
48
49
50
# File 'lib/monark/context.rb', line 46

def assume_done
  load_manifest
  schema = Schema.new
  schema.next_migration = migrations.count
end

#doneObject



39
40
41
42
43
44
# File 'lib/monark/context.rb', line 39

def done
  load_manifest
  count = migrations.count
  schema = Schema.new
  schema.next_migration = count if count > schema.next_migration
end

#load_manifestObject



28
29
30
31
32
33
# File 'lib/monark/context.rb', line 28

def load_manifest
  @loaded_manifest ||= begin
    DSL.new(self).instance_eval(manifest, root.join(migrations_file_name).to_s, 0)
    true
  end
end

#migrate(options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/monark/context.rb', line 52

def migrate(options = {})
  load_manifest
  schema = Schema.new
  ActiveRecord::Base.transaction do
    migrations.each_with_index do |migration, index|
      if (index >= schema.next_migration) || (migration.reloadable? && options[:reload])
        migration.perform
      end
    end
    done
  end
end

#push(*args) ⇒ Object



35
36
37
# File 'lib/monark/context.rb', line 35

def push(*args)
  migrations.push(*args)
end