Class: CSSPushServer

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCSSPushServer

Returns a new instance of CSSPushServer.



13
14
15
16
17
18
# File 'lib/css_push_server.rb', line 13

def initialize
  css_root = File.join(ROOT, "public", "stylesheets")

  self.directories = [css_root]
  self.last_mtime = Time.now
end

Instance Attribute Details

#directoriesObject

Returns the value of attribute directories.



11
12
13
# File 'lib/css_push_server.rb', line 11

def directories
  @directories
end

#last_mtimeObject

Returns the value of attribute last_mtime.



11
12
13
# File 'lib/css_push_server.rb', line 11

def last_mtime
  @last_mtime
end

Class Method Details

.runObject



7
8
9
# File 'lib/css_push_server.rb', line 7

def self.run
  self.new.run
end

Instance Method Details

#broadcast_changes(files) ⇒ Object



38
39
40
41
42
43
# File 'lib/css_push_server.rb', line 38

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

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

#find_filesObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/css_push_server.rb', line 57

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



49
50
51
52
53
54
55
# File 'lib/css_push_server.rb', line 49

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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/css_push_server.rb', line 20

def run
  if (!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



45
46
47
# File 'lib/css_push_server.rb', line 45

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