Parent

Guard::Interactor

The interactor triggers specific action from input read by a interactor implementation.

Currently the following actions are implemented:

It’s also possible to scope `reload` and `run all` actions to only a specified group or a guard.

@example Reload backend group

backend reload

@example Reload rspec guard

spork reload

@example Run all jasmine specs

jasmine

@abstract

Public Class Methods

auto_detect() click to toggle source

Tries to detect an optimal interactor for the current environment.

It returns the Readline implementation when:

  • rb-readline is installed

  • The Ruby implementation is JRuby

  • The current OS is not Mac OS X

Otherwise the plain gets interactor is returned.

@return [Interactor] an interactor implementation

# File lib/guard/interactor.rb, line 79
def self.auto_detect
  require 'readline'

  if defined?(RbReadline) || defined?(JRUBY_VERSION) || RbConfig::CONFIG['target_os'] =~ /linux/
    ReadlineInteractor.new
  else
    SimpleInteractor.new
  end
end
fabricate() click to toggle source

Get an instance of the currently configured interactor implementation.

@return [Interactor] an interactor implementation

# File lib/guard/interactor.rb, line 53
def self.fabricate
  case @interactor
  when :readline
    ReadlineInteractor.new
  when :simple
    SimpleInteractor.new
  when :off
    nil
  else
    auto_detect
  end
end
interactor=(interactor) click to toggle source

Set the interactor implementation

@param [Symbol] interactor the name of the interactor

# File lib/guard/interactor.rb, line 44
def self.interactor=(interactor)
  @interactor = interactor
end

Public Instance Methods

extract_scopes_and_action(line) click to toggle source

Extract the Guard or group scope and action from the input line.

@example `spork reload` will only reload rspec @example `jasmine` will only run all jasmine specs

@param [String] line the readline input @return [Array] the group or guard scope and the action

# File lib/guard/interactor.rb, line 192
def extract_scopes_and_action(line)
  scopes  = { }
  entries = line.split(' ')

  case entries.length
  when 1
    unless action = action_from_entry(entries[0])
      scopes = scopes_from_entry(entries[0])
    end
  when 2
    scopes = scopes_from_entry(entries[0])
    action = action_from_entry(entries[1])
  end

  action = :run_all if !action && (!scopes.empty? || entries.empty?)

  [scopes, action]
end
help() click to toggle source

Show the help.

# File lib/guard/interactor.rb, line 163
def help
  puts ''
  puts '[e]xit, [q]uit   Exit Guard'
  puts '[p]ause          Toggle file modification listener'
  puts '[r]eload         Reload Guard'
  puts '[n]otification   Toggle notifications'
  puts '<enter>          Run all Guards'
  puts ''
  puts 'You can scope the reload action to a specific guard or group:'
  puts ''
  puts 'rspec reload     Reload the RSpec Guard'
  puts 'backend reload   Reload the backend group'
  puts ''
  puts 'You can also run only a specific Guard or all Guards in a specific group:'
  puts ''
  puts 'jasmine          Run the jasmine Guard'
  puts 'frontend         Run all Guards in the frontend group'
  puts ''
end
process_input(line) click to toggle source

Process the input from readline.

@param [String] line the input line

# File lib/guard/interactor.rb, line 118
def process_input(line)
  scopes, action = extract_scopes_and_action(line)

  case action
  when :help
    help
  when :stop
    ::Guard.stop
    exit
  when :pause
    ::Guard.pause
  when :reload
    reload(scopes)
  when :run_all
    ::Guard.run_all(scopes)
  when :notification
    toggle_notification
  else
    ::Guard::UI.error "Unknown command #{ line }"
  end
end
read_line() click to toggle source

Read the user input. This method must be implemented by each interactor implementation.

@abstract

# File lib/guard/interactor.rb, line 110
def read_line
  raise NotImplementedError
end
reload(scopes) click to toggle source

Execute the reload action.

@param [Hash] scopes the reload scopes

# File lib/guard/interactor.rb, line 144
def reload(scopes)
  ::Guard::UI.info 'Reload'
  ::Guard::Dsl.reevaluate_guardfile if scopes.empty?
  ::Guard.reload(scopes)
end
start() click to toggle source

Start the line reader in its own thread.

# File lib/guard/interactor.rb, line 91
def start
  return if ENV['GUARD_ENV'] == 'test'
  @thread = Thread.new { read_line } if !@thread || !@thread.alive?
end
stop() click to toggle source

Kill interactor thread if not current

# File lib/guard/interactor.rb, line 98
def stop
  return if ENV['GUARD_ENV'] == 'test'
  unless Thread.current == @thread
    @thread.kill
  end
end
toggle_notification() click to toggle source

Toggle the system notifications on/off

# File lib/guard/interactor.rb, line 152
def toggle_notification
  if ENV['GUARD_NOTIFY'] == 'true'
    ::Guard::UI.info 'Turn off notifications'
    ::Guard::Notifier.turn_off
  else
    ::Guard::Notifier.turn_on
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.