Class: Guard::Migrate

Inherits:
Plugin
  • Object
show all
Defined in:
lib/guard/migrate.rb,
lib/guard/migrate/notify.rb,
lib/guard/migrate/migration.rb

Defined Under Namespace

Classes: Migration, Notify

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Migrate

Returns a new instance of Migrate.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/guard/migrate.rb', line 9

def initialize(options = {})
  super

  @bundler = true unless options[:bundler] == false
  @cmd = options[:cmd].to_s unless options[:cmd].to_s.empty?
  @reset = true if options[:reset] == true
  @test_prepare = options[:test_prepare]
  @run_on_start = true if options[:run_on_start] == true
  @rails_env = options[:rails_env]
  @seed = options[:seed]
end

Instance Attribute Details

#rails_envObject (readonly)

Returns the value of attribute rails_env.



7
8
9
# File 'lib/guard/migrate.rb', line 7

def rails_env
  @rails_env
end

#seedObject (readonly)

Returns the value of attribute seed.



7
8
9
# File 'lib/guard/migrate.rb', line 7

def seed
  @seed
end

Instance Method Details

#bundler?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/guard/migrate.rb', line 21

def bundler?
  !!@bundler && File.exist?("#{Dir.pwd}/Gemfile")
end

#cmd?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/guard/migrate.rb', line 29

def cmd?
  !!@cmd
end

#migrate(migrations = []) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/guard/migrate.rb', line 81

def migrate(migrations = [])
  return if !reset? && migrations.empty?
  if reset?
    Compat::UI.info "Running #{rake_string}"
    result = system(rake_string)
    result &&= 'reset'
  else
    result = run_all_migrations(migrations)
  end

  Notify.new(result).notify
end

#rake_string(version = nil) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/guard/migrate.rb', line 105

def rake_string(version = nil)
  [
    bundler_command,
    custom_command,
    rake_command,
    migrate_string(version),
    seed_string,
    prepare_string,
    rails_env_string
  ].compact.join(' ')
end

#reloadObject

Called on Ctrl-Z signal This method should be mainly used for “reload” (really!) actions like reloading passenger/spork/bundler/…



61
62
63
# File 'lib/guard/migrate.rb', line 61

def reload
  migrate if run_on_start?
end

#reset?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/guard/migrate.rb', line 37

def reset?
  !!@reset
end

#run_allObject

Called on Ctrl-/ signal This method should be principally used for long action like running all specs/tests/…



67
68
69
# File 'lib/guard/migrate.rb', line 67

def run_all
  migrate if run_on_start?
end

#run_on_changes(paths) ⇒ Object

Called on file(s) modifications



72
73
74
75
76
77
78
79
# File 'lib/guard/migrate.rb', line 72

def run_on_changes(paths)
  if paths.any? { |path| path.match(%r{^db/migrate/(\d+).+\.rb}) } || reset?
    migrations = paths.map { |path| Migration.new(path) }
    migrate(migrations)
  elsif paths.any? { |path| path.match(%r{^db/seeds\.rb$}) }
    seed_only
  end
end

#run_on_start?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/guard/migrate.rb', line 25

def run_on_start?
  !!@run_on_start
end

#run_redo?(version) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/guard/migrate.rb', line 101

def run_redo?(version)
  !reset? && version && !version.empty?
end

#seed_onlyObject



94
95
96
97
98
99
# File 'lib/guard/migrate.rb', line 94

def seed_only
  Compat::UI.info "Running #{seed_only_string}"
  result = system(seed_only_string)
  result &&= 'seed'
  Notify.new(result).notify
end

#seed_only_stringObject



117
118
119
120
121
122
123
124
125
126
# File 'lib/guard/migrate.rb', line 117

def seed_only_string
  [
    bundler_command,
    custom_command,
    rake_command,
    seed_string,
    prepare_string,
    rails_env_string
  ].compact.join(' ')
end

#startObject

Called once when Guard starts Please override initialize method to init stuff



50
51
52
# File 'lib/guard/migrate.rb', line 50

def start
  migrate if run_on_start?
end

#stopObject

Called on Ctrl-C signal (when Guard quits)



55
56
57
# File 'lib/guard/migrate.rb', line 55

def stop
  true
end

#test_prepare?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/guard/migrate.rb', line 33

def test_prepare?
  !!@test_prepare
end