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

- (MachineGun) initialize

Returns a new instance of MachineGun



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

def initialize
  @running = false
end

Class Method Details

+ (Object) run(*args)

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

- (Object) run(opts = {})

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

- (Boolean) running?

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

- (Object) stop

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