Class: MachineGun

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

Overview

An automatic reloading Rack development web server for Ruby.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMachineGun

Returns a new instance of MachineGun.



13
14
15
# File 'lib/machinegun.rb', line 13

def initialize
  @running = false
end

Class Method Details

.run(*args) ⇒ Object

Helper method to instantiate a new object and run the server.

See Also:



9
10
11
# File 'lib/machinegun.rb', line 9

def self.run *args
  new.run *args
end

Instance Method Details

#run(opts = {}) ⇒ Object

Run the automatically-reloading web server. This method blocks.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :interval (Numeric) — default: 0.5

    Interval in seconds to scan the filesystem for changes.



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/machinegun.rb', line 21

def run opts = {}
  @running = true
  interval = opts[:interval] || 0.5
   
  @pid = start_server
   
  @watcher = FileWatcher.new("./**/*.rb")
  
  @watcher.watch interval do
    stop_server
    @pid = start_server
  end
end

#running?Boolean

Returns true if the server is running.

Returns:

  • (Boolean)

    true if the server is running.



45
46
47
# File 'lib/machinegun.rb', line 45

def running?
  @running
end

#stopObject

Stop watching for file changes and shutdown the web server.



36
37
38
39
40
41
42
# File 'lib/machinegun.rb', line 36

def stop
  return unless @running
  
  @watcher.stop
  stop_server
  @running = false
end