Class: Watchr::Controller

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

Overview

The controller contains the app’s core logic.

Examples
script = Watchr::Script.new(file)
contrl = Watchr::Controller.new(script)
contrl.run

Calling #run will enter the listening loop, and from then on every file event will trigger its corresponding action defined in script

The controller also automatically adds the script’s file itself to its list of monitored files and will detect any changes to it, providing on the fly updates of defined rules.

Instance Method Summary collapse

Constructor Details

#initialize(script) ⇒ Controller

Creates a controller object around given script

Parameters
script<Script>

The script object



38
39
40
# File 'lib/watchr/controller.rb', line 38

def initialize(script)
  @script  = script
end

Instance Method Details

#handlerObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/watchr/controller.rb', line 22

def handler
  @handler ||= begin
                 handler = Watchr.handler.new
                 handler.add_observer self
                 Watchr.debug "using %s handler" % handler.class.name
                 Script.handler = handler
                 handler
               end
  @handler
end

#monitored_pathsObject

List of paths the script is monitoring.

Basically this means all paths below current directoly recursivelly that match any of the rules’ patterns, plus the script file.

Returns
paths<Array>

List of monitored paths



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/watchr/controller.rb', line 85

def monitored_paths
  paths = Dir['**/*'].select do |path|
    watch = false
    @script.rules.reverse.each do |r|
      rule_watches = r.watch(path)
      if false
        $stderr.print "watch ", path, " ", rule_watches, "\n"
      end
      next if rule_watches.nil?
      watch = rule_watches
      break
    end
    watch
  end
  paths.each do |path|
    # $stderr.print "lookup #{path}\n"
    @script.depends_on(path).each do |dependence|
      # $stderr.print "add #{dependence} for #{path}\n"
      paths << dependence
    end
  end
  paths.push(@script.path).compact!
  paths.uniq!
  # $stderr.print "watch #{paths.map {|path| Pathname(path).expand_path }.join(' ')}\n"
  paths.map {|path| Pathname(path).expand_path }
end

#runObject

Enters listening loop.

Will block control flow until application is explicitly stopped/killed.



46
47
48
49
50
# File 'lib/watchr/controller.rb', line 46

def run
  @script.parse!
  handler.listen(monitored_paths)
rescue Interrupt
end

#update(path, event_type = nil) ⇒ Object

Callback for file events.

Called while control flow in in listening loop. It will execute the file’s corresponding action as defined in the script. If the file is the script itself, it will refresh its state to account for potential changes.

Parameters
path<Pathname, String>

path that triggered event

event<Symbol>

event type (ignored for now)



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/watchr/controller.rb', line 62

def update(path, event_type = nil)
  path = Pathname(path).expand_path
  # p path, event_type
  if path == @script.path && ![ :load, :deleted, :moved ].include?(event_type)
    @script.parse!
    handler.refresh(monitored_paths)
  else
    begin
      @script.call_action_for(path, event_type)
    rescue Refresh => refresh
      handler.refresh(monitored_paths)
    end
  end
end