class Guard::Interactor


@abstract
jasmine
@example Run all jasmine specs
spork reload
@example Reload rspec guard
backend reload
@example Reload backend group
It’s also possible to scope ‘reload` and `run all` actions to only a specified group or a guard.
- <enter> => Run all
- n, notification => Toggle notifications
- p, pause => Toggle file modification listener
- r, reload => Reload Guard
q. quit => Exit Guard
- e, exit,
- h, help => Show help
Currently the following actions are implemented:
read by a interactor implementation.
The interactor triggers specific action from input

def self.auto_detect

Returns:
  • (Interactor) - an interactor implementation
def self.auto_detect
  require 'readline'
  if defined?(RbReadline) || defined?(JRUBY_VERSION) || RbConfig::CONFIG['target_os'] =~ /linux/i
    ReadlineInteractor.new
  else
    SimpleInteractor.new
  end
end

def self.fabricate

Returns:
  • (Interactor) - an interactor implementation
def self.fabricate
  case @interactor
  when :readline
    ReadlineInteractor.new
  when :simple
    SimpleInteractor.new
  when :off
    nil
  else
    auto_detect
  end
end

def self.interactor=(interactor)

Parameters:
  • interactor (Symbol) -- the name of the interactor
def self.interactor=(interactor)
  @interactor = interactor
end

def action_from_entry(entry)

Returns:
  • (Symbol) - a Guard action

Parameters:
  • entry (String) -- the possible action entry
def action_from_entry(entry)
  if STOP_ENTRIES.include?(entry)
    :stop
  elsif RELOAD_ENTRIES.include?(entry)
    :reload
  elsif PAUSE_ENTRIES.include?(entry)
    :pause
  elsif HELP_ENTRIES.include?(entry)
    :help
  elsif NOTIFICATION_ENTRIES.include?(entry)
    :notification
  end
end

def extract_scopes_and_action(line)

Returns:
  • (Array) - the group or guard scope and the action

Parameters:
  • line (String) -- the readline input

Other tags:
    Example: `jasmine` will only run all jasmine specs -
    Example: `spork reload` will only reload rspec -
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

def help


Show the help.
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

def process_input(line)

Parameters:
  • line (String) -- the input line
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

def read_line

Other tags:
    Abstract: -
def read_line
  raise NotImplementedError
end

def reload(scopes)

Parameters:
  • scopes (Hash) -- the reload scopes
def reload(scopes)
  ::Guard::UI.info 'Reload'
  ::Guard::Dsl.reevaluate_guardfile if scopes.empty?
  ::Guard.reload(scopes)
end

def scopes_from_entry(entry)

Returns:
  • (Hash) - a hash with a Guard or a group scope

Parameters:
  • entry (String) -- the possible scope entry
def scopes_from_entry(entry)
  scopes = { }
  if guard = ::Guard.guards(entry)
    scopes[:guard] = guard
  end
  if group = ::Guard.groups(entry)
    scopes[:group] = group
  end
  scopes
end

def start


Start the line reader in its own thread.
def start
  return if ENV['GUARD_ENV'] == 'test'
  @thread = Thread.new { read_line } if !@thread || !@thread.alive?
end

def stop


Kill interactor thread if not current
def stop
  return if ENV['GUARD_ENV'] == 'test'
  unless Thread.current == @thread
    @thread.kill
  end
end

def toggle_notification


Toggle the system notifications on/off
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