Module: Reloadlive

Defined in:
lib/reloadlive/config.rb,
lib/reloadlive/render.rb,
lib/reloadlive/options.rb,
lib/reloadlive/version.rb,
lib/reloadlive/frontend.rb

Defined Under Namespace

Classes: Frontend, Render

Constant Summary collapse

VERSION =
"1.0.4"
@@options =
{ 'port' => 4567, 'host' => '0.0.0.0', 'static' => Dir.pwd }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.optionsObject



51
52
53
# File 'lib/reloadlive/options.rb', line 51

def self.options
  @@options
end

Instance Method Details

#binary?(file) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/reloadlive/config.rb', line 21

def binary? file
  s = (File.read(file, File.stat(file).blksize) || "").split(//)
  ((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30
end

#builder(port) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/reloadlive/config.rb', line 7

def builder port
  thread
  Rack::Builder.new do
    map "/" do
      run Frontend
    end
    Faye::WebSocket.load_adapter('thin')
    faye = Faye::RackAdapter.new :mount => '/', :timeout => 45
    map "/faye" do
      run faye
    end
  end
end

#dirs(stars = false) ⇒ Object



26
27
28
29
30
31
# File 'lib/reloadlive/config.rb', line 26

def dirs(stars=false)
  dirs = options['watch'].dup
  dirs.map! do |dir|
    File.absolute_path(dir) + (stars ? "/**/*" : "")
  end
end

#last_file_changedObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/reloadlive/config.rb', line 33

def last_file_changed
  timestamp = 0
  last_file_changed = nil
  Dir.glob(dirs(true)).each do |file|
    next unless File.file? file
    next if binary? file
    ts = File.stat(file).mtime.to_i
    if ts > timestamp
      last_file_changed = file
      timestamp = ts
    end
  end
  last_file_changed
end

#optionsObject



47
48
49
# File 'lib/reloadlive/options.rb', line 47

def options
  @@options
end

#threadObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/reloadlive/config.rb', line 48

def thread
  t = Thread.new do
    client = Faye::Client.new("http://localhost:#{options['port']}/faye")
    listener = Listen::Listener.new(*dirs) do |modified, added, removed|
      filename = modified.first ? modified.first    :
                    added.first ? added.first       :
                                  last_file_changed
      return if binary? filename
      render = Render.new(File.basename(filename), File.read(filename))
      client.publish('/message', {'body' => render.formatted_data, 'title' => render.title })
      puts "PUSH " + File.basename(filename)
    end
    listener.start
  end
  t.abort_on_exception = true
end