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.



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

def initialize(root, migrations_file_name = Monark::DEFAULT_MIGRATIONS_FILE_NAME)
  self.root                 = Pathname.new(root)
  self.migrations_file_name = migrations_file_name
  @migrations               = []
  @env                      = Monark.env
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

Returns the value of attribute env.



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

def env
  @env
end

#manifestObject



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

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



42
43
44
45
46
# File 'lib/monark/context.rb', line 42

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

#doneObject



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

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

#load_manifestObject



24
25
26
27
28
29
# File 'lib/monark/context.rb', line 24

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



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/monark/context.rb', line 48

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



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

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