Class: Rack::Reshow

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

Defined Under Namespace

Classes: Page, RackStaticBugAvoider

Constant Summary collapse

@@store_file =
'reshow.ps'

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ Reshow

Returns a new instance of Reshow.



106
107
108
109
110
# File 'lib/rack/reshow.rb', line 106

def initialize( app, opts = {} )
  root = Object::File.expand_path(Object::File.dirname(__FILE__))
  @app = RackStaticBugAvoider.new(app, Rack::Static.new(app, :urls => ["/__reshow__"], :root => root))
  @store = PStore.new(@@store_file)
end

Instance Method Details

#[](path) ⇒ Object



151
152
153
# File 'lib/rack/reshow.rb', line 151

def []( path )
  @store.transaction(true) {|store| store[path] || Array.new}
end

#call(env) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/rack/reshow.rb', line 112

def call( env )
  request = Request.new(env)
  path = request.path
  return serve_stylesheet(request) if path =~ /__reshow__\/assets/
  status, headers, body = @app.call(env)
  if status == 200 and body.respond_to? :join
    body = body.join
    if body[/<head>.*?<\/head>/] and body[/<body>.*?<\/body>/]
      page = Page.new body, path, @app
      # Store response
      @store.transaction do |store|
        store[path] ||= []
        store[path] << page unless body.nil? or store[path].last.eql?(page)
      end
      # Insert Reshow assets
      page.append_to_tag '</head>', [style, javascript].join("\n")
      # Prepare for Reshow bar
      @store.transaction(true) do |store|
        page.body = %q{<div id="__reshow_bodies__"></div>}
        store[path].reverse.each do |p|
          page.prepend_to_tag '<div id="__reshow_bodies__">', Page.tag(:div, p.body, :class => '__reshow_body__')
        end
        versions = store[path].count
        # Insert Reshow Bar
        page.append_to_tag '</body>', toolbar(versions)
        # Insert versioned stylesheets
        versions.times do |v|
          escaped_path = Rack::Utils.escape(path)
          href = "/__reshow__/assets?path=#{escaped_path}&version=#{v}"
          page.append_to_tag '</head>', "<link charset='utf-8' href='#{href}' rel='stylesheet' type='text/css'>"
        end
      end
      headers['Content-Length'] = page.length.to_s
      body = [page.to_s]
    end
  end
  [status, headers, body]
end

#purge!Object



155
156
157
# File 'lib/rack/reshow.rb', line 155

def purge!
  Object::File.delete(@@store_file) if Object::File.exists?(@@store_file)
end