Class: Pushify::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/pushify/server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



14
15
16
17
18
19
20
21
22
# File 'lib/pushify/server.rb', line 14

def initialize
  self.directories = [
    File.join(ROOT, "public", "stylesheets"),
    File.join(ROOT, "public", "images"),
    File.join(ROOT, "public", "javascripts"),
    File.join(ROOT, "app", "stylesheets")
  ]
  self.last_mtime = Time.now
end

Instance Attribute Details

#directoriesObject

Returns the value of attribute directories.



12
13
14
# File 'lib/pushify/server.rb', line 12

def directories
  @directories
end

#last_mtimeObject

Returns the value of attribute last_mtime.



12
13
14
# File 'lib/pushify/server.rb', line 12

def last_mtime
  @last_mtime
end

Class Method Details

.runObject



8
9
10
# File 'lib/pushify/server.rb', line 8

def self.run
  self.new.run
end

Instance Method Details

#broadcast_changes(files) ⇒ Object



42
43
44
45
46
47
# File 'lib/pushify/server.rb', line 42

def broadcast_changes(files)
  puts "\n\nBroadcasting updates for: \n"
  puts files.values.map{|d| d[:rio_name]}.join(", ")

  Pushify::Juggernaut.send_to_all("Pushify.touch([#{ files.values.map{|d| "'" + d[:rio_name] + "'"}.join(", ") }])")
end

#find_filesObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pushify/server.rb', line 61

def find_files
  result = {}
  targets = self.directories

  # from ZenTest
  targets.each do |target|
    Find.find(target) do |f|

      next if test ?d, f
      next if f =~ /(swp|~|rej|orig)$/ # temporary/patch files
      next if f =~ /(\.svn)/           # svn files
      next if f =~ /\/\.?#/            # Emacs autosave/cvs merge files
      next if f =~ /\.DS_Store/        # OSX metadata

      filename = f.sub(/^\.\//, '')
    
      rio_name = Regexp.new("^#{Regexp.escape(target)}(.*)").match(filename)[1]
      result[filename] = { :mtime => File.stat(filename).mtime, :rio_name => rio_name } rescue next
    end
  end

  return result
end

#find_files_to_broadcastObject



53
54
55
56
57
58
59
# File 'lib/pushify/server.rb', line 53

def find_files_to_broadcast
  files = find_files
  files.each do |filename, data|
    files.delete(filename) unless self.last_mtime < data[:mtime]
  end
  files
end

#runObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pushify/server.rb', line 24

def run
  if (!Pushify::Juggernaut)
    puts "Juggernaut needs to be running for autospec to work"
    return
  end
  begin
    loop do
      wait_for_changes
      files = find_files_to_broadcast
      self.last_mtime = files.values.map {|d| d[:mtime] }.max
      broadcast_changes(files)
    end
  rescue Interrupt
    puts
    # Quit with ^C
  end
end

#wait_for_changesObject



49
50
51
# File 'lib/pushify/server.rb', line 49

def wait_for_changes
  Kernel.sleep 1 until !find_files_to_broadcast.empty?
end