Class: Guard::Interactor

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

Overview

The interactor reads user input and triggers specific action upon them unless its locked.

Currently the following actions are implemented:

  • stop, quit, exit, s, q, e => Exit Guard

  • reload, r, z => Reload Guard

  • pause, p => Pause Guard

  • Everything else => Run all

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

Examples:

‘backend reload` will only reload backend group

‘spork reload` will only reload rspec guard

‘jasmine` will only run all jasmine specs

Constant Summary collapse

STOP_ACTIONS =
%w[stop quit exit s q e]
RELOAD_ACTIONS =
%w[reload r z]
PAUSE_ACTIONS =
%w[pause p]

Instance Method Summary collapse

Instance Method Details

#action_from_entry(entry) ⇒ Symbol

Extract action from entry if an existing action is present

Parameters:

  • Interactor (String)

    entry gets from $stdin

Returns:

  • (Symbol)

    A guard action



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

def action_from_entry(entry)
  if STOP_ACTIONS.include?(entry)
    :stop
  elsif RELOAD_ACTIONS.include?(entry)
    :reload
  elsif PAUSE_ACTIONS.include?(entry)
    :pause
  end
end

#extract_scopes_and_action(entry) ⇒ Array

Extract guard or group scope and action from Interactor entry

Examples:

‘spork reload` will only reload rspec

‘jasmine` will only run all jasmine specs

Parameters:

  • Interactor (String)

    entry gets from $stdin

Returns:

  • (Array)

    entry group or guard scope hash and action



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/guard/interactor.rb', line 66

def extract_scopes_and_action(entry)
  scopes  = {}
  entries = entry.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

  [scopes, action]
end

#scopes_from_entry(entry) ⇒ Hash

Extract guard or group scope from entry if valid

Parameters:

  • Interactor (String)

    entry gets from $stdin

Returns:

  • (Hash)

    An hash with a guard or a group scope



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/guard/interactor.rb', line 88

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

#startObject

Start the interactor in its own thread.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/guard/interactor.rb', line 27

def start
  return if ENV["GUARD_ENV"] == 'test'

  if !@thread || !@thread.alive?
    @thread = Thread.new do
      while entry = $stdin.gets.chomp
        scopes, action = extract_scopes_and_action(entry)
        case action
        when :stop
          ::Guard.stop
        when :pause
          ::Guard.pause
        when :reload
          ::Guard::Dsl.reevaluate_guardfile if scopes.empty?
          ::Guard.reload(scopes)
        when :run_all
          ::Guard.run_all(scopes)
        end
      end
    end
  end
end

#stopObject

Kill interactor thread if not current



52
53
54
55
56
# File 'lib/guard/interactor.rb', line 52

def stop
  unless Thread.current == @thread
    @thread.kill
  end
end